Download Any YouTube Video From Anywhere 2016 – User JS for Opera 12.x, Firefox and Android

SCREENSHOT-Subhash-Browser-YouTube-Video-DownloadMy Internet connection does not allow me to play videos live. I need to download them first and then play them. I used to use a FireFox extension called Download YouTube as FLV and MP4 for this. However, my primary browser is Opera 12.x. Today, I decided to write a User JS that would provide the same functionality in Opera 12.x. (The Opera browser company does not provide 12x anymore. They sold out and provide a crappy Chrome imitation.) UPDATE: I adapted this script for my Android browser app and also created a Greasemonkey version for Firefox. On Youtube pages, it displays a drop-down list with download options in various formats. On other pages that have inline videos, it adds a download button. (Check http://softwarerecs.stackexchange.com/questions/817/software-to-download-youtube-videos-to-hard-drive/32132#32132)

// ==UserScript==
// @name        YouTube Downloader JavaScript
// @namespace   com.vsubhash.js.youtube.downloader
// @description Downloads YouTube videos
// @include     https://www.youtube.com/*
// @version     2
// @grant       none
// ==/UserScript==


/*
 * YouTube DL JavaScript
 * Licensed under GPL v3 as provided at
 * https://www.gnu.org/licenses/gpl.html
 *
 */

document.addEventListener("DOMContentLoaded", addYouTubeDetectingButton, false);

function parseYTPlayer() {
  console.error("Inside parser" );
  console.error("Title" + ytplayer.config.args.title);
  console.error("Format" + unescape(ytplayer.config.args.adaptive_fmts));
  
  try { 
    if (document.getElementsByTagName("video")[0] && (document.getElementsByTagName("video")[0].pause)) {
      document.getElementsByTagName("video")[0].pause();
    }
  } catch (e) {
    window.alert(e);
  }
  
  var arFormatParams, arFormats, i, j, sURL, sQuality, sMimeType, sExtension;
  var oSelect, oOption;
  oSelect = document.getElementById("mvyJsVideoDownloadList");
  
  if (oSelect == null) { 
    return;
  } else {
    // Reset download list
    oSelect.length = 0;    
  }
  
  
  arFormats = ytplayer.config.args.url_encoded_fmt_stream_map.split(",");
  for (i = 0; i < arFormats.length; i++) {
    //console.error("Format " + i + ". " + arFormats[i]);
    
    arFormatParams = arFormats[i].split("&")
    sURL = "";
    for (j = 0; j < arFormatParams.length; j++) { // console.error("\tFormat parameter " + arFormatParams[j]); if (arFormatParams[j].indexOf("url=") > -1) {
        sURL = unescape(arFormatParams[j].substring("url=".length));
      } else {
        if (arFormatParams[j].indexOf("type=") > -1) {
          sMimeType = unescape(arFormatParams[j].substring("type=".length));
        }
        
        if (arFormatParams[j].indexOf("quality=") > -1) {
          sQuality = arFormatParams[j].substring("quality=".length);
        }
      } 
    }
    
    if ((sURL.length > 0) && (sQuality.length > 0) && (sMimeType.length > 0)) {
      oOption = document.createElement("option");
      oOption.text = sQuality + " - " + sMimeType;
      sURL = sURL + 
             "&title=" + 
             escape(ytplayer.config.args.title);
      //console.error("Final URL " + sURL);       
      oOption.setAttribute("onclick", "(function() { location.href = '" + sURL + "'; })();");       
      oSelect.add(oOption);
    }
  }
  
  if (oSelect.length > 0) {
    oSelect.style.display = "inline";
  } else {
    oSelect.style.display = "none";
    window.alert("Some error had occurred");
  }
  
} 


function handle_DownloadButtonClick() {
  if (document.getElementsByTagName("video").length > 0) {
   if (document.getElementsByTagName("video")[0].src.length > 10) {
     document.getElementById("mvJsLink").setAttribute("href",document.getElementsByTagName("video")[0].src + "&title=" + escape(document.title));
     
     document.getElementById("mvJsLink").innerHTML = document.getElementsByTagName("video")[0].src.substring(0,35) + "...";
     document.getElementById("mvJsLink").click();
     
   } else {
     window.alert("First play the video a bit and pause. Then, we will get the video link.");
   }
  } else {
    window.alert("No video has loaded yet.");
  }
}




function addYouTubeDetectingButton() {
  console.log("Executing YouTube detector");
  var i, n, oDlButtonEl, oDlDiv, oVideosList;
  
  if ((location.href.indexOf("youtube.com/embed") != -1) ||
      (location.href.indexOf("youtube.com/watch") != -1)) {
    console.log("Inside YouTube Iframe");
    
    oDlDiv = document.createElement("div");
    oDlDiv.setAttribute("id", "mvyJsDiv");
    oDlDiv.setAttribute("style", "color: black; padding-left: 1em; background-color: orange!important; width: 100%!important; height: 1.2cm!important; font-size: 0.34cm!important; line-height: 0.4cm!important; font-family: sans-serif!important;  ");
    
    if (location.href.indexOf("youtube.com/embed") > -1) {
      oDlButtonEl = document.createElement("input");
      oDlDiv.setAttribute("id", "mvJsButton");    
      oDlButtonEl.setAttribute("type", "button");
      oDlButtonEl.setAttribute("value", "Grab Video");     

      oDlButtonEl.addEventListener("click", handle_DownloadButtonClick, false);
      
      oDlButtonEl.setAttribute("style", "color: white!important; border: 1px solid orange; box-shadow: 2px 2px 5px black; background-color: red!important; height: 0.9cm!important; width: 5cm!important; font-size: 0.4cm!important; line-height: 0.5cm!important; ; font-family: sans-serif!important;");
      oDlDiv.appendChild(oDlButtonEl);
      
      oVidLink = document.createElement("a");
      oVidLink.setAttribute("id", "mvJsLink");        
      oVidLink.setAttribute("style", "color: darkblue!important; text-decoration: underline!important; font-size: 0.3cm!important; margin: 0.2cm 1cm 0.2cm 1cm!important;");
      oVidLink.innerHTML="Video URL";
      oVidLink.setAttribute("href","javascript:window.alert('First play the video a bit and pause. Then, we will get the video link.');");
      oDlDiv.appendChild(oVidLink);
      document.getElementById("player").parentNode.insertBefore(oDlDiv, document.getElementById("player"));
    } else {
      oDlDiv.innerHTML = "Download video from:";
      oVideosList = document.createElement("select");
      oVideosList.setAttribute("id", "mvyJsVideoDownloadList");                
      oVideosList.setAttribute("style", "display: none; font-size: 0.3cm!important; margin: 0.2cm 1cm 0.2cm 1em!important;");
      oDlDiv.appendChild(oVideosList);
      document.getElementById("player").parentNode.insertBefore(oDlDiv, document.getElementById("player"));
      parseYTPlayer();  
    }
    
    document.getElementById("player").setAttribute("style", document.getElementById("player").getAttribute("style") + "height: 90%!important; ");      
    document.getElementsByTagName("body")[0].style="background-color: white!important; line-height: 0.5cm;";
    document.getElementById("player").parentNode.style="background-color: white!important; ";

  }
}




Advertisements
This entry was posted in news and tagged , , , , . Bookmark the permalink.