mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-10-29 18:00:15 +00:00
Add command line option for target folder (-f)
This commit is contained in:
@@ -2,20 +2,20 @@ import subprocess
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
def song(input_song, output_song, avconv=False, verbose=False):
|
def song(input_song, output_song, folder, avconv=False, verbose=False):
|
||||||
"""Do the audio format conversion."""
|
"""Do the audio format conversion."""
|
||||||
if not input_song == output_song:
|
if not input_song == output_song:
|
||||||
print('Converting {0} to {1}'.format(
|
print('Converting {0} to {1}'.format(
|
||||||
input_song, output_song.split('.')[-1]))
|
input_song, output_song.split('.')[-1]))
|
||||||
if avconv:
|
if avconv:
|
||||||
exit_code = convert_with_avconv(input_song, output_song, verbose)
|
exit_code = convert_with_avconv(input_song, output_song, folder, verbose)
|
||||||
else:
|
else:
|
||||||
exit_code = convert_with_ffmpeg(input_song, output_song, verbose)
|
exit_code = convert_with_ffmpeg(input_song, output_song, folder, verbose)
|
||||||
return exit_code
|
return exit_code
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def convert_with_avconv(input_song, output_song, verbose):
|
def convert_with_avconv(input_song, output_song, folder, verbose):
|
||||||
"""Convert the audio file using avconv."""
|
"""Convert the audio file using avconv."""
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
avconv_path = '..\\Scripts\\avconv.exe'
|
avconv_path = '..\\Scripts\\avconv.exe'
|
||||||
@@ -31,12 +31,12 @@ def convert_with_avconv(input_song, output_song, verbose):
|
|||||||
'-loglevel', level,
|
'-loglevel', level,
|
||||||
'-i', 'Music/' + input_song,
|
'-i', 'Music/' + input_song,
|
||||||
'-ab', '192k',
|
'-ab', '192k',
|
||||||
'Music/' + output_song]
|
os.path.join(folder, output_song)]
|
||||||
|
|
||||||
return subprocess.call(command)
|
return subprocess.call(command)
|
||||||
|
|
||||||
|
|
||||||
def convert_with_ffmpeg(input_song, output_song, verbose):
|
def convert_with_ffmpeg(input_song, output_song, folder, verbose):
|
||||||
"""Convert the audio file using FFMpeg.
|
"""Convert the audio file using FFMpeg.
|
||||||
|
|
||||||
What are the differences and similarities between ffmpeg, libav, and avconv?
|
What are the differences and similarities between ffmpeg, libav, and avconv?
|
||||||
@@ -73,7 +73,7 @@ def convert_with_ffmpeg(input_song, output_song, verbose):
|
|||||||
elif output_ext == 'm4a':
|
elif output_ext == 'm4a':
|
||||||
ffmpeg_params = '-cutoff 20000 -c:a libfdk_aac -b:a 192k -vn '
|
ffmpeg_params = '-cutoff 20000 -c:a libfdk_aac -b:a 192k -vn '
|
||||||
|
|
||||||
command = '{0}-i Music/{1} {2}Music/{3}'.format(
|
command = '{0}-i {1} {2}{3}'.format(
|
||||||
ffmpeg_pre, input_song, ffmpeg_params, output_song).split(' ')
|
ffmpeg_pre, os.path.join(folder, input_song), ffmpeg_params, os.path.join(folder, output_song)).split(' ')
|
||||||
|
|
||||||
return subprocess.call(command)
|
return subprocess.call(command)
|
||||||
|
|||||||
@@ -10,12 +10,12 @@ def compare(music_file, metadata):
|
|||||||
already_tagged = False
|
already_tagged = False
|
||||||
try:
|
try:
|
||||||
if music_file.endswith('.mp3'):
|
if music_file.endswith('.mp3'):
|
||||||
audiofile = EasyID3('Music/' + music_file)
|
audiofile = EasyID3(music_file)
|
||||||
# fetch track title metadata
|
# fetch track title metadata
|
||||||
already_tagged = audiofile['title'][0] == metadata['name']
|
already_tagged = audiofile['title'][0] == metadata['name']
|
||||||
elif music_file.endswith('.m4a'):
|
elif music_file.endswith('.m4a'):
|
||||||
tags = {'title': '\xa9nam'}
|
tags = {'title': '\xa9nam'}
|
||||||
audiofile = MP4('Music/' + music_file)
|
audiofile = MP4(music_file)
|
||||||
# fetch track title metadata
|
# fetch track title metadata
|
||||||
already_tagged = audiofile[tags['title']] == metadata['name']
|
already_tagged = audiofile[tags['title']] == metadata['name']
|
||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
@@ -42,7 +42,7 @@ def embed(music_file, meta_tags):
|
|||||||
def embed_mp3(music_file, meta_tags):
|
def embed_mp3(music_file, meta_tags):
|
||||||
"""Embed metadata to MP3 files."""
|
"""Embed metadata to MP3 files."""
|
||||||
# EasyID3 is fun to use ;)
|
# EasyID3 is fun to use ;)
|
||||||
audiofile = EasyID3('Music/' + music_file)
|
audiofile = EasyID3(music_file)
|
||||||
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']
|
||||||
@@ -66,7 +66,7 @@ def embed_mp3(music_file, meta_tags):
|
|||||||
if meta_tags['copyright']:
|
if meta_tags['copyright']:
|
||||||
audiofile['copyright'] = meta_tags['copyright']
|
audiofile['copyright'] = meta_tags['copyright']
|
||||||
audiofile.save(v2_version=3)
|
audiofile.save(v2_version=3)
|
||||||
audiofile = ID3('Music/' + music_file)
|
audiofile = ID3(music_file)
|
||||||
albumart = urllib.request.urlopen(meta_tags['album']['images'][0]['url'])
|
albumart = urllib.request.urlopen(meta_tags['album']['images'][0]['url'])
|
||||||
audiofile["APIC"] = APIC(encoding=3, mime='image/jpeg', type=3,
|
audiofile["APIC"] = APIC(encoding=3, mime='image/jpeg', type=3,
|
||||||
desc=u'Cover', data=albumart.read())
|
desc=u'Cover', data=albumart.read())
|
||||||
@@ -96,7 +96,7 @@ def embed_m4a(music_file, meta_tags):
|
|||||||
'copyright': 'cprt',
|
'copyright': 'cprt',
|
||||||
'tempo': 'tmpo'}
|
'tempo': 'tmpo'}
|
||||||
|
|
||||||
audiofile = MP4('Music/' + music_file)
|
audiofile = MP4(music_file)
|
||||||
audiofile[tags['artist']] = meta_tags['artists'][0]['name']
|
audiofile[tags['artist']] = meta_tags['artists'][0]['name']
|
||||||
audiofile[tags['albumartist']] = meta_tags['artists'][0]['name']
|
audiofile[tags['albumartist']] = meta_tags['artists'][0]['name']
|
||||||
audiofile[tags['album']] = meta_tags['album']['name']
|
audiofile[tags['album']] = meta_tags['album']['name']
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import spotipy.oauth2 as oauth2
|
|||||||
from urllib.request import quote
|
from urllib.request import quote
|
||||||
from slugify import slugify
|
from slugify import slugify
|
||||||
|
|
||||||
|
|
||||||
def input_link(links):
|
def input_link(links):
|
||||||
"""Let the user input a number."""
|
"""Let the user input a number."""
|
||||||
while True:
|
while True:
|
||||||
@@ -51,6 +52,9 @@ def get_arguments():
|
|||||||
'-a', '--avconv', default=False,
|
'-a', '--avconv', default=False,
|
||||||
help='Use avconv for conversion otherwise set defaults to ffmpeg',
|
help='Use avconv for conversion otherwise set defaults to ffmpeg',
|
||||||
action='store_true')
|
action='store_true')
|
||||||
|
parser.add_argument(
|
||||||
|
'-f', '--folder', default='Music/',
|
||||||
|
help='path to folder where files will stored in')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-v', '--verbose', default=False, help='show debug output',
|
'-v', '--verbose', default=False, help='show debug output',
|
||||||
action='store_true')
|
action='store_true')
|
||||||
@@ -105,7 +109,7 @@ def filter_path(path):
|
|||||||
os.makedirs(path)
|
os.makedirs(path)
|
||||||
for temp in os.listdir(path):
|
for temp in os.listdir(path):
|
||||||
if temp.endswith('.temp'):
|
if temp.endswith('.temp'):
|
||||||
os.remove('{0}/{1}'.format(path, temp))
|
os.remove(os.path.join(path, temp))
|
||||||
|
|
||||||
|
|
||||||
def grace_quit():
|
def grace_quit():
|
||||||
|
|||||||
32
spotdl.py
32
spotdl.py
@@ -175,26 +175,26 @@ def download_song(content):
|
|||||||
else:
|
else:
|
||||||
music_file = misc.generate_filename(content.title)
|
music_file = misc.generate_filename(content.title)
|
||||||
link.download(
|
link.download(
|
||||||
filepath='Music/{0}{1}'.format(music_file, args.input_ext))
|
filepath='{0}{1}'.format(os.path.join(args.folder, music_file), args.input_ext))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def check_exists(music_file, raw_song, islist=True):
|
def check_exists(music_file, raw_song, islist=True):
|
||||||
"""Check if the input song already exists in the 'Music' folder."""
|
"""Check if the input song already exists in the given folder."""
|
||||||
songs = os.listdir('Music')
|
songs = os.listdir(args.folder)
|
||||||
for song in songs:
|
for song in songs:
|
||||||
if song.endswith('.temp'):
|
if song.endswith('.temp'):
|
||||||
os.remove(u'Music/{0}'.format(song))
|
os.remove(os.path.join(args.folder, song))
|
||||||
continue
|
continue
|
||||||
# check if any song with similar name is already present in Music/
|
# check if any song with similar name is already present in the given folder
|
||||||
umfile = misc.generate_filename(music_file)
|
umfile = misc.generate_filename(music_file)
|
||||||
if song.startswith(umfile):
|
if song.startswith(umfile):
|
||||||
# check if the already downloaded song has correct metadata
|
# check if the already downloaded song has correct metadata
|
||||||
already_tagged = metadata.compare(song, generate_metadata(raw_song))
|
already_tagged = metadata.compare(os.path.join(args.folder, song), generate_metadata(raw_song))
|
||||||
|
|
||||||
# if not, remove it and download again without prompt
|
# if not, remove it and download again without prompt
|
||||||
if misc.is_spotify(raw_song) and not already_tagged:
|
if misc.is_spotify(raw_song) and not already_tagged:
|
||||||
os.remove('Music/{0}'.format(song))
|
os.remove(os.path.join(args.folder, song))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# do not prompt and skip the current song
|
# do not prompt and skip the current song
|
||||||
@@ -206,7 +206,7 @@ def check_exists(music_file, raw_song, islist=True):
|
|||||||
prompt = input('Song with same name has already been downloaded. '
|
prompt = input('Song with same name has already been downloaded. '
|
||||||
'Re-download? (y/n): ').lower()
|
'Re-download? (y/n): ').lower()
|
||||||
if prompt == 'y':
|
if prompt == 'y':
|
||||||
os.remove('Music/{0}'.format(song))
|
os.remove(os.path.join(args.folder, song))
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
@@ -274,13 +274,13 @@ def grab_single(raw_song, number=None):
|
|||||||
print('')
|
print('')
|
||||||
input_song = music_file + args.input_ext
|
input_song = music_file + args.input_ext
|
||||||
output_song = music_file + args.output_ext
|
output_song = music_file + args.output_ext
|
||||||
convert.song(input_song, output_song, avconv=args.avconv,
|
convert.song(input_song, output_song, args.folder,
|
||||||
verbose=args.verbose)
|
avconv=args.avconv, verbose=args.verbose)
|
||||||
if not args.input_ext == args.output_ext:
|
if not args.input_ext == args.output_ext:
|
||||||
os.remove('Music/{0}'.format(input_song))
|
os.remove(os.path.join(args.folder, input_song))
|
||||||
meta_tags = generate_metadata(raw_song)
|
meta_tags = generate_metadata(raw_song)
|
||||||
if not args.no_metadata:
|
if not args.no_metadata:
|
||||||
metadata.embed(output_song, meta_tags)
|
metadata.embed(os.path.join(args.folder, output_song), meta_tags)
|
||||||
else:
|
else:
|
||||||
print('No audio streams available')
|
print('No audio streams available')
|
||||||
|
|
||||||
@@ -289,6 +289,7 @@ class Args(object):
|
|||||||
manual = False
|
manual = False
|
||||||
input_ext = '.m4a'
|
input_ext = '.m4a'
|
||||||
output_ext = '.mp3'
|
output_ext = '.mp3'
|
||||||
|
folder = 'Music/'
|
||||||
|
|
||||||
args = Args()
|
args = Args()
|
||||||
# token is mandatory when using Spotify's API
|
# token is mandatory when using Spotify's API
|
||||||
@@ -296,15 +297,18 @@ args = Args()
|
|||||||
token = misc.generate_token()
|
token = misc.generate_token()
|
||||||
spotify = spotipy.Spotify(auth=token)
|
spotify = spotipy.Spotify(auth=token)
|
||||||
|
|
||||||
misc.filter_path('Music')
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
os.chdir(sys.path[0])
|
os.chdir(sys.path[0])
|
||||||
args = misc.get_arguments()
|
args = misc.get_arguments()
|
||||||
|
|
||||||
|
misc.filter_path(args.folder)
|
||||||
|
|
||||||
if args.song:
|
if args.song:
|
||||||
grab_single(raw_song=args.song)
|
grab_single(raw_song=args.song)
|
||||||
elif args.list:
|
elif args.list:
|
||||||
grab_list(text_file=args.list)
|
grab_list(text_file=args.list)
|
||||||
elif args.username:
|
elif args.username:
|
||||||
feed_playlist(username=args.username)
|
feed_playlist(username=args.username)
|
||||||
|
else:
|
||||||
|
misc.filter_path('Music')
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import os
|
|||||||
|
|
||||||
raw_song = 'http://open.spotify.com/track/0JlS7BXXD07hRmevDnbPDU'
|
raw_song = 'http://open.spotify.com/track/0JlS7BXXD07hRmevDnbPDU'
|
||||||
|
|
||||||
for x in os.listdir('Music'):
|
for x in os.listdir(spotdl.args.folder):
|
||||||
os.remove('Music/' + x)
|
os.remove(os.path.join(spotdl.args.folder, x))
|
||||||
|
|
||||||
|
|
||||||
def test_spotify_title():
|
def test_spotify_title():
|
||||||
@@ -50,7 +50,7 @@ def test_convert():
|
|||||||
music_file = spotdl.misc.generate_filename(content.title)
|
music_file = spotdl.misc.generate_filename(content.title)
|
||||||
input_song = music_file + spotdl.args.input_ext
|
input_song = music_file + spotdl.args.input_ext
|
||||||
output_song = music_file + spotdl.args.output_ext
|
output_song = music_file + spotdl.args.output_ext
|
||||||
convert = spotdl.convert.song(input_song, output_song)
|
convert = spotdl.convert.song(input_song, output_song, spotdl.args.folder)
|
||||||
assert convert == expect_convert
|
assert convert == expect_convert
|
||||||
|
|
||||||
|
|
||||||
@@ -60,9 +60,9 @@ def test_metadata():
|
|||||||
music_file = spotdl.misc.generate_filename(content.title)
|
music_file = spotdl.misc.generate_filename(content.title)
|
||||||
meta_tags = spotdl.generate_metadata(raw_song)
|
meta_tags = spotdl.generate_metadata(raw_song)
|
||||||
output_song = music_file + spotdl.args.output_ext
|
output_song = music_file + spotdl.args.output_ext
|
||||||
metadata_output = spotdl.metadata.embed(output_song, meta_tags)
|
metadata_output = spotdl.metadata.embed(os.path.join(spotdl.args.folder, output_song), meta_tags)
|
||||||
input_song = music_file + spotdl.args.input_ext
|
input_song = music_file + spotdl.args.input_ext
|
||||||
metadata_input = spotdl.metadata.embed(input_song, meta_tags)
|
metadata_input = spotdl.metadata.embed(os.path.join(spotdl.args.folder, input_song), meta_tags)
|
||||||
assert metadata_output == (metadata_input == expect_metadata)
|
assert metadata_output == (metadata_input == expect_metadata)
|
||||||
|
|
||||||
|
|
||||||
@@ -71,6 +71,6 @@ def test_check_exists2():
|
|||||||
content = spotdl.go_pafy(raw_song)
|
content = spotdl.go_pafy(raw_song)
|
||||||
music_file = spotdl.misc.generate_filename(content.title)
|
music_file = spotdl.misc.generate_filename(content.title)
|
||||||
input_song = music_file + spotdl.args.input_ext
|
input_song = music_file + spotdl.args.input_ext
|
||||||
os.remove('Music/' + input_song)
|
os.remove(os.path.join(spotdl.args.folder, input_song))
|
||||||
check = spotdl.check_exists(music_file, raw_song, islist=True)
|
check = spotdl.check_exists(music_file, raw_song, islist=True)
|
||||||
assert check == expect_check
|
assert check == expect_check
|
||||||
|
|||||||
Reference in New Issue
Block a user