faubiguy

Youtube Download Script 2.1

Jul 9th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.18 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import os, glob, re, base64, time, sys
  3. import urllib.request as request
  4. from mutagen.oggvorbis import OggVorbis, OggVorbisHeaderError
  5. #from mutagen.flac import Picture
  6. import youtube_dl
  7.  
  8. #urlPattern = re.compile(r'http://[\w\-\.]+?\.?([\w\-]+\.[\w\-]+)/?[^\s]*', re.UNICODE)
  9. kvPattern = re.compile(r'\b((?:\w+\s+)*\w+)\s*(?::|:)\s*([^\s].*)$', re.UNICODE)
  10. kvPattern2 = re.compile(r'\b\[((?:\w+\s+)*\w+)\]\s*(\w+(?:\s+\w+)*)$', re.UNICODE)
  11. titlePattern = re.compile(r'^([\s\w]*\w)\s*\-\s*(\w[\s\w]*)$', re.UNICODE)
  12. #idPattern = re.compile(r'[\w_\-]{11}', re.UNICODE)
  13. tagdict = {'bpm'     : 'BPM',
  14.         'artist'     : 'ARTIST',
  15.         'song'       : 'TITLE',
  16.         'title'      : 'TITLE',
  17.         'track'      : 'TITLE',
  18.         'genre'      : 'GENRE',
  19.         'language'   : 'LANGUAGE',
  20.         'album'      : 'ALBUM',
  21.         'disc'       : 'ALBUM',
  22.         'website'    : 'WEBSITE',
  23.         'site'       : 'WEBSITE',
  24.         'from'       : 'ORIGINALALBUM',
  25.         'original'   : 'ORIGINAL',
  26.         'source'     : 'ORIGINAL',
  27.         'key'        : 'KEY',
  28.         'arrangement': 'ARRANGER',
  29.         'arranger'   : 'ARRANGER',
  30.         'arrange'    : 'ARRANGER',
  31.         'lyric'      : 'LYRICIST',
  32.         'lyrics'     : 'LYRICIST',
  33.         'lyricist'   : 'LYRICIST',
  34.         }
  35. #TODO website, vocals, vocalist, circle!=artist, lyrics, arranger, orginal, key, lyrics=re.match(/lyrics:(.*?)(?:\s\s|$)/, description), video id, bracketed labels
  36.  
  37. def tag(vorbis, title, description, channel, video_id):
  38.     lines = description.split('\n')
  39.     tags = {}
  40.     for tag, value in (match.group(1,2) for match in (re.search(kvPattern, line) or re.search(kvPattern2, line) for line in lines) if match):
  41.         tag = tag.lower()
  42.         if tag in tagdict:
  43.             tags[tagdict[tag]] = value
  44.         if tag == 'circle' or tag == 'group':
  45.             if 'artist' not in tags:
  46.                 tags['ARTIST'] = value
  47.             continue
  48.         if tag == 'singer' or tag == 'vocals' or tag == 'vocalist' or tag == 'vocal':
  49.             tags['VOCALIST'] = value
  50.             tags['VOCAL'] = 'Yes'
  51.     #print(title)
  52.     titleMatch = re.match(titlePattern, title) or re.match(kvPattern2, title)
  53.     if titleMatch:
  54.         if 'ARTIST' not in tags:
  55.             tags['ARTIST'] = titleMatch.group(1).strip()
  56.         if 'TITLE' not in tags:
  57.             tags['TITLE'] = titleMatch.group(2).strip()
  58.     else:
  59.         if 'TITLE' not in tags:
  60.             tags['TITLE'] = title
  61.         if 'ARTIST' not in tags:
  62.             tags['ARTIST'] = channel
  63.     if 'vocal' in title.lower():
  64.         tags['VOCAL'] = 'Yes'
  65.     tags['UPLOADER'] = channel
  66.     tags['YOUTUBE_ID'] = video_id
  67.     lyrics=re.search(r'lyrics:(.*?)(?:\n\n|$)', description, re.UNICODE + re.IGNORECASE)
  68.     if lyrics:
  69.         tags['LYRICS'] = lyrics.group(1)
  70.         tags['VOCAL'] = 'Yes'
  71.     picture = Picture()
  72.     picture.type = 3
  73.     picture.mime = 'image/jpeg'
  74.     picture.width = 480
  75.     picture.height = 360
  76.     #if os.path.exists(filename+'.jpg'):
  77.         #with open(filename+'.jpg', 'rb') as picfile:
  78.             #picture.data = picfile.read()
  79.     with request.urlopen('http://img.youtube.com/vi/{0}/0.jpg'.format(video_id)) as thumbhttp:
  80.         picture.data = thumbhttp.read()
  81.         #with open(filename+'.jpg', 'wb') as picfile:
  82.             #picfile.write(picture.data)
  83.     tags['METADATA_BLOCK_PICTURE'] = base64.b64encode(picture.write()).decode('ascii')
  84.     vorbis.clear()
  85.     for tag, value in tags.items():
  86.         vorbis[tag] = value
  87.     if 'LANGUAGE' not in tags:
  88.         del vorbis['LANGUAGE']
  89.     vorbis.pprint()
  90.     vorbis.save()
  91.    
  92. #oldcwd = os.getcwd()
  93. #os.chdir('/home/zenith/Music/Library/ytdl')
  94. #with open('dirnum', 'r') as dirnum_file:
  95.     #dirnum = int(dirnum_file.read())
  96. #dirname = 'download_' + str(dirnum)
  97. #if not os.path.exists(dirname):
  98.     #os.mkdir(dirname)
  99. #os.chdir(dirname)
  100.        
  101. class Tagger(youtube_dl.postprocessor.common.PostProcessor):
  102.    
  103.     def run(self, information):
  104.         filename = information['filepath']
  105.         print('Tagging {0}'.format(filename), file=sys.stderr)
  106.         try:
  107.             vorbis = OggVorbis(filename)
  108.         except OggVorbisHeaderError:
  109.             print('Unable to read. Renaming and Skipping.', file=sys.stderr)
  110.             os.rename(filename, '.'+filename)
  111.             information['filepath'] = '.'+filename
  112.         else
  113.             description = information['description']
  114.             uploader = information['uploader']
  115.             title = information['title']
  116.             video_id = information['id']
  117.             tag(vorbis, title, description, uploader, video_id)
  118.             print('Successfully tagged', file=sys.stderr)
  119.         return [], information
  120.    
  121.  
  122. youtube_dl_options = {
  123.     'format': 'bestaudio/best',
  124.     'outtmpl': '%(uploader)s - %(title)s [%(id)s].%(ext)s',
  125.     'postprocessors': [
  126.         {
  127.             'key': 'FFmpegExtractAudio',
  128.             'preferredcodec': 'vorbis',
  129.             'preferredquality': '0',
  130.             'nopostoverwrites': False,
  131.         },
  132.     ],
  133.     'writedescription': True,
  134.     'logtostderr': True
  135. }
  136.  
  137. try:
  138.     with youtube_dl.YoutubeDL(youtube_dl_options) as downloader:
  139.         downloader.add_post_processor(Tagger())
  140.         for video in sys.stdin:
  141.             video = video.strip()
  142.             if video[0] != ';' and video[0] != '#':
  143.                 try:
  144.                     downloader.download([video])
  145.                 except:
  146.                     print('Error occured downloading video: {0}'.format(video), file=sys.stderr)
  147.                 video = '#' + video
  148.             print(video)
  149.         do_extract()
  150. except:
  151.     sys.stdout.write(sys.stdin.read())
  152.  
  153. os.chdir('..')
  154. with open('dirnum', 'w') as dirnum_file:
  155.     dirnum_file.write(str(dirnum+1))
  156.    
  157. #os.chdir(oldcwd)
Add Comment
Please, Sign In to add comment