// ==UserScript==
// @name	Youtube Flash-No-Mo
// @namespace	http://superhappykittymeow.com
// @description	makes the youtubes into html5 with mp4 videos so those cute kitten videos won't chew up all your CPU
// @author	Kale Stedman
// @include     http://www.youtube.com/watch?*
// @version	1.0
// ==/UserScript==
/* thanks to Christian Pedersen for Youtube HTML5 Converter, on which this userscript is based, Google Operating System's blog post http://googlesystem.blogspot.com/2008/04/download-youtube-videos-as-mp4-files.html, and Bouke Regnerus's clever PHP approach to the same goal.

Do note that not all browsers support HTML5 -- or even, for that matter, MP4s via HTML5.  This works fine on Safari4 with Greasekit, and I'm certain it'll work in other webkit-based browsers with no issue.  Firefox 3.5, however, has no built-in handler for mp4 video, so you're likely going to want to install some sort of plugin to allow for MP4 playability if that is your browser of choice.

This UserScript lays atop Youtube and strips out the Flash video, replacing it instead with an embedded MP4 using HTML5's <video> tag.  This has the benefit of drastically reducing CPU usage on both Linux and OSX, as well as removing in-video advertisements (as they are also Flash).  In addition, I've added a link to download the current video on the sidebar.

KNOWN BUGS:
	* Playlist auto advance doesn't work.
	* Fullscreen mode is inaccessible (unless you pop the video out to a new window, in which the Flash player is loaded)
	* Only supports fmt=18, which is the vast majority of videos. Need to add support for HD (22) and a fall-through for crappy version if 18's not available.

*/


// Determining the video_id and the hash:

if (!document.getElementById('download-youtube-video')) {
	var video_id=null;
	var video_hash=null;
	var video_player=document.getElementById('movie_player');
	if(video_player){
		var flash_variables=video_player.attributes.getNamedItem('flashvars');
		
		if(flash_variables){
			var flash_values=flash_variables.value;
			
			if(flash_values){
				var video_id_match=flash_values.match(/[^a-z]video_id=([^(\&|$)]*)/);
				if(video_id_match!=null)video_id=video_id_match[1];
				var video_hash_match=flash_values.match(/[^a-z]t=([^(\&|$)]*)/);
				
				if(video_hash_match!=null)video_hash=video_hash_match[1]
			}
		}
	}
				
	if(video_id==null||video_hash==null){
		var args=null;
		try{
			args=yt.getConfig('SWF_ARGS')
		}
						
		catch(e){}
		if(args){
			video_id=args['video_id'];
			video_hash=args['t']
		}
	}

// determine where the actual video is located

    if(video_id!=null&&video_hash!=null){
        var video_url_high = "http://youtube.com/get_video?video_id="+video_id+"&t="+video_hash+"&fmt=18";
   }
}

// Add download link to the sidebar

   var embedElement = document.getElementById('watch-embed-div');   
   if (embedElement) 
   {
     var html5Element = document.createElement('div');
     var html5Content = '<br /> <span id=\'html5-youtube-video\'><a href=\'' + video_url_high + '\'>Download MP4</a></span>';
     html5Element.innerHTML = html5Content;
     embedElement.appendChild(html5Element);
  }
  
// destroy the div in which the flash lives

var parent = document.getElementById('watch-this-vid');
document.getElementById('watch-player-div').innerHTML='';

// time to create ourselves a <video>!

newvideotag = document.createElement('video');
newvideotag.setAttribute('src',video_url_high);
newvideotag.setAttribute('width','640');
newvideotag.setAttribute('height','360');
newvideotag.setAttribute('controls','true');
//newvideotag.setAttribute('autobuffer','true');
newvideotag.setAttribute('autoplay','true');
// that last option is personal preference. if you don't want video playback to start after enough's in the buffer (about 2-3
// seconds on a modern connection), disable autoplay and set autobuffer. with the latter, the video will load in the background
// but won't play. 

// insert the magic

parent.appendChild(newvideotag);

