@ -47,7 +47,7 @@ class InfoExtractor(object):
uploader_id : Nickname or id of the video uploader .
uploader_id : Nickname or id of the video uploader .
location : Physical location of the video .
location : Physical location of the video .
player_url : SWF Player URL ( used for rtmpdump ) .
player_url : SWF Player URL ( used for rtmpdump ) .
subtitles : The . srt file contents .
subtitles : The subtitle file contents .
urlhandle : [ internal ] The urlHandle to be used to download the file ,
urlhandle : [ internal ] The urlHandle to be used to download the file ,
like returned by urllib . request . urlopen
like returned by urllib . request . urlopen
@ -235,56 +235,56 @@ class YoutubeIE(InfoExtractor):
def _get_available_subtitles ( self , video_id ) :
def _get_available_subtitles ( self , video_id ) :
request = compat_urllib_request . Request ( ' http://video.google.com/timedtext?hl=en&type=list&v= %s ' % video_id )
request = compat_urllib_request . Request ( ' http://video.google.com/timedtext?hl=en&type=list&v= %s ' % video_id )
try :
try :
s rt _list = compat_urllib_request . urlopen ( request ) . read ( ) . decode ( ' utf-8 ' )
s ub _list = compat_urllib_request . urlopen ( request ) . read ( ) . decode ( ' utf-8 ' )
except ( compat_urllib_error . URLError , compat_http_client . HTTPException , socket . error ) as err :
except ( compat_urllib_error . URLError , compat_http_client . HTTPException , socket . error ) as err :
return ( u ' WARNING: unable to download video subtitles: %s ' % compat_str ( err ) , None )
return ( u ' WARNING: unable to download video subtitles: %s ' % compat_str ( err ) , None )
s rt _lang_list = re . findall ( r ' name= " ([^ " ]*) " [^>]+lang_code= " ([ \ w \ -]+) " ' , s rt _list)
s ub _lang_list = re . findall ( r ' name= " ([^ " ]*) " [^>]+lang_code= " ([ \ w \ -]+) " ' , s ub _list)
s rt _lang_list = dict ( ( l [ 1 ] , l [ 0 ] ) for l in s rt _lang_list)
s ub _lang_list = dict ( ( l [ 1 ] , l [ 0 ] ) for l in s ub _lang_list)
if not s rt _lang_list:
if not s ub _lang_list:
return ( u ' WARNING: video has no closed captions ' , None )
return ( u ' WARNING: video has no closed captions ' , None )
return s rt _lang_list
return s ub _lang_list
def _request_subtitle ( self , s tr_lang, str _name, video_id , format = ' srt ' ) :
def _request_subtitle ( self , s ub_lang, sub _name, video_id , format = ' srt ' ) :
self . report_video_subtitles_request ( video_id , s tr _lang)
self . report_video_subtitles_request ( video_id , s ub _lang)
params = compat_urllib_parse . urlencode ( {
params = compat_urllib_parse . urlencode ( {
' lang ' : s tr _lang,
' lang ' : s ub _lang,
' name ' : s tr _name,
' name ' : s ub _name,
' v ' : video_id ,
' v ' : video_id ,
' fmt ' : format ,
' fmt ' : format ,
} )
} )
url = ' http://www.youtube.com/api/timedtext? ' + params
url = ' http://www.youtube.com/api/timedtext? ' + params
try :
try :
s rt = compat_urllib_request . urlopen ( url ) . read ( ) . decode ( ' utf-8 ' )
s ub = compat_urllib_request . urlopen ( url ) . read ( ) . decode ( ' utf-8 ' )
except ( compat_urllib_error . URLError , compat_http_client . HTTPException , socket . error ) as err :
except ( compat_urllib_error . URLError , compat_http_client . HTTPException , socket . error ) as err :
return ( u ' WARNING: unable to download video subtitles: %s ' % compat_str ( err ) , None )
return ( u ' WARNING: unable to download video subtitles: %s ' % compat_str ( err ) , None )
if not s rt :
if not s ub :
return ( u ' WARNING: Did not fetch video subtitles ' , None )
return ( u ' WARNING: Did not fetch video subtitles ' , None )
return ( None , s tr_lang, srt )
return ( None , s ub_lang, sub )
def _extract_subtitle ( self , video_id ) :
def _extract_subtitle ( self , video_id ) :
self . report_video_subtitles_download ( video_id )
self . report_video_subtitles_download ( video_id )
s rt _lang_list = self . _get_available_subtitles ( video_id )
s ub _lang_list = self . _get_available_subtitles ( video_id )
if self . _downloader . params . get ( ' subtitleslang ' , False ) :
if self . _downloader . params . get ( ' subtitleslang ' , False ) :
s rt _lang = self . _downloader . params . get ( ' subtitleslang ' )
s ub _lang = self . _downloader . params . get ( ' subtitleslang ' )
elif ' en ' in s rt _lang_list:
elif ' en ' in s ub _lang_list:
s rt _lang = ' en '
s ub _lang = ' en '
else :
else :
s rt_lang = list ( srt _lang_list. keys ( ) ) [ 0 ]
s ub_lang = list ( sub _lang_list. keys ( ) ) [ 0 ]
if not s rt_lang in srt _lang_list:
if not s ub_lang in sub _lang_list:
return ( u ' WARNING: no closed captions found in the specified language " %s " ' % s rt _lang, None )
return ( u ' WARNING: no closed captions found in the specified language " %s " ' % s ub _lang, None )
sub = self . _request_subtitle ( s rt_lang, srt_lang_list [ srt _lang] . encode ( ' utf-8 ' ) , video_id )
sub title = self . _request_subtitle ( s ub_lang, sub_lang_list [ sub _lang] . encode ( ' utf-8 ' ) , video_id )
return [ sub ]
return [ sub title ]
def _extract_all_subtitles ( self , video_id ) :
def _extract_all_subtitles ( self , video_id ) :
self . report_video_subtitles_download ( video_id )
self . report_video_subtitles_download ( video_id )
s rt _lang_list = self . _get_available_subtitles ( video_id )
s ub _lang_list = self . _get_available_subtitles ( video_id )
sub s = [ ]
sub title s = [ ]
for s rt_lang in srt _lang_list:
for s ub_lang in sub _lang_list:
sub = self . _request_subtitle ( s rt_lang, srt_lang_list [ srt _lang] . encode ( ' utf-8 ' ) , video_id )
sub title = self . _request_subtitle ( s ub_lang, sub_lang_list [ sub _lang] . encode ( ' utf-8 ' ) , video_id )
sub s. append ( sub )
sub title s. append ( sub title )
return sub s
return sub title s
def _print_formats ( self , formats ) :
def _print_formats ( self , formats ) :
print ( ' Available formats: ' )
print ( ' Available formats: ' )
@ -511,16 +511,16 @@ class YoutubeIE(InfoExtractor):
if self . _downloader . params . get ( ' writesubtitles ' , False ) :
if self . _downloader . params . get ( ' writesubtitles ' , False ) :
video_subtitles = self . _extract_subtitle ( video_id )
video_subtitles = self . _extract_subtitle ( video_id )
if video_subtitles :
if video_subtitles :
( s rt_error, srt_lang , srt ) = video_subtitles [ 0 ]
( s ub_error, sub_lang , sub ) = video_subtitles [ 0 ]
if s rt _error:
if s ub _error:
self . _downloader . trouble ( s rt _error)
self . _downloader . trouble ( s ub _error)
if self . _downloader . params . get ( ' allsubtitles ' , False ) :
if self . _downloader . params . get ( ' allsubtitles ' , False ) :
video_subtitles = self . _extract_all_subtitles ( video_id )
video_subtitles = self . _extract_all_subtitles ( video_id )
for video_subtitle in video_subtitles :
for video_subtitle in video_subtitles :
( s rt_error, srt_lang , srt ) = video_subtitle
( s ub_error, sub_lang , sub ) = video_subtitle
if s rt _error:
if s ub _error:
self . _downloader . trouble ( s rt _error)
self . _downloader . trouble ( s ub _error)
if ' length_seconds ' not in video_info :
if ' length_seconds ' not in video_info :
self . _downloader . trouble ( u ' WARNING: unable to extract video duration ' )
self . _downloader . trouble ( u ' WARNING: unable to extract video duration ' )