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