I used the following code to download HTML files and subtitle files over the Internet. But when I give the url of a Youtube video, the download stops very quickly and though some file IS downloaded, it doesn't open. Why ?

class read
{
	public static void main(String aa[])throws IOException
	{
		byte a;
		int  b,c,d;
		URL url=new URL("http://www.youtube.com/v/sHQXU9Kzgr8&color1=0xb1b1b1&color2=0xd0d0d0&hl=en_US&feature=player_detailpage&fs=1");
		URLConnection conn=url.openConnection();
		InputStream stream=conn.getInputStream();
		FileOutputStream out=new FileOutputStream("/home/daud/Desktop/video");
		while ((b=stream.read())!=-1)
			out.write(b);
	}
}

Dani AI

Generated

As suggested, the hex dump is the key diagnostic. The file starts with the bytes "CWS", which is the standard SWF (Shockwave Flash) magic signature for a compressed Flash file — in other words the YouTube player (or a player binary/HTML) was saved, not the raw video stream. That explains the small download and why the file will not open as an MP4/MKV. (archive.org)

A player/embed URL (the old /v/ or other embed endpoints) delivers a player page or binary that then streams the actual media inside the browser; it is not a direct media file URL. Confirming this is done by checking the HTTP response code and headers (Content-Type and Content-Length). A real MP4/progressive file usually begins with an ftyp box (not CWS) and will report a video content-type. (developers.google.com)

Practical checklist and a compact Java pattern to follow:

  • Request the URL with HttpURLConnection and call getResponseCode(); if non-2xx use getErrorStream().
  • Inspect getContentType() and getContentLength() to see whether a player/HTML was returned.
  • Read and write in chunks with buffered streams instead of byte-by-byte; use try-with-resources to close streams. Example pattern (not a verbatim copy of posted code):
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestProperty("User-Agent","Mozilla/5.0");
int code = c.getResponseCode();
String ct = c.getContentType();
InputStream in = (code==200 ? c.getInputStream() : c.getErrorStream());
try (BufferedInputStream bin = new BufferedInputStream(in);
     BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("out.bin"))) {
    byte[] buf = new byte[8192];
    int n;
    while ((n = bin.read(buf)) != -1) bout.write(buf,0,n);
}

Use the URL/URLConnection guidance and buffered-stream best practices when reading network data. (docs.oracle.com)

Finally, note the service rules: downloading videos from YouTube is restricted by its Terms of Service unless an explicit download feature or permission exists. For legitimate needs, use the provider’s official APIs/features or obtain permission. (youtube.com)

Summary: the dump shows a Flash player was saved (CWS), not a video file. The corrective steps are to inspect headers/response code and use buffered reads; to obtain the actual video stream one must use an appropriate, authorized source.

Recommended Answers

All 6 Replies

Maybe the server doesn't want you to download the file.
What do you receive when you have the problem?
Rename the file received to .txt and open it in an editor.

Have you tried catching and displaying any Exceptions that may be thrown?
And if you're reading/writing individual bytes, you should use buffered streams or it may just grind to a near halt on a large file

I tried to catch exceptions but there were none. When I tried to change the file to txt and open it by gedit, it said that it couldn't recognize the coding. Why did the download stop so quickly ?

Can you open the file in a hexeditor and post some of its contents in hex.

This is what I found when I opened it through vi :

CWS^Hn^G^@^@xÚ}UÝrÚF^X=^Had§v^RÚ<98>8Á6n<9b>é´Ó^Bv°<83>û<93>ÆÅ<8e>ë©<8d>)ÔIï^X!-b^]!©ÒB /Ð\ù®Ìä¢ïÒ«¾G<9f>Äýv^E6^]§EÃj÷ûÿÎÙ]^M1÷^LÈü     ¬'°^?^Wôû6ihÀ¸^Bð~è^B]!<82>/<8b>E^.<8c>^Dï9^EËï^U^G¼^X}ÿãÏg»?üê<84><95>b÷^W<9b>u̾+
ç<81><83>Õñ^RÀÈ<93>õÚ̶<99>M<92>^L`ù®^_n^A¥¡]<92>Ol×aÃ^@Ø-mVJ[$1<80>PºnÒü+@pá2 ÎüÀeùï<98>ýü1^_pÏÉ¿ä¢ë÷EþÀe<96>^H¹ÅÅ(ozvþ¥)
<85>ÂLÊM<99>²½)^_<92>>^DÌ<81>Ó
MAA<80>r¡¼UÞÞÞ*oíî<!u^V^Xp<9b>ù-n^C3-<92>ê^^à2Ï^QÝVÄ,ß³#ª²ü<98>^T9Ù<85>)ú!U^Z¸æ<88><85>-<9b>    <93>»<81>é0Òß¡<94>®ë¿n)<âÞ^H<87>èuç
Üh^FÛ<91>(<92>ªh^EÑ^W<83><8e>K¸<94>v+^Ei¼:þ<84>¼^Ñÿ°^R^M<9f><88>Íýç/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ã©)wÊ<95>TNºfè<92>©ÛP¡5=^Qo©<8f>¿ÉÒ×l<9f>¦oÖèP<9c>ü´×¬¶Nö<8e>j^P]^^M/*u-©s%^OSvrÎÒq,CSe&^[Yl'åMþY,Gs^T  ÖCn¼@÷1³ú!}^?P^[/NîÚ}_<85>%7ú¼â^_^D&£g
"ff.txt" [Incomplete last line][converted] 6 lines, 1648 characters

That doesn't look like hex characters.
Can you open it in a hex editor and display the contents in hex?
Also open a video file of the same type in the hex editor and compare the first few bytes of it to the mystery file's contents to see if you were starting to get a video file and then were cut off.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.