mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-10-29 18:00:15 +00:00
Refactor conversion and minor changes to metadata
This commit is contained in:
@@ -18,58 +18,60 @@ https://trac.ffmpeg.org/wiki/Encode/AAC
|
||||
def song(input_song, output_song, folder, avconv=False):
|
||||
""" Do the audio format conversion. """
|
||||
if not input_song == output_song:
|
||||
convert = Converter(input_song, output_song, folder)
|
||||
log.info('Converting {0} to {1}'.format(
|
||||
input_song, output_song.split('.')[-1]))
|
||||
if avconv:
|
||||
exit_code = convert_with_avconv(input_song, output_song, folder)
|
||||
exit_code = convert.with_avconv()
|
||||
else:
|
||||
exit_code = convert_with_ffmpeg(input_song, output_song, folder)
|
||||
exit_code = convert.with_ffmpeg()
|
||||
return exit_code
|
||||
return 0
|
||||
|
||||
|
||||
def convert_with_avconv(input_song, output_song, folder):
|
||||
""" Convert the audio file using avconv. """
|
||||
if log.level == 10:
|
||||
level = 'debug'
|
||||
else:
|
||||
level = '0'
|
||||
class Converter:
|
||||
def __init__(self, input_song, output_song, folder):
|
||||
self.input_song = input_song
|
||||
self.output_song = output_song
|
||||
self.folder = folder
|
||||
|
||||
command = ['avconv', '-loglevel', level, '-i',
|
||||
os.path.join(folder, input_song), '-ab', '192k',
|
||||
os.path.join(folder, output_song)]
|
||||
def with_avconv(self):
|
||||
if log.level == 10:
|
||||
level = 'debug'
|
||||
else:
|
||||
level = '0'
|
||||
|
||||
log.debug(command)
|
||||
command = ['avconv', '-loglevel', level, '-i',
|
||||
os.path.join(self.folder, self.input_song), '-ab', '192k',
|
||||
os.path.join(self.folder, self.output_song)]
|
||||
|
||||
return subprocess.call(command)
|
||||
log.debug(command)
|
||||
return subprocess.call(command)
|
||||
|
||||
def with_ffmpeg(self):
|
||||
ffmpeg_pre = 'ffmpeg -y '
|
||||
|
||||
def convert_with_ffmpeg(input_song, output_song, folder):
|
||||
""" Convert the audio file using FFmpeg. """
|
||||
ffmpeg_pre = 'ffmpeg -y '
|
||||
if not log.level == 10:
|
||||
ffmpeg_pre += '-hide_banner -nostats -v panic '
|
||||
|
||||
if not log.level == 10:
|
||||
ffmpeg_pre += '-hide_banner -nostats -v panic '
|
||||
input_ext = self.input_song.split('.')[-1]
|
||||
output_ext = self.output_song.split('.')[-1]
|
||||
|
||||
input_ext = input_song.split('.')[-1]
|
||||
output_ext = output_song.split('.')[-1]
|
||||
if input_ext == 'm4a':
|
||||
if output_ext == 'mp3':
|
||||
ffmpeg_params = '-codec:v copy -codec:a libmp3lame -q:a 2 '
|
||||
elif output_ext == 'webm':
|
||||
ffmpeg_params = '-c:a libopus -vbr on -b:a 192k -vn '
|
||||
|
||||
if input_ext == 'm4a':
|
||||
if output_ext == 'mp3':
|
||||
ffmpeg_params = '-codec:v copy -codec:a libmp3lame -q:a 2 '
|
||||
elif output_ext == 'webm':
|
||||
ffmpeg_params = '-c:a libopus -vbr on -b:a 192k -vn '
|
||||
elif input_ext == 'webm':
|
||||
if output_ext == 'mp3':
|
||||
ffmpeg_params = ' -ab 192k -ar 44100 -vn '
|
||||
elif output_ext == 'm4a':
|
||||
ffmpeg_params = '-cutoff 20000 -c:a libfdk_aac -b:a 192k -vn '
|
||||
|
||||
elif input_ext == 'webm':
|
||||
if output_ext == 'mp3':
|
||||
ffmpeg_params = ' -ab 192k -ar 44100 -vn '
|
||||
elif output_ext == 'm4a':
|
||||
ffmpeg_params = '-cutoff 20000 -c:a libfdk_aac -b:a 192k -vn '
|
||||
command = '{0}-i {1} {2}{3}'.format(
|
||||
ffmpeg_pre, os.path.join(self.folder, self.input_song),
|
||||
ffmpeg_params, os.path.join(self.folder, self.output_song)).split(' ')
|
||||
|
||||
command = '{0}-i {1} {2}{3}'.format(
|
||||
ffmpeg_pre, os.path.join(folder, input_song),
|
||||
ffmpeg_params, os.path.join(folder, output_song)).split(' ')
|
||||
|
||||
log.debug(command)
|
||||
|
||||
return subprocess.call(command)
|
||||
log.debug(command)
|
||||
return subprocess.call(command)
|
||||
|
||||
@@ -8,7 +8,6 @@ import urllib.request
|
||||
|
||||
def compare(music_file, metadata):
|
||||
"""Check if the input music file title matches the expected title."""
|
||||
already_tagged = False
|
||||
try:
|
||||
if music_file.endswith('.mp3'):
|
||||
audiofile = EasyID3(music_file)
|
||||
@@ -17,29 +16,31 @@ def compare(music_file, metadata):
|
||||
audiofile = MP4(music_file)
|
||||
already_tagged = audiofile[tags['title']] == metadata['name']
|
||||
except (KeyError, TypeError):
|
||||
pass
|
||||
already_tagged = False
|
||||
|
||||
return already_tagged
|
||||
|
||||
|
||||
def embed(music_file, meta_tags):
|
||||
""" Embed metadata. """
|
||||
embedder = EmbedMetadata(music_file, meta_tags)
|
||||
embed = EmbedMetadata(music_file, meta_tags)
|
||||
if music_file.endswith('.m4a'):
|
||||
log.info('Applying metadata')
|
||||
return embedder.m4a()
|
||||
return embed.as_m4a()
|
||||
elif music_file.endswith('.mp3'):
|
||||
log.info('Applying metadata')
|
||||
return embedder.mp3()
|
||||
return embed.as_mp3()
|
||||
else:
|
||||
log.warning('Cannot embed metadata into given output extension')
|
||||
return False
|
||||
|
||||
|
||||
class EmbedMetadata:
|
||||
def __init__(self, music_file, meta_tags):
|
||||
self.music_file = music_file
|
||||
self.meta_tags = meta_tags
|
||||
|
||||
def mp3(self):
|
||||
def as_mp3(self):
|
||||
""" Embed metadata to MP3 files. """
|
||||
music_file = self.music_file
|
||||
meta_tags = self.meta_tags
|
||||
@@ -81,7 +82,7 @@ class EmbedMetadata:
|
||||
audiofile.save(v2_version=3)
|
||||
return True
|
||||
|
||||
def m4a(self):
|
||||
def as_m4a(self):
|
||||
""" Embed metadata to M4A files. """
|
||||
music_file = self.music_file
|
||||
meta_tags = self.meta_tags
|
||||
|
||||
Reference in New Issue
Block a user