mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-10-29 18:00:15 +00:00
Refactor embedding metadata to media
This commit is contained in:
@@ -48,7 +48,6 @@ class EncoderBase(ABC):
|
||||
self._loglevel = loglevel
|
||||
self._additional_arguments = additional_arguments
|
||||
|
||||
@abstractmethod
|
||||
def set_argument(self, argument):
|
||||
"""
|
||||
This method must be used to set any custom functionality
|
||||
@@ -56,13 +55,12 @@ class EncoderBase(ABC):
|
||||
"""
|
||||
self._additional_arguments += argument.split()
|
||||
|
||||
@abstractmethod
|
||||
def get_encoding(self, filename):
|
||||
def get_encoding(self, path):
|
||||
"""
|
||||
This method must determine the encoding for a local
|
||||
audio file. Such as "mp3", "wav", "m4a", etc.
|
||||
"""
|
||||
_, extension = os.path.splitext(filename)
|
||||
_, extension = os.path.splitext(path)
|
||||
# Ignore the initial dot from file extension
|
||||
return extension[1:]
|
||||
|
||||
@@ -75,7 +73,7 @@ class EncoderBase(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _generate_encode_command(self, input_file, output_file):
|
||||
def _generate_encode_command(self, input_path, target_path):
|
||||
"""
|
||||
This method must the complete command for that would be
|
||||
used to invoke the encoder and perform the encoding.
|
||||
@@ -92,9 +90,17 @@ class EncoderBase(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def re_encode(self, input_file, output_file):
|
||||
def re_encode(self, input_path, target_path):
|
||||
"""
|
||||
This method must invoke FFmpeg to encode a given input
|
||||
This method must invoke the encoder to encode a given input
|
||||
file to a specified output file.
|
||||
"""
|
||||
pass
|
||||
|
||||
def re_encode_from_stdin(self, input_encoding, target_path):
|
||||
"""
|
||||
This method must invoke the encoder to encode stdin to a
|
||||
specified output file.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@@ -31,14 +31,11 @@ class EncoderFFmpeg(EncoderBase):
|
||||
raise FFmpegNotFoundError(e.args[0])
|
||||
self._rules = RULES
|
||||
|
||||
def set_argument(self, argument):
|
||||
super().set_argument(argument)
|
||||
|
||||
def set_trim_silence(self):
|
||||
self.set_argument("-af silenceremove=start_periods=1")
|
||||
|
||||
def get_encoding(self, filename):
|
||||
return super().get_encoding(filename)
|
||||
def get_encoding(self, path):
|
||||
return super().get_encoding(path)
|
||||
|
||||
def _generate_encoding_arguments(self, input_encoding, output_encoding):
|
||||
initial_arguments = self._rules.get(input_encoding)
|
||||
@@ -46,26 +43,24 @@ class EncoderFFmpeg(EncoderBase):
|
||||
raise TypeError(
|
||||
'The input format ("{}") is not supported.'.format(
|
||||
input_encoding,
|
||||
)
|
||||
)
|
||||
))
|
||||
arguments = initial_arguments.get(output_encoding)
|
||||
if arguments is None:
|
||||
raise TypeError(
|
||||
'The output format ("{}") is not supported.'.format(
|
||||
output_encoding,
|
||||
)
|
||||
)
|
||||
))
|
||||
return arguments
|
||||
|
||||
def set_debuglog(self):
|
||||
self._loglevel = "-loglevel debug"
|
||||
|
||||
def _generate_encode_command(self, input_file, output_file,
|
||||
def _generate_encode_command(self, input_path, target_path,
|
||||
input_encoding=None, output_encoding=None):
|
||||
if input_encoding is None:
|
||||
input_encoding = self.get_encoding(input_file)
|
||||
input_encoding = self.get_encoding(input_path)
|
||||
if output_encoding is None:
|
||||
output_encoding = self.get_encoding(output_file)
|
||||
output_encoding = self.get_encoding(target_path)
|
||||
arguments = self._generate_encoding_arguments(
|
||||
input_encoding,
|
||||
output_encoding
|
||||
@@ -73,30 +68,30 @@ class EncoderFFmpeg(EncoderBase):
|
||||
command = [self.encoder_path] \
|
||||
+ ["-y", "-nostdin"] \
|
||||
+ self._loglevel.split() \
|
||||
+ ["-i", input_file] \
|
||||
+ ["-i", input_path] \
|
||||
+ arguments.split() \
|
||||
+ self._additional_arguments \
|
||||
+ [output_file]
|
||||
+ [target_path]
|
||||
|
||||
return command
|
||||
|
||||
def re_encode(self, input_file, output_file, delete_original=False):
|
||||
def re_encode(self, input_path, target_path, delete_original=False):
|
||||
encode_command = self._generate_encode_command(
|
||||
input_file,
|
||||
output_file
|
||||
input_path,
|
||||
target_path
|
||||
)
|
||||
process = subprocess.Popen(encode_command)
|
||||
process.wait()
|
||||
encode_successful = process.returncode == 0
|
||||
if encode_successful and delete_original:
|
||||
os.remove(input_file)
|
||||
os.remove(input_path)
|
||||
return process
|
||||
|
||||
def re_encode_from_stdin(self, input_encoding, output_file):
|
||||
output_encoding = self.get_encoding(output_file)
|
||||
def re_encode_from_stdin(self, input_encoding, target_path):
|
||||
output_encoding = self.get_encoding(target_path)
|
||||
encode_command = self._generate_encode_command(
|
||||
"-",
|
||||
output_file,
|
||||
target_path,
|
||||
input_encoding=input_encoding,
|
||||
)
|
||||
process = subprocess.Popen(encode_command, stdin=subprocess.PIPE)
|
||||
|
||||
@@ -15,47 +15,47 @@ class TestEncoderFFmpeg:
|
||||
|
||||
|
||||
class TestEncodingDefaults:
|
||||
def m4a_to_mp3_encoder(input_file, output_file):
|
||||
def m4a_to_mp3_encoder(input_path, target_path):
|
||||
command = [
|
||||
'ffmpeg', '-y', '-nostdin', '-hide_banner', '-nostats', '-v', 'panic',
|
||||
'-i', input_file,
|
||||
'-i', input_path,
|
||||
'-codec:v', 'copy',
|
||||
'-codec:a', 'libmp3lame',
|
||||
'-ar', '48000',
|
||||
'-b:a', '192k',
|
||||
'-vn', output_file
|
||||
'-vn', target_path
|
||||
]
|
||||
return command
|
||||
|
||||
def m4a_to_webm_encoder(input_file, output_file):
|
||||
def m4a_to_webm_encoder(input_path, target_path):
|
||||
command = [
|
||||
'ffmpeg', '-y', '-nostdin', '-hide_banner', '-nostats', '-v', 'panic',
|
||||
'-i', input_file,
|
||||
'-i', input_path,
|
||||
'-codec:a', 'libopus',
|
||||
'-vbr', 'on',
|
||||
'-b:a', '192k',
|
||||
'-vn', output_file
|
||||
'-vn', target_path
|
||||
]
|
||||
return command
|
||||
|
||||
def m4a_to_m4a_encoder(input_file, output_file):
|
||||
def m4a_to_m4a_encoder(input_path, target_path):
|
||||
command = [
|
||||
'ffmpeg', '-y', '-nostdin', '-hide_banner', '-nostats', '-v', 'panic',
|
||||
'-i', input_file,
|
||||
'-i', input_path,
|
||||
'-acodec', 'copy',
|
||||
'-b:a', '192k',
|
||||
'-vn', output_file
|
||||
'-vn', target_path
|
||||
]
|
||||
return command
|
||||
|
||||
def m4a_to_flac_encoder(input_file, output_file):
|
||||
def m4a_to_flac_encoder(input_path, target_path):
|
||||
command = [
|
||||
'ffmpeg', '-y', '-nostdin', '-hide_banner', '-nostats', '-v', 'panic',
|
||||
'-i', input_file,
|
||||
'-i', input_path,
|
||||
'-codec:a', 'flac',
|
||||
'-ar', '48000',
|
||||
'-b:a', '192k',
|
||||
'-vn', output_file
|
||||
'-vn', target_path
|
||||
]
|
||||
return command
|
||||
|
||||
@@ -71,47 +71,47 @@ class TestEncodingDefaults:
|
||||
|
||||
|
||||
class TestEncodingInDebugMode:
|
||||
def m4a_to_mp3_encoder_with_debug(input_file, output_file):
|
||||
def m4a_to_mp3_encoder_with_debug(input_path, target_path):
|
||||
command = [
|
||||
'ffmpeg', '-y', '-nostdin', '-loglevel', 'debug',
|
||||
'-i', input_file,
|
||||
'-i', input_path,
|
||||
'-codec:v', 'copy',
|
||||
'-codec:a', 'libmp3lame',
|
||||
'-ar', '48000',
|
||||
'-b:a', '192k',
|
||||
'-vn', output_file
|
||||
'-vn', target_path
|
||||
]
|
||||
return command
|
||||
|
||||
def m4a_to_webm_encoder_with_debug(input_file, output_file):
|
||||
def m4a_to_webm_encoder_with_debug(input_path, target_path):
|
||||
command = [
|
||||
'ffmpeg', '-y', '-nostdin', '-loglevel', 'debug',
|
||||
'-i', input_file,
|
||||
'-i', input_path,
|
||||
'-codec:a', 'libopus',
|
||||
'-vbr', 'on',
|
||||
'-b:a', '192k',
|
||||
'-vn', output_file
|
||||
'-vn', target_path
|
||||
]
|
||||
return command
|
||||
|
||||
def m4a_to_m4a_encoder_with_debug(input_file, output_file):
|
||||
def m4a_to_m4a_encoder_with_debug(input_path, target_path):
|
||||
command = [
|
||||
'ffmpeg', '-y', '-nostdin', '-loglevel', 'debug',
|
||||
'-i', input_file,
|
||||
'-i', input_path,
|
||||
'-acodec', 'copy',
|
||||
'-b:a', '192k',
|
||||
'-vn', output_file
|
||||
'-vn', target_path
|
||||
]
|
||||
return command
|
||||
|
||||
def m4a_to_flac_encoder_with_debug(input_file, output_file):
|
||||
def m4a_to_flac_encoder_with_debug(input_path, target_path):
|
||||
command = [
|
||||
'ffmpeg', '-y', '-nostdin', '-loglevel', 'debug',
|
||||
'-i', input_file,
|
||||
'-i', input_path,
|
||||
'-codec:a', 'flac',
|
||||
'-ar', '48000',
|
||||
'-b:a', '192k',
|
||||
'-vn', output_file
|
||||
'-vn', target_path
|
||||
]
|
||||
return command
|
||||
|
||||
@@ -128,55 +128,55 @@ class TestEncodingInDebugMode:
|
||||
|
||||
|
||||
class TestEncodingAndTrimSilence:
|
||||
def m4a_to_mp3_encoder_and_trim_silence(input_file, output_file):
|
||||
def m4a_to_mp3_encoder_and_trim_silence(input_path, target_path):
|
||||
command = [
|
||||
'ffmpeg', '-y', '-nostdin', '-hide_banner', '-nostats', '-v', 'panic',
|
||||
'-i', input_file,
|
||||
'-i', input_path,
|
||||
'-codec:v', 'copy',
|
||||
'-codec:a', 'libmp3lame',
|
||||
'-ar', '48000',
|
||||
'-b:a', '192k',
|
||||
'-vn',
|
||||
'-af', 'silenceremove=start_periods=1',
|
||||
output_file
|
||||
target_path
|
||||
]
|
||||
return command
|
||||
|
||||
def m4a_to_webm_encoder_and_trim_silence(input_file, output_file):
|
||||
def m4a_to_webm_encoder_and_trim_silence(input_path, target_path):
|
||||
command = [
|
||||
'ffmpeg', '-y', '-nostdin', '-hide_banner', '-nostats', '-v', 'panic',
|
||||
'-i', input_file,
|
||||
'-i', input_path,
|
||||
'-codec:a', 'libopus',
|
||||
'-vbr', 'on',
|
||||
'-b:a', '192k',
|
||||
'-vn',
|
||||
'-af', 'silenceremove=start_periods=1',
|
||||
output_file
|
||||
target_path
|
||||
]
|
||||
return command
|
||||
|
||||
def m4a_to_m4a_encoder_and_trim_silence(input_file, output_file):
|
||||
def m4a_to_m4a_encoder_and_trim_silence(input_path, target_path):
|
||||
command = [
|
||||
'ffmpeg', '-y', '-nostdin', '-hide_banner', '-nostats', '-v', 'panic',
|
||||
'-i', input_file,
|
||||
'-i', input_path,
|
||||
'-acodec', 'copy',
|
||||
'-b:a', '192k',
|
||||
'-vn',
|
||||
'-af', 'silenceremove=start_periods=1',
|
||||
output_file
|
||||
target_path
|
||||
]
|
||||
return command
|
||||
|
||||
def m4a_to_flac_encoder_and_trim_silence(input_file, output_file):
|
||||
def m4a_to_flac_encoder_and_trim_silence(input_path, target_path):
|
||||
command = [
|
||||
'ffmpeg', '-y', '-nostdin', '-hide_banner', '-nostats', '-v', 'panic',
|
||||
'-i', input_file,
|
||||
'-i', input_path,
|
||||
'-codec:a', 'flac',
|
||||
'-ar', '48000',
|
||||
'-b:a', '192k',
|
||||
'-vn',
|
||||
'-af', 'silenceremove=start_periods=1',
|
||||
output_file
|
||||
target_path
|
||||
]
|
||||
return command
|
||||
|
||||
|
||||
Reference in New Issue
Block a user