Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- youtube2flac.py
- Downloads audio from YouTube, converts to FLAC, and bundles everything into a
- neat little package, ready for uploading.
- Only works with Python 2.
- Required Python modules: Beautiful Soup 4, requests, YoutubeDL. Also requires
- FFMpeg to be installed and availible on the path. mktorrent is nice too.
- """
- import bs4
- import getpass
- import os
- import pprint
- import requests
- import youtube_dl
- perfect_log = """EAC extraction logfile from New Delhi
- Used Drive : FUTURISTIC ALIEN TECHNOLOGY
- Read offset correction : 312
- Read mode : Secure
- Utilize accurate stream : Yes
- Defeat audio cache : Yes
- Make use of C2 pointers : No
- Fill up missing offset samples with silence : Yes
- Delete leading and trailing silent blocks : No
- Null samples used in CRC calculations : Yes
- Gap handling : Appended to previous track
- Add ID3 tag : No
- Track 1
- Accurately ripped (confidence 9) [APOOLOO] (AR v1)
- Copy OK
- All tracks accurately ripped
- No errors occurred
- End of status report"""
- ytdl_opts = {
- 'format': 'bestaudio/best',
- 'outtmpl': '%(id)s.mp3',
- 'postprocessors': [{
- 'key': 'FFmpegExtractAudio',
- 'preferredcodec': 'mp3',
- }],
- }
- def youtube_search(query, base_url='https://www.youtube.com/results?sp=EgQQARgC&q='):
- r = requests.get(base_url + query)
- soup = bs4.BeautifulSoup(r.text, 'lxml')
- videos = soup.find_all('div', {'data-context-item-id': True}, class_='yt-lockup-video')
- if len(videos) == 0:
- print "no videos found with filters, removing filters"
- return youtube_search(query, base_url='https://www.youtube.com/results?q=')
- return 'https://www.youtube.com/watch?v=' + videos[0]['data-context-item-id']
- def get_audio(youtube_url):
- with youtube_dl.YoutubeDL(ytdl_opts) as downloader:
- downloader.download([youtube_url])
- meta = downloader.extract_info(youtube_url, download=False)
- return meta['id'] + '.mp3'
- def convert_to_flac(filename, output):
- os.system('ffmpeg -i "{}" "{}"'.format(filename, output))
- def write_log_and_cue(out_directory):
- with open(os.path.join(out_directory, out_directory + '.log'), 'w') as log:
- log.write(perfect_log)
- with open(os.path.join(out_directory, out_directory + '.cue'), 'w') as cue:
- cue.write('lmao no one actually opens the cue anyway\n')
- def get_announce_url():
- return raw_input('enter announce url: ')
- def is_mktorrent_available():
- return os.system('which mktorrent') == 0
- def make_torrent(torrent_dir, announce):
- if is_mktorrent_available():
- os.system('mktorrent -a {} -d -o "{}" -p "{}"'.format(announce, torrent_dir + '.torrent', torrent_dir))
- else:
- print 'mktorrent isn\'t installed. looks like you\'re gonna have to make your torrent manually.'
- def main():
- album_name = raw_input('enter album name: ')
- out_directory = album_name + ' [FLAC]'
- if not os.path.exists(out_directory):
- os.makedirs(out_directory)
- youtube_url = youtube_search(album_name + ' full album')
- mp3_file = get_audio(youtube_url)
- convert_to_flac(mp3_file, os.path.join(out_directory, 'album.flac'))
- write_log_and_cue(out_directory)
- make_torrent(out_directory, get_announce_url())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement