Advertisement
Guest User

youtube2flac.py

a guest
Dec 17th, 2016
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. """
  2.  youtube2flac.py
  3.  
  4.  Downloads audio from YouTube, converts to FLAC, and bundles everything into a
  5.  neat little package, ready for uploading.
  6.  
  7.  Only works with Python 2.
  8.  
  9.  Required Python modules: Beautiful Soup 4, requests, YoutubeDL. Also requires
  10.  FFMpeg to be installed and availible on the path. mktorrent is nice too.
  11. """
  12.  
  13. import bs4
  14. import getpass
  15. import os
  16. import pprint
  17. import requests
  18. import youtube_dl
  19.  
  20. perfect_log = """EAC extraction logfile from New Delhi
  21.  
  22. Used Drive : FUTURISTIC ALIEN TECHNOLOGY
  23. Read offset correction : 312
  24. Read mode : Secure
  25. Utilize accurate stream : Yes
  26. Defeat audio cache : Yes
  27. Make use of C2 pointers : No
  28. Fill up missing offset samples with silence : Yes
  29. Delete leading and trailing silent blocks : No
  30. Null samples used in CRC calculations  : Yes
  31. Gap handling : Appended to previous track
  32. Add ID3 tag : No
  33.  
  34. Track  1
  35.     Accurately ripped (confidence 9)  [APOOLOO]  (AR v1)
  36.     Copy OK
  37.  
  38. All tracks accurately ripped
  39. No errors occurred
  40. End of status report"""
  41.  
  42. ytdl_opts = {
  43.     'format': 'bestaudio/best',
  44.     'outtmpl': '%(id)s.mp3',
  45.     'postprocessors': [{
  46.         'key': 'FFmpegExtractAudio',
  47.         'preferredcodec': 'mp3',
  48.     }],
  49. }
  50.  
  51. def youtube_search(query, base_url='https://www.youtube.com/results?sp=EgQQARgC&q='):
  52.     r = requests.get(base_url + query)
  53.     soup = bs4.BeautifulSoup(r.text, 'lxml')
  54.     videos = soup.find_all('div', {'data-context-item-id': True}, class_='yt-lockup-video')
  55.     if len(videos) == 0:
  56.         print "no videos found with filters, removing filters"
  57.         return youtube_search(query, base_url='https://www.youtube.com/results?q=')
  58.     return 'https://www.youtube.com/watch?v=' + videos[0]['data-context-item-id']
  59.  
  60. def get_audio(youtube_url):
  61.     with youtube_dl.YoutubeDL(ytdl_opts) as downloader:
  62.         downloader.download([youtube_url])
  63.         meta = downloader.extract_info(youtube_url, download=False)
  64.         return meta['id'] + '.mp3'
  65.  
  66. def convert_to_flac(filename, output):
  67.     os.system('ffmpeg -i "{}" "{}"'.format(filename, output))
  68.  
  69. def write_log_and_cue(out_directory):
  70.     with open(os.path.join(out_directory, out_directory + '.log'), 'w') as log:
  71.         log.write(perfect_log)
  72.     with open(os.path.join(out_directory, out_directory + '.cue'), 'w') as cue:
  73.         cue.write('lmao no one actually opens the cue anyway\n')
  74.  
  75. def get_announce_url():
  76.     return raw_input('enter announce url: ')
  77.  
  78. def is_mktorrent_available():
  79.     return os.system('which mktorrent') == 0
  80.  
  81. def make_torrent(torrent_dir, announce):
  82.     if is_mktorrent_available():
  83.         os.system('mktorrent -a {} -d -o "{}" -p "{}"'.format(announce, torrent_dir + '.torrent', torrent_dir))
  84.     else:
  85.         print 'mktorrent isn\'t installed. looks like you\'re gonna have to make your torrent manually.'
  86.  
  87. def main():
  88.     album_name = raw_input('enter album name: ')
  89.     out_directory = album_name + ' [FLAC]'
  90.     if not os.path.exists(out_directory):
  91.         os.makedirs(out_directory)
  92.     youtube_url = youtube_search(album_name + ' full album')
  93.     mp3_file = get_audio(youtube_url)
  94.     convert_to_flac(mp3_file, os.path.join(out_directory, 'album.flac'))
  95.     write_log_and_cue(out_directory)
  96.     make_torrent(out_directory, get_announce_url())
  97.  
  98. if __name__ == '__main__':
  99.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement