#NEWLINE# def prepare( self , **kwargs ) : #NEWLINE# #INDENT# assert ( self . url or self . vid ) #NEWLINE# if ( ( not self . vid ) and self . url ) : #NEWLINE# #INDENT# self . vid = self . __class__ . get_vid_from_url ( self . url ) #NEWLINE# if ( self . vid is None ) : #NEWLINE# #INDENT# self . download_playlist_by_url ( self . url , ** kwargs ) #NEWLINE# exit ( 0 ) #NEWLINE# #UNINDENT# #UNINDENT# video_info = parse . parse_qs ( get_content ( 'https://www.youtube.com/get_video_info?video_id={}' . format ( self . vid ) ) ) #NEWLINE# if ( 'status' not in video_info ) : #NEWLINE# #INDENT# log . wtf ( '[Failed] Unknown status.' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( video_info [ 'status' ] == [ 'ok' ] ) : #NEWLINE# #INDENT# if ( ( 'use_cipher_signature' not in video_info ) or ( video_info [ 'use_cipher_signature' ] == [ 'False' ] ) ) : #NEWLINE# #INDENT# self . title = parse . unquote_plus ( video_info [ 'title' ] [ 0 ] ) #NEWLINE# stream_list = video_info [ 'url_encoded_fmt_stream_map' ] [ 0 ] . split ( ',' ) #NEWLINE# video_page = get_content ( ( 'https://www.youtube.com/watch?v=%s' % self . vid ) ) #NEWLINE# try : #NEWLINE# #INDENT# ytplayer_config = json . loads ( re . search ( 'ytplayer.config\\s*=\\s*([^\n]+?});' , video_page ) . group ( 1 ) ) #NEWLINE# self . html5player = ( 'https:' + ytplayer_config [ 'assets' ] [ 'js' ] ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# self . html5player = None #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# video_page = get_content ( ( 'https://www.youtube.com/watch?v=%s' % self . vid ) ) #NEWLINE# ytplayer_config = json . loads ( re . search ( 'ytplayer.config\\s*=\\s*([^\n]+?});' , video_page ) . group ( 1 ) ) #NEWLINE# self . title = ytplayer_config [ 'args' ] [ 'title' ] #NEWLINE# self . html5player = ( 'https:' + ytplayer_config [ 'assets' ] [ 'js' ] ) #NEWLINE# stream_list = ytplayer_config [ 'args' ] [ 'url_encoded_fmt_stream_map' ] . split ( ',' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( video_info [ 'status' ] == [ 'fail' ] ) : #NEWLINE# #INDENT# if ( video_info [ 'errorcode' ] == [ '150' ] ) : #NEWLINE# #INDENT# video_page = get_content ( ( 'https://www.youtube.com/watch?v=%s' % self . vid ) ) #NEWLINE# try : #NEWLINE# #INDENT# ytplayer_config = json . loads ( re . search ( 'ytplayer.config\\s*=\\s*([^\n]+});ytplayer' , video_page ) . group ( 1 ) ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# msg = re . search ( 'class="message">([^<]+)<' , video_page ) . group ( 1 ) #NEWLINE# log . wtf ( ( '[Failed] "%s"' % msg . strip ( ) ) ) #NEWLINE# #UNINDENT# if ( 'title' in ytplayer_config [ 'args' ] ) : #NEWLINE# #INDENT# self . title = ytplayer_config [ 'args' ] [ 'title' ] #NEWLINE# self . html5player = ( 'https:' + ytplayer_config [ 'assets' ] [ 'js' ] ) #NEWLINE# stream_list = ytplayer_config [ 'args' ] [ 'url_encoded_fmt_stream_map' ] . split ( ',' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# log . wtf ( '[Error] The uploader has not made this video available in your country.' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( video_info [ 'errorcode' ] == [ '100' ] ) : #NEWLINE# #INDENT# log . wtf ( '[Failed] This video does not exist.' , exit_code = int ( video_info [ 'errorcode' ] [ 0 ] ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# log . wtf ( ( '[Failed] %s' % video_info [ 'reason' ] [ 0 ] ) , exit_code = int ( video_info [ 'errorcode' ] [ 0 ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# log . wtf ( '[Failed] Invalid status.' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for stream in stream_list : #NEWLINE# #INDENT# metadata = parse . parse_qs ( stream ) #NEWLINE# stream_itag = metadata [ 'itag' ] [ 0 ] #NEWLINE# self . streams [ stream_itag ] = { #NEWLINE# #INDENT# 'itag' : metadata [ 'itag' ] [ 0 ] , #NEWLINE# 'url' : metadata [ 'url' ] [ 0 ] , #NEWLINE# 'sig' : ( metadata [ 'sig' ] [ 0 ] if ( 'sig' in metadata ) else None ) , #NEWLINE# 's' : ( metadata [ 's' ] [ 0 ] if ( 's' in metadata ) else None ) , #NEWLINE# 'quality' : metadata [ 'quality' ] [ 0 ] , #NEWLINE# 'type' : metadata [ 'type' ] [ 0 ] , #NEWLINE# 'mime' : metadata [ 'type' ] [ 0 ] . split ( ';' ) [ 0 ] , #NEWLINE# 'container' : mime_to_container ( metadata [ 'type' ] [ 0 ] . split ( ';' ) [ 0 ] ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# caption_tracks = ytplayer_config [ 'args' ] [ 'caption_tracks' ] . split ( ',' ) #NEWLINE# for ct in caption_tracks : #NEWLINE# #INDENT# lang = None #NEWLINE# for i in ct . split ( '&' ) : #NEWLINE# #INDENT# [ k , v ] = i . split ( '=' ) #NEWLINE# if ( ( k == 'lc' ) and ( lang is None ) ) : #NEWLINE# #INDENT# lang = v #NEWLINE# #UNINDENT# if ( ( k == 'v' ) and ( v [ 0 ] != '.' ) ) : #NEWLINE# #INDENT# lang = v #NEWLINE# #UNINDENT# if ( k == 'u' ) : #NEWLINE# #INDENT# ttsurl = parse . unquote_plus ( v ) #NEWLINE# #UNINDENT# #UNINDENT# tts_xml = parseString ( get_content ( ttsurl ) ) #NEWLINE# transcript = tts_xml . getElementsByTagName ( 'transcript' ) [ 0 ] #NEWLINE# texts = transcript . getElementsByTagName ( 'text' ) #NEWLINE# srt = '' #NEWLINE# seq = 0 #NEWLINE# for text in texts : #NEWLINE# #INDENT# if ( text . firstChild is None ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# seq += 1 #NEWLINE# start = float ( text . getAttribute ( 'start' ) ) #NEWLINE# if text . getAttribute ( 'dur' ) : #NEWLINE# #INDENT# dur = float ( text . getAttribute ( 'dur' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# dur = 1.0 #NEWLINE# #UNINDENT# finish = ( start + dur ) #NEWLINE# ( m , s ) = divmod ( start , 60 ) #NEWLINE# ( h , m ) = divmod ( m , 60 ) #NEWLINE# start = '{:0>2}:{:0>2}:{:06.3f}' . format ( int ( h ) , int ( m ) , s ) . replace ( '.' , ',' ) #NEWLINE# ( m , s ) = divmod ( finish , 60 ) #NEWLINE# ( h , m ) = divmod ( m , 60 ) #NEWLINE# finish = '{:0>2}:{:0>2}:{:06.3f}' . format ( int ( h ) , int ( m ) , s ) . replace ( '.' , ',' ) #NEWLINE# content = text . firstChild . nodeValue #NEWLINE# srt += ( '%s\n' % str ( seq ) ) #NEWLINE# srt += ( '%s --> %s\n' % ( start , finish ) ) #NEWLINE# srt += ( '%s\n\n' % content ) #NEWLINE# #UNINDENT# self . caption_tracks [ lang ] = srt #NEWLINE# #UNINDENT# #UNINDENT# except : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# dashmpd = ytplayer_config [ 'args' ] [ 'dashmpd' ] #NEWLINE# dash_xml = parseString ( get_content ( dashmpd ) ) #NEWLINE# for aset in dash_xml . getElementsByTagName ( 'AdaptationSet' ) : #NEWLINE# #INDENT# mimeType = aset . getAttribute ( 'mimeType' ) #NEWLINE# if ( mimeType == 'audio/mp4' ) : #NEWLINE# #INDENT# rep = aset . getElementsByTagName ( 'Representation' ) [ ( - 1 ) ] #NEWLINE# burls = rep . getElementsByTagName ( 'BaseURL' ) #NEWLINE# dash_mp4_a_url = burls [ 0 ] . firstChild . nodeValue #NEWLINE# dash_mp4_a_size = burls [ 0 ] . getAttribute ( 'yt:contentLength' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( mimeType == 'audio/webm' ) : #NEWLINE# #INDENT# rep = aset . getElementsByTagName ( 'Representation' ) [ ( - 1 ) ] #NEWLINE# burls = rep . getElementsByTagName ( 'BaseURL' ) #NEWLINE# dash_webm_a_url = burls [ 0 ] . firstChild . nodeValue #NEWLINE# dash_webm_a_size = burls [ 0 ] . getAttribute ( 'yt:contentLength' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( mimeType == 'video/mp4' ) : #NEWLINE# #INDENT# for rep in aset . getElementsByTagName ( 'Representation' ) : #NEWLINE# #INDENT# w = int ( rep . getAttribute ( 'width' ) ) #NEWLINE# h = int ( rep . getAttribute ( 'height' ) ) #NEWLINE# itag = rep . getAttribute ( 'id' ) #NEWLINE# burls = rep . getElementsByTagName ( 'BaseURL' ) #NEWLINE# dash_url = burls [ 0 ] . firstChild . nodeValue #NEWLINE# dash_size = burls [ 0 ] . getAttribute ( 'yt:contentLength' ) #NEWLINE# self . dash_streams [ itag ] = { #NEWLINE# #INDENT# 'quality' : ( '%sx%s' % ( w , h ) ) , #NEWLINE# 'itag' : itag , #NEWLINE# 'type' : mimeType , #NEWLINE# 'mime' : mimeType , #NEWLINE# 'container' : 'mp4' , #NEWLINE# 'src' : [ dash_url , dash_mp4_a_url ] , #NEWLINE# 'size' : ( int ( dash_size ) + int ( dash_mp4_a_size ) ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( mimeType == 'video/webm' ) : #NEWLINE# #INDENT# for rep in aset . getElementsByTagName ( 'Representation' ) : #NEWLINE# #INDENT# w = int ( rep . getAttribute ( 'width' ) ) #NEWLINE# h = int ( rep . getAttribute ( 'height' ) ) #NEWLINE# itag = rep . getAttribute ( 'id' ) #NEWLINE# burls = rep . getElementsByTagName ( 'BaseURL' ) #NEWLINE# dash_url = burls [ 0 ] . firstChild . nodeValue #NEWLINE# dash_size = burls [ 0 ] . getAttribute ( 'yt:contentLength' ) #NEWLINE# self . dash_streams [ itag ] = { #NEWLINE# #INDENT# 'quality' : ( '%sx%s' % ( w , h ) ) , #NEWLINE# 'itag' : itag , #NEWLINE# 'type' : mimeType , #NEWLINE# 'mime' : mimeType , #NEWLINE# 'container' : 'webm' , #NEWLINE# 'src' : [ dash_url , dash_webm_a_url ] , #NEWLINE# 'size' : ( int ( dash_size ) + int ( dash_webm_a_size ) ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# except : #NEWLINE# #INDENT# if ( not self . html5player ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# self . js = get_content ( self . html5player ) #NEWLINE# if ( 'adaptive_fmts' in ytplayer_config [ 'args' ] ) : #NEWLINE# #INDENT# streams = [ dict ( [ ( i . split ( '=' ) [ 0 ] , parse . unquote ( i . split ( '=' ) [ 1 ] ) ) for i in afmt . split ( '&' ) ] ) for afmt in ytplayer_config [ 'args' ] [ 'adaptive_fmts' ] . split ( ',' ) ] #NEWLINE# for stream in mimeType : #NEWLINE# #INDENT# if stream [ 'type' ] . startswith ( 'audio/mp4' ) : #NEWLINE# #INDENT# dash_mp4_a_url = stream [ 'url' ] #NEWLINE# if ( 's' in stream ) : #NEWLINE# #INDENT# sig = self . __class__ . decipher ( self . js , stream [ 's' ] ) #NEWLINE# dash_mp4_a_url += '&signature={}' . format ( sig ) #NEWLINE# #UNINDENT# dash_mp4_a_size = stream [ 'clen' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if stream [ 'type' ] . startswith ( 'audio/webm' ) : #NEWLINE# #INDENT# dash_webm_a_url = stream [ 'url' ] #NEWLINE# if ( 's' in stream ) : #NEWLINE# #INDENT# sig = self . __class__ . decipher ( self . js , stream [ 's' ] ) #NEWLINE# dash_webm_a_url += '&signature={}' . format ( sig ) #NEWLINE# #UNINDENT# dash_webm_a_size = stream [ 'clen' ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for stream in streams : #NEWLINE# #INDENT# if ( 'size' in stream ) : #NEWLINE# #INDENT# if stream [ 'type' ] . startswith ( 'video/mp4' ) : #NEWLINE# #INDENT# mimeType = 'video/mp4' #NEWLINE# dash_url = stream [ 'url' ] #NEWLINE# if ( 's' in stream ) : #NEWLINE# #INDENT# sig = self . __class__ . decipher ( self . js , stream [ 's' ] ) #NEWLINE# dash_url += '&signature={}' . format ( sig ) #NEWLINE# #UNINDENT# dash_size = stream [ 'clen' ] #NEWLINE# itag = stream [ 'itag' ] #NEWLINE# self . dash_streams [ itag ] = { #NEWLINE# #INDENT# 'quality' : stream [ 'size' ] , #NEWLINE# 'itag' : itag , #NEWLINE# 'type' : mimeType , #NEWLINE# 'mime' : mimeType , #NEWLINE# 'container' : 'mp4' , #NEWLINE# 'src' : [ dash_url , dash_mp4_a_url ] , #NEWLINE# 'size' : ( int ( dash_size ) + int ( dash_mp4_a_size ) ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if stream [ 'type' ] . startswith ( 'video/webm' ) : #NEWLINE# #INDENT# mimeType = 'video/webm' #NEWLINE# dash_url = stream [ 'url' ] #NEWLINE# if ( 's' in stream ) : #NEWLINE# #INDENT# sig = self . __class__ . decipher ( self . js , stream [ 's' ] ) #NEWLINE# dash_url += '&signature={}' . format ( sig ) #NEWLINE# #UNINDENT# dash_size = stream [ 'clen' ] #NEWLINE# itag = stream [ 'itag' ] #NEWLINE# self . dash_streams [ itag ] = { #NEWLINE# #INDENT# 'quality' : stream [ 'size' ] , #NEWLINE# 'itag' : itag , #NEWLINE# 'type' : mimeType , #NEWLINE# 'mime' : mimeType , #NEWLINE# 'container' : 'webm' , #NEWLINE# 'src' : [ dash_url , dash_webm_a_url ] , #NEWLINE# 'size' : ( int ( dash_size ) + int ( dash_webm_a_size ) ) , #NEWLINE# #UNINDENT# }