Press enter to see results or esc to cancel.


Simple JAVA program to download Youtube vidoes


Using the below simple java program you can download Youtube videos. We have to download an executable exe file from below link or their website.

Download youtube-dl here. youtube-dl.rar

Download this exe file and keep in the folder where you need to have your downloaded videos.

1. Project Structure in Eclipse IDE.


2. my_main.java


package com.chillyfacts.com;
import java.io.PrintWriter;
public class my_main {
	public static void main(String[] args) {
		String download_path="E:\\youtube_db_java";
		String url="https://www.youtube.com/watch?v=s34CqtrUS0A";
		String[] command =
	    {
	        "cmd",
	    };
	    Process p;
		try {
			p = Runtime.getRuntime().exec(command); 
		        new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
	                new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
	                PrintWriter stdin = new PrintWriter(p.getOutputStream());
	                stdin.println("cd \""+download_path+"\"");
	                stdin.println(download_path+"\\youtube-dl "+url);
	                stdin.close();
	                p.waitFor();
	    	} catch (Exception e) {
	 		e.printStackTrace();
		}
	}	
}	

Update the path and video url as per your need.

3. SyncPipe.java


package com.chillyfacts.com;
import java.io.InputStream;
import java.io.OutputStream;
class SyncPipe implements Runnable
{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
      istrm_ = istrm;
      ostrm_ = ostrm;
  }
  public void run() {
      try
      {
          final byte[] buffer = new byte[1024];
          for (int length = 0; (length = istrm_.read(buffer)) != -1; )
          {
              ostrm_.write(buffer, 0, length);
          }
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  private final OutputStream ostrm_;
  private final InputStream istrm_;
}

4. Run my_main.java as Java Application, The video should have downloaded in the folder specified.


5. Code to download the video with thumbnails.


package com.chillyfacts.com;
import java.io.PrintWriter;
public class my_main {
	public static void main(String[] args) {
		String download_path="E:\\youtube_db_java";
		String url="https://www.youtube.com/watch?v=s34CqtrUS0A";
		String[] command =
	    {
	        "cmd",
	    };
	    Process p;
		try {
			p = Runtime.getRuntime().exec(command); 
		        new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
	                new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
	                PrintWriter stdin = new PrintWriter(p.getOutputStream());
	                stdin.println("cd \""+download_path+"\"");
	                stdin.println(download_path+"\\youtube-dl "+url+ " --write-all-thumbnails");
	                stdin.close();
	                p.waitFor();
	    	} catch (Exception e) {
	 		e.printStackTrace();
		}
	}	
}	

On running the above code, It will download all the thumbnails available for that video.


6. Download the complete project here.

Download_Youtube_VID

7. This example is completely based on CMD commands. Please see this post for more details.
Download Youtube Videos using Command Prompt CMD