mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-12-29 21:31:23 +00:00
Some minor fixes
This commit is contained in:
@@ -33,12 +33,12 @@ def get_arguments():
|
|||||||
help='download songs from a file')
|
help='download songs from a file')
|
||||||
group.add_argument('-u', '--username',
|
group.add_argument('-u', '--username',
|
||||||
help="load user's playlists into <playlist_name>.txt")
|
help="load user's playlists into <playlist_name>.txt")
|
||||||
parser.add_argument('-n', '--no-convert', default=False,
|
|
||||||
help='skip the conversion process and meta-tags', action='store_true')
|
|
||||||
parser.add_argument('-m', '--manual', default=False,
|
parser.add_argument('-m', '--manual', default=False,
|
||||||
help='choose the song to download manually', action='store_true')
|
help='choose the song to download manually', action='store_true')
|
||||||
|
parser.add_argument('-nm', '--no-metadata', default=False,
|
||||||
|
help='do not embed metadata in songs', action='store_true')
|
||||||
parser.add_argument('-f', '--ffmpeg', default=False,
|
parser.add_argument('-f', '--ffmpeg', default=False,
|
||||||
help='Use ffmpeg instead of libav for conversion. If not set defaults to libav',
|
help='Use ffmpeg for conversion otherwise set defaults to libav',
|
||||||
action='store_true')
|
action='store_true')
|
||||||
parser.add_argument('-v', '--verbose', default=False,
|
parser.add_argument('-v', '--verbose', default=False,
|
||||||
help='show debug output', action='store_true')
|
help='show debug output', action='store_true')
|
||||||
|
|||||||
87
spotdl.py
87
spotdl.py
@@ -126,18 +126,18 @@ def generate_filename(content):
|
|||||||
title = slugify(title, ok='-_()[]{}', lower=False)
|
title = slugify(title, ok='-_()[]{}', lower=False)
|
||||||
return fix_encoding(title)
|
return fix_encoding(title)
|
||||||
|
|
||||||
def download_song(content, input_ext):
|
def download_song(content):
|
||||||
music_file = generate_filename(content)
|
music_file = generate_filename(content)
|
||||||
if input_ext == '.webm':
|
if args.input_ext == '.webm':
|
||||||
link = content.getbestaudio(preftype='webm')
|
link = content.getbestaudio(preftype='webm')
|
||||||
if link is not None:
|
if link is not None:
|
||||||
link.download(filepath='Music/' + music_file + input_ext)
|
link.download(filepath='Music/' + music_file + args.input_ext)
|
||||||
else:
|
else:
|
||||||
link = content.getbestaudio(preftype='m4a')
|
link = content.getbestaudio(preftype='m4a')
|
||||||
if link is not None:
|
if link is not None:
|
||||||
link.download(filepath='Music/' + music_file + input_ext)
|
link.download(filepath='Music/' + music_file + args.input_ext)
|
||||||
|
|
||||||
def convert_with_avconv(music_file, input_ext, output_ext, verbose):
|
def convert_with_libav(music_file):
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
avconv_path = 'Scripts\\avconv.exe'
|
avconv_path = 'Scripts\\avconv.exe'
|
||||||
else:
|
else:
|
||||||
@@ -146,13 +146,13 @@ def convert_with_avconv(music_file, input_ext, output_ext, verbose):
|
|||||||
avconv_path + ' -loglevel 0 -i "' +
|
avconv_path + ' -loglevel 0 -i "' +
|
||||||
'Music/' +
|
'Music/' +
|
||||||
music_file +
|
music_file +
|
||||||
input_ext + '" -ab 192k "' +
|
args.input_ext + '" -ab 192k "' +
|
||||||
'Music/' +
|
'Music/' +
|
||||||
music_file +
|
music_file +
|
||||||
output_ext + '"')
|
args.output_ext + '"')
|
||||||
os.remove('Music/' + music_file + input_ext)
|
os.remove('Music/' + music_file + args.input_ext)
|
||||||
|
|
||||||
def convert_with_FFmpeg(music_file, input_ext, output_ext, verbose):
|
def convert_with_FFmpeg(music_file):
|
||||||
# What are the differences and similarities between ffmpeg, libav, and avconv?
|
# What are the differences and similarities between ffmpeg, libav, and avconv?
|
||||||
# https://stackoverflow.com/questions/9477115
|
# https://stackoverflow.com/questions/9477115
|
||||||
# ffmeg encoders high to lower quality
|
# ffmeg encoders high to lower quality
|
||||||
@@ -161,31 +161,31 @@ def convert_with_FFmpeg(music_file, input_ext, output_ext, verbose):
|
|||||||
# on MacOS brew install ffmpeg --with-fdk-aac will do just that. Other OS?
|
# on MacOS brew install ffmpeg --with-fdk-aac will do just that. Other OS?
|
||||||
# https://trac.ffmpeg.org/wiki/Encode/AAC
|
# https://trac.ffmpeg.org/wiki/Encode/AAC
|
||||||
#
|
#
|
||||||
print(music_file, input_ext, output_ext, verbose)
|
print(music_file)
|
||||||
if verbose:
|
if args.verbose:
|
||||||
ffmpeg_pre = 'ffmpeg -y '
|
ffmpeg_pre = 'ffmpeg -y '
|
||||||
else:
|
else:
|
||||||
ffmpeg_pre = 'ffmpeg -hide_banner -nostats -v panic -y '
|
ffmpeg_pre = 'ffmpeg -hide_banner -nostats -v panic -y '
|
||||||
|
|
||||||
if input_ext == '.m4a':
|
if args.input_ext == '.m4a':
|
||||||
if output_ext == '.mp3':
|
if args.output_ext == '.mp3':
|
||||||
ffmpeg_params = '-codec:v copy -codec:a libmp3lame -q:a 2 '
|
ffmpeg_params = '-codec:v copy -codec:a libmp3lame -q:a 2 '
|
||||||
elif output_ext == '.webm':
|
elif output_ext == '.webm':
|
||||||
ffmpeg_params = '-c:a libopus -vbr on -b:a 192k -vn '
|
ffmpeg_params = '-c:a libopus -vbr on -b:a 192k -vn '
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
elif input_ext == '.webm':
|
elif args.input_ext == '.webm':
|
||||||
if output_ext == '.mp3':
|
if args.output_ext == '.mp3':
|
||||||
ffmpeg_params = '-ab 192k -ar 44100 -vn '
|
ffmpeg_params = '-ab 192k -ar 44100 -vn '
|
||||||
elif output_ext == '.m4a':
|
elif args.output_ext == '.m4a':
|
||||||
ffmpeg_params = '-cutoff 20000 -c:a libfdk_aac -b:a 256k -vn '
|
ffmpeg_params = '-cutoff 20000 -c:a libfdk_aac -b:a 256k -vn '
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
print('Unknown formats. Unable to convert.', input_ext, output_ext)
|
print('Unknown formats. Unable to convert.', args.input_ext, args.output_ext)
|
||||||
return
|
return
|
||||||
|
|
||||||
if verbose:
|
if args.verbose:
|
||||||
print(ffmpeg_pre +
|
print(ffmpeg_pre +
|
||||||
'-i "Music/' + music_file + input_ext + '" ' +
|
'-i "Music/' + music_file + input_ext + '" ' +
|
||||||
ffmpeg_params +
|
ffmpeg_params +
|
||||||
@@ -196,7 +196,7 @@ def convert_with_FFmpeg(music_file, input_ext, output_ext, verbose):
|
|||||||
'-i "Music/' + music_file + input_ext + '" ' +
|
'-i "Music/' + music_file + input_ext + '" ' +
|
||||||
ffmpeg_params +
|
ffmpeg_params +
|
||||||
'"Music/' + music_file + output_ext + '" ')
|
'"Music/' + music_file + output_ext + '" ')
|
||||||
os.remove('Music/' + music_file + input_ext)
|
os.remove('Music/' + music_file + args.input_ext)
|
||||||
|
|
||||||
def check_exists(music_file, raw_song, islist):
|
def check_exists(music_file, raw_song, islist):
|
||||||
files = os.listdir("Music")
|
files = os.listdir("Music")
|
||||||
@@ -217,8 +217,7 @@ def check_exists(music_file, raw_song, islist):
|
|||||||
if islist:
|
if islist:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
prompt = raw_input(
|
prompt = raw_input('Song with same name has already been downloaded. Re-download? (y/n): ').lower()
|
||||||
'Song with same name has already been downloaded. Re-download? (y/n): ').lower()
|
|
||||||
if prompt == "y":
|
if prompt == "y":
|
||||||
os.remove("Music/" + file)
|
os.remove("Music/" + file)
|
||||||
return False
|
return False
|
||||||
@@ -226,20 +225,20 @@ def check_exists(music_file, raw_song, islist):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
# Remove song from file once downloaded
|
# Remove song from file once downloaded
|
||||||
def fix_metadata(music_file, meta_tags, output_ext):
|
def fix_metadata(music_file, meta_tags):
|
||||||
if meta_tags is None:
|
if meta_tags is None:
|
||||||
print('Could not find meta-tags')
|
print('Could not find meta-tags')
|
||||||
elif output_ext == '.m4a':
|
elif args.output_ext == '.m4a':
|
||||||
print('Fixing meta-tags')
|
print('Fixing meta-tags')
|
||||||
fix_metadata_m4a(music_file, meta_tags, output_ext)
|
fix_metadata_m4a(music_file, meta_tags)
|
||||||
elif output_ext == '.mp3':
|
elif args.output_ext == '.mp3':
|
||||||
print('Fixing meta-tags')
|
print('Fixing meta-tags')
|
||||||
fix_metadata_mp3(music_file, meta_tags, output_ext)
|
fix_metadata_mp3(music_file, meta_tags)
|
||||||
else:
|
else:
|
||||||
print('Cannot embed meta-tags into given output extension')
|
print('Cannot embed meta-tags into given output extension')
|
||||||
|
|
||||||
def fix_metadata_mp3(music_file, meta_tags, output_ext):
|
def fix_metadata_mp3(music_file, meta_tags):
|
||||||
audiofile = EasyID3('Music/' + music_file + output_ext)
|
audiofile = EasyID3('Music/' + music_file + args.output_ext)
|
||||||
audiofile['artist'] = meta_tags['artists'][0]['name']
|
audiofile['artist'] = meta_tags['artists'][0]['name']
|
||||||
audiofile['albumartist'] = meta_tags['artists'][0]['name']
|
audiofile['albumartist'] = meta_tags['artists'][0]['name']
|
||||||
audiofile['album'] = meta_tags['album']['name']
|
audiofile['album'] = meta_tags['album']['name']
|
||||||
@@ -250,13 +249,13 @@ def fix_metadata_mp3(music_file, meta_tags, output_ext):
|
|||||||
audiofile['discnumber'] = [meta_tags['disc_number'], 0]
|
audiofile['discnumber'] = [meta_tags['disc_number'], 0]
|
||||||
audiofile['date'] = meta_tags['release_date']
|
audiofile['date'] = meta_tags['release_date']
|
||||||
audiofile.save(v2_version=3)
|
audiofile.save(v2_version=3)
|
||||||
audiofile = ID3('Music/' + music_file + output_ext)
|
audiofile = ID3('Music/' + music_file + args.output_ext)
|
||||||
albumart = urllib2.urlopen(meta_tags['album']['images'][0]['url'])
|
albumart = urllib2.urlopen(meta_tags['album']['images'][0]['url'])
|
||||||
audiofile["APIC"] = APIC(encoding=3, mime='image/jpeg', type=3, desc=u'Cover', data=albumart.read())
|
audiofile["APIC"] = APIC(encoding=3, mime='image/jpeg', type=3, desc=u'Cover', data=albumart.read())
|
||||||
albumart.close()
|
albumart.close()
|
||||||
audiofile.save(v2_version=3)
|
audiofile.save(v2_version=3)
|
||||||
|
|
||||||
def fix_metadata_m4a(music_file, meta_tags, output_ext):
|
def fix_metadata_m4a(music_file, meta_tags):
|
||||||
# eyed serves only mp3 not aac so using mutagen
|
# eyed serves only mp3 not aac so using mutagen
|
||||||
# Apple has specific tags - see mutagen docs -
|
# Apple has specific tags - see mutagen docs -
|
||||||
# http://mutagen.readthedocs.io/en/latest/api/mp4.html
|
# http://mutagen.readthedocs.io/en/latest/api/mp4.html
|
||||||
@@ -274,7 +273,7 @@ def fix_metadata_m4a(music_file, meta_tags, output_ext):
|
|||||||
'cpil': 'cpil',
|
'cpil': 'cpil',
|
||||||
'tempo': 'tmpo'}
|
'tempo': 'tmpo'}
|
||||||
|
|
||||||
audiofile = MP4('Music/' + music_file + output_ext)
|
audiofile = MP4('Music/' + music_file + args.output_ext)
|
||||||
audiofile[tags['artist']] = meta_tags['artists'][0]['name']
|
audiofile[tags['artist']] = meta_tags['artists'][0]['name']
|
||||||
audiofile[tags['album']] = meta_tags['album']['name']
|
audiofile[tags['album']] = meta_tags['album']['name']
|
||||||
audiofile[tags['title']] = meta_tags['name']
|
audiofile[tags['title']] = meta_tags['name']
|
||||||
@@ -288,17 +287,13 @@ def fix_metadata_m4a(music_file, meta_tags, output_ext):
|
|||||||
albumart.close()
|
albumart.close()
|
||||||
audiofile.save()
|
audiofile.save()
|
||||||
|
|
||||||
def convert_song(music_file, input_ext, output_ext, ffmpeg, verbose):
|
def convert_song(music_file):
|
||||||
print(music_file, input_ext, output_ext, ffmpeg, verbose)
|
if not args.input_ext == args.output_ext:
|
||||||
if not input_ext == output_ext:
|
print('Converting ' + music_file + args.input_ext + ' to ' + args.output_ext[1:])
|
||||||
print('Converting ' + music_file + input_ext + ' to ' + output_ext[1:])
|
if args.ffmpeg:
|
||||||
if ffmpeg:
|
convert_with_FFmpeg(music_file)
|
||||||
convert_with_FFmpeg(music_file, input_ext, output_ext, verbose)
|
|
||||||
else:
|
else:
|
||||||
convert_with_avconv(music_file, input_ext, output_ext, verbose)
|
convert_with_libav(music_file)
|
||||||
else:
|
|
||||||
print('Skipping conversion since input_ext = output_ext')
|
|
||||||
|
|
||||||
|
|
||||||
# Logic behind preparing the song to download to finishing meta-tags
|
# Logic behind preparing the song to download to finishing meta-tags
|
||||||
def grab_single(raw_song, number=None):
|
def grab_single(raw_song, number=None):
|
||||||
@@ -312,12 +307,12 @@ def grab_single(raw_song, number=None):
|
|||||||
print(get_YouTube_title(content, number))
|
print(get_YouTube_title(content, number))
|
||||||
music_file = generate_filename(content)
|
music_file = generate_filename(content)
|
||||||
if not check_exists(music_file, raw_song, islist=islist):
|
if not check_exists(music_file, raw_song, islist=islist):
|
||||||
download_song(content, args.input_ext)
|
download_song(content)
|
||||||
print('')
|
print('')
|
||||||
if not args.no_convert:
|
convert_song(music_file)
|
||||||
convert_song(music_file, args.input_ext, args.output_ext, args.ffmpeg, args.verbose)
|
meta_tags = generate_metadata(raw_song)
|
||||||
meta_tags = generate_metadata(raw_song)
|
if not args.no_metadata:
|
||||||
fix_metadata(music_file, meta_tags, args.output_ext)
|
fix_metadata(music_file, meta_tags)
|
||||||
|
|
||||||
# Fix python2 encoding issues
|
# Fix python2 encoding issues
|
||||||
def grab_list(file):
|
def grab_list(file):
|
||||||
|
|||||||
Reference in New Issue
Block a user