Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

YouTube Subtitle Downloader
Saturday, May 5th, 2012 at 6:47:15am MDT 

  1. // ==UserScript==
  2. // @name           Download YouTube Captions
  3. // @namespace      http://userscripts.org/*
  4. // @include        http://www.youtube.com/watch?v=iLSM4HpILQY
  5. // ==/UserScript==
  6. var id = unsafeWindow.pageVideoId;
  7. var caption_array=[false];
  8. function make_timeline(time)
  9. {
  10.         var string="";
  11.         var integer=0;
  12.         time=time+"";
  13.         if(seconds=time.match(/\.(.*)/))
  14.                 seconds=Math.round(parseFloat(seconds[1].substr(0,1)+"."+seconds[1].substr(1,2)));
  15.         else
  16.                 seconds="0";
  17.         time=parseInt(time);
  18.         if(time>=36000)
  19.         {
  20.                 integer=Math.floor(time/3600);
  21.                 time-=3600*integer;
  22.                 string+=integer+":";
  23.         }
  24.         else if(time>=3600)
  25.         {
  26.                 integer=Math.floor(time/3600);
  27.                 time-=3600*integer;
  28.                 string+="0"+integer+":";
  29.         }
  30.         else string+="00:";
  31.         if(time>=600)
  32.         {
  33.                 integer=Math.floor(time/60);
  34.                 time-=60*integer;
  35.                 string+=integer+":";
  36.         }
  37.         else if(time>=60)
  38.         {
  39.                 integer=Math.floor(time/60);
  40.                 time-=60*integer;
  41.                 string+="0"+integer+":";
  42.         }
  43.         else
  44.                 string+="00:";
  45.         integer=Math.floor(time);
  46.         if(time>=10)
  47.                 string+=integer+","+seconds;
  48.         else
  49.                 string+="0"+integer+","+seconds;
  50.         return string;
  51. };
  52. function parse_captions(xml)
  53. {
  54.         var captions=new DOMParser().parseFromString(xml,"text/xml").getElementsByTagName("text");
  55.         var srt_output="";
  56.         Array.forEach(captions,function(caption,i)
  57.         {
  58.                 srt_output+= i + "\n"
  59.                         + make_timeline(caption.getAttribute("start"))
  60.                         + " --> "
  61.                         + make_timeline(parseFloat(caption.getAttribute("start"))
  62.                         + parseFloat(caption.getAttribute("dur")))
  63.                         + "\n" + caption.textContent.replace(/&#(\d+);/g,function($0, $1) {
  64.                                 return String.fromCharCode(Number($1));
  65.                         }).replace(/&/g,"&") + "\n\n";
  66.         });
  67.         return srt_output;
  68. };
  69. function load_caption(selector)
  70. {
  71.         var caption=caption_array[selector.selectedIndex];
  72.         if(!caption)return;
  73.         GM_xmlhttpRequest({
  74.                 method:"GET",
  75.                 url:"http://video.google.com/timedtext?hl="+caption.lang_code+"&lang="+caption.lang_code+"&name="+caption.name+"&v="+id,
  76.                 onload:function(xhr)
  77.                 {
  78.                         if(xhr.responseText!="")
  79.                                 location.href="data:text/srt;charset=utf-8,"+encodeURIComponent(parse_captions(xhr.responseText));
  80.                         else
  81.                                 alert("Error: No response from server.");
  82.                         document.getElementById("captions_selector_option").selected=true;
  83.                 }
  84.         });
  85. };
  86. function load_captions(video_code)
  87. {
  88.         GM_xmlhttpRequest({
  89.                 method:"GET",
  90.                 url:"http://video.google.com/timedtext?hl=en&v="+id+"&type=list",
  91.                 onload:function(xhr)
  92.                 {
  93.                         if(xhr.responseText=="")
  94.                         {
  95.                                 document.getElementById("captions_selector_option").innerHTML="No Captions";
  96.                                 return;
  97.                         }
  98.                         var captions=new DOMParser().parseFromString(xhr.responseText,"text/xml").getElementsByTagName("track");
  99.                         var select=document.getElementById("captions_selector");
  100.                         Array.forEach(captions,function(caption,i)
  101.                         {
  102.                                 var caption_info= {
  103.                                         name : caption.getAttribute("name"),
  104.                                         lang_code : caption.getAttribute("lang_code"),
  105.                                         lang_name : caption.getAttribute("lang_translated")
  106.                                 };
  107.                                 caption_array.push(caption_info);
  108.                                 var option=document.createElement("option");
  109.                                 option.innerHTML=caption_info.lang_name;
  110.                                 select.appendChild(option);
  111.                         });
  112.                         document.getElementById("captions_selector_option").innerHTML="-Download Captions-";
  113.                         document.getElementById("captions_selector").disabled=false;
  114.                 }
  115.         });
  116. };
  117. (function()
  118. {
  119.         var div=document.createElement("div")
  120.         div.setAttribute("style","padding-top:5px;");
  121.         var select=document.createElement("select");
  122.         select.id="captions_selector";
  123.         select.disabled=true;
  124.         var option=document.createElement("option");
  125.         option.id="captions_selector_option";
  126.         option.innerHTML="Loading Captions";
  127.         option.selected=true;
  128.         select.appendChild(option);
  129.         select.addEventListener("change",function()
  130.         {
  131.                 load_caption(this);
  132.         },false);
  133.         div.appendChild(select);
  134.         var tabs=document.getElementById("watch-actions-area").getElementsByTagName("div")[0];
  135.         var last_element=tabs.getElementsByTagName("div");
  136.         tabs.insertBefore(div, last_element[last_element.length-1]);
  137.         load_captions();
  138. })();

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right
worth-right
fantasy-obligation