Adobe Air Download Youtube Videos

This is some simple javascript for use with the Adobe Air SDK. The below code will show the progress of the download too.

var fileName;
var swfArg = [];
 
// Get the file name.
 
function getVInfo(url) {
		var req = new XMLHttpRequest();
               req.onreadystatechange = function() {
                    if (req.readyState == 4) {
                        var str = req.responseText;
			var stra = str.split("\n");
			var n = 0;
			var n2 = 0;
			var n3 = 0;
			while (n < stra.length) {
			if (stra[n].indexOf("title") != -1 && n2 == 0) {
				n2 = n;
				if (n3 != 0) {
				n = stra.length;
			}
			}
			if (stra[n].indexOf("swfArgs") != -1 && n3 == 0) {
			n3 = n;
			if (n2 != 0) {
			n = stra.length;
			}
			}
			n = n + 1;
			}
			swfArg = stra[n3].split(',');
			fileName = stra[n2];
			fileName = fileName.replace("<title>", "");
			fileName = fileName.replace("</title>", "");
			fileName = "YouTube Downloader/" + fileName.replace("YouTube - ", "") + ".flv";
			fileName = fileName.replace("	", "");
			downVideo(document.getElementById("yurl").value);
                    }
                };
                req.open('GET', url, true);
                req.send(null);
 
			}
	function downVideo(url){
		var n = url.indexOf('v=');
		var n2 = url.indexOf('&', n);
		var len;
		if (n2 != -1) {
		len = n2 - 1;
		}
		else {
			len = url.length;
		}
		var vid = url.substr(n + 2, len);
		la = swfArg[3].split(': ');
		l = la[1].replace("\"", "");
		ska = swfArg[4].split(': ');
		sk = ska[1].replace("\"", "");
		ta = swfArg[5].split(': ');
		t = ta[1].replace("\"", "");
		ha = swfArg[6].split(': ');
		hl = ha[1].replace("\"", "");
		pa = swfArg[7].split(': ');
		plid = pa[1].replace("\"", "");
		var url = 'http://youtube.com/get_video.php?video_id=' + vid + '&l=' + l + '&sk=' + sk + '&t=' + t + '&hl=' + hl + '&plid=' + plid;
		var request = new air.URLRequest(url);
		var urlStream = new air.URLStream();
		var fileData = new air.ByteArray();
		urlStream.addEventListener( air.ProgressEvent.PROGRESS, doProgress );
		urlStream.addEventListener(air.Event.COMPLETE, completeHandler);
		try {
			urlStream.load(request);
		}
		catch (error) {
			air.trace("Unable to load URL: " + error);
		}
 
	function completeHandler(event){
	urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
	var file = air.File.documentsDirectory;
	var fileStream = new air.FileStream();
        file = file.resolvePath(fileName);
	fileStream.open(file, air.FileMode.WRITE);
	fileStream.writeBytes(fileData, 0, fileData.length);
	fileStream.close();
	document.getElementById("crap").innerHTML = "Done downloading " + fileName.replace("YouTube Downloader/", "");
				}
			}
function doProgress( e )
{
	var loaded = e.bytesLoaded;
	var total = e.bytesTotal;
	var pct = Math.ceil( ( loaded / total ) * 100 );
 
	document.getElementById( 'crap' ).innerText = 'Downloading... ' + pct.toString() + '%';
}

Call the function getVInfo(url), which will call the other function. Below is an example of a button that calls that function.

<input type="button" id="downb"  onclick='getVInfo(document.getElementById("yurl").value);' value='Download'/>

There is also a text input box with the id of yurl.

Share and Enjoy:
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • Facebook
  • MySpace
  • Add to favorites
  • StumbleUpon
  • Twitter
  • Yahoo! Bookmarks

Related posts:

  1. Stupidvideos.com trying to charge you to download videos
  2. Crappy seeder
  3. 10 year old from Uruguay uses One Laptop Per Child project laptop to post cow birth to YouTube
  4. YouTube Downloader Preview
  5. OS X stream videos and music to the xbox 360 with fuppes
This entry was posted in Uncategorized and tagged , , , , , , , , , . Bookmark the permalink.



Comments are closed.