2012年6月14日星期四

Get subtitle file for TED video

It's a pity that we cant dowload subtitle for videos at TED.COM.  Some people have written 
desktop software and web app to solve the problem.But now these things can't work at all. 
Maybe TED changed  the contents of web page. So I write the script using Perl today.
 It could get subtitle file from TED.COM and convert it to SRT format(SubRip Subtitle File). 
 The script  could just work, I 'll make it perfect next week. Enjoy it!
Last version: https://gist.github.com/949659 
Executable file(Win32): http://www.tinyurl.com/tedsubtitle
Usage: ted.exe URL languageCode output.src 
[php:nogutter] view plaincopy
  1. ################################################################################  
  2. #  File: ted.pl  
  3. #  Desscription: Get ted talk's subtitle from TED.com and convert subtitle of TED video to SRT format(SubRip Subtitle File)  
  4. #  Usage: ted.pl URL languageCode output.src   
  5. #  Executable File(Win32): http://www.tinyurl.com/tedsubtitle  
  6. #  Create: Thinkhy  
  7. #  Date: 2011.04.30     
  8. #  ChangeLog: 1 Add language code. 2011.05.06  
  9. #  
  10. #  LanguageCode     Language    
  11. #  alb              Albanian  
  12. #  ara              Arabic        
  13. #  bul              Bulgarian          
  14. #  chi_hans         Chinese (Simplified)  
  15. #  chi_hant         Chinese (Traditional)  
  16. #  cze              Czech  
  17. #  dut              Dutch  
  18. #  eng              English  
  19. #  est              Estonian  
  20. #  fin              Finnish  
  21. #  fre_fr           French (France)  
  22. #  ger              German  
  23. #  gre              Greek  
  24. #  heb              Hebrew  
  25. #  hun              Hungarian  
  26. #  ita              Italian  
  27. #  kor              Korean      
  28. #  pol              Polish     
  29. #  por_br           Portuguese (Brazil)  
  30. #  rum              Romanian  
  31. #  rus              Russian  
  32. #  scr              Croatian  
  33. #  spa              Spanish  
  34. #  tur              Turkish  
  35. #  ukr              Ukrainian  
  36. #  
  37. ################################################################################  
  38. ##!/usr/local/bin/perl  
  39.   
  40. use strict;  
  41. use Data::Dumper;  
  42. use JSON;  
  43.   
  44. use LWP::Simple qw(get);  
  45.   
  46. # Magic Number  
  47. my $durationOfAdv = 16000; #  seconds of Advertisement time(millisecond).  
  48.   
  49. # Get content from file  
  50. # my $content = GetContentFromFile("back.json");  
  51.   
  52. # The TED talk URL  
  53. my $url = $ARGV[0];  
  54.   
  55. # languageCode    
  56. my $languageCode = $ARGV[1];  
  57.   
  58. # output file of SRT format  
  59. my $outputFile = $ARGV[2];  
  60.   
  61. # !!Note: What you should do is to write URL of TED talks here.  
  62. # my $url = "http://www.ted.com/talks/stephen_wolfram_computing_a_theory_of_everything.html";  
  63.   
  64. open OUT, ">out.html";  
  65. print "Get html content from URL: $url/n";  
  66.   
  67. # First of all, Get the talkID from the web page.  
  68. my $html = GetUrl($url);  
  69. $html =~ m/(?<=var talkID = ).*?(/d+)/g;  
  70. my $talkID = $1;  
  71. chomp($talkID);  
  72. print  "TalkID: $talkID/n";  
  73.   
  74. #/(?<=/t)/w+/   
  75. print OUT $html;  
  76. print "Have got html content from URL: $url/n";  
  77.   
  78. # Get subtitle content from TED.COM  
  79. my $subtitleUrl = "http://www.ted.com/talks/subtitles/id/$talkID/lang/$languageCode/format/text";   
  80. my $content = GetUrl($subtitleUrl);  
  81.   
  82. open DEBUG, ">out.json";  
  83. print DEBUG $content;  
  84.   
  85. # Decode JSON text  
  86. open SRT, ">$outputFile";  
  87. my $json = new JSON;  
  88. my $obj = $json->decode($content);  
  89.   
  90. my $startTime = $obj->{captions}->[10]->{startTime};  
  91. my $duration = $obj->{captions}->[10]->{duration};  
  92. my $subtitle = $obj->{captions}->[10]->{content};  
  93.   
  94. my $cnt = 0;  
  95. my $len = scalar(@{$obj->{captions}});  
  96. print $len;  
  97. #foreach my $element ($obj->{captions})  
  98. for (;$cnt < $len$cnt++)  
  99. {  
  100.     #my %hash = %$element;  
  101.     my $startTime = $obj->{captions}->[$cnt]->{startTime};  
  102.     my $duration = $obj->{captions}->[$cnt]->{duration};  
  103.     my $subtitle = $obj->{captions}->[$cnt]->{content};  
  104.   
  105.     OutputSrt(1+$cnt$startTime$duration$subtitle);  
  106. }  
  107.   
  108. ###########################################################  
  109. # Sub Functions  
  110. ###########################################################  
  111. sub GetTime  
  112. {  
  113.     my ($time) = @_;  
  114.   
  115.     my $hour = int($time  / 1000 / 3600);   
  116.     my $minute = int((int($time / 1000) % 3600) / 60 );   
  117.     my $second = int($time / 1000) - $hour * 3600 - $minute * 60;   
  118.     my $msecond = $time - ($hour * 3600 + $minute * 60 + $second) * 1000;  
  119.   
  120.     return ($hour$minute$second$msecond);  
  121. }  
  122.   
  123. sub OutputSrt  
  124. {  
  125.     my ($orderNum$startTime$duration$subtitle) = @_;  
  126.   
  127.     # plus the duration of advertisement  
  128.     $startTime += $durationOfAdv;  
  129.   
  130.     # Caculate endTime by duration  
  131.     my $endTime = $startTime + $duration;   
  132.   
  133.     my($hour$minute$second$msecond) = GetTime($startTime);   
  134.   
  135.     print SRT "$orderNum/n"; # order number  
  136.   
  137.     # Begin time  
  138.     print SRT $hour.":".$minute.":".$second.",$msecond";  
  139.   
  140.     # delimitation  
  141.     print SRT " --> ";  
  142.   
  143.     # End time  
  144.     my($hour1$minute1$second1) = GetTime($endTime);   
  145.     print SRT $hour1.":".$minute1.":".$second1.",$msecond/n";  
  146.   
  147.     # Subtitle  
  148.     print SRT "$subtitle/n/n";  
  149. }  
  150.   
  151.   
  152. sub GetContentFromFile  
  153. {  
  154.     my $file = shift;   
  155.     my $content;  
  156.   
  157.     open FILE, $file;    
  158.     while(<FILE>) {  
  159.         $content .= "$_";  
  160.     }  
  161.   
  162.     return $content;  
  163. }  
  164.   
  165. # Test URL: http://www.ted.com/talks/subtitles/id/1130/lang/eng/format/text  
  166. sub GetUrl  
  167. {  
  168.     my $url = shift;  
  169.     my $content = get($url);  
  170.   
  171.     return $content;  
  172. }  

没有评论: