Decouple fetching metadata

This commit is contained in:
Ritiek Malhotra
2020-03-22 21:44:04 +05:30
parent dae76a0abb
commit 7413c541d3
12 changed files with 373 additions and 485 deletions

View File

@@ -26,8 +26,18 @@ from spotdl.encode.exceptions import EncoderNotFoundError
class EncoderBase(ABC):
"""
Defined encoders must inherit from this abstract base class
and implement their own functionality for the below defined
methods.
"""
@abstractmethod
def __init__(self, encoder_path, loglevel, additional_arguments):
def __init__(self, encoder_path, loglevel, additional_arguments=[]):
"""
This method must make sure whether specified encoder
is available under PATH.
"""
if shutil.which(encoder_path) is None:
raise EncoderNotFoundError(
"{} executable does not exist or was not found in PATH.".format(
@@ -40,26 +50,51 @@ class EncoderBase(ABC):
@abstractmethod
def set_argument(self, argument):
"""
This method must be used to set any custom functionality
for the encoder by passing arguments to it.
"""
self._additional_arguments += argument.split()
@abstractmethod
def get_encoding(self, filename):
"""
This method must determine the encoding for a local
audio file. Such as "mp3", "wav", "m4a", etc.
"""
_, extension = os.path.splitext(filename)
# Ignore the initial dot from file extension
return extension[1:]
@abstractmethod
def set_debuglog(self):
"""
This method must enable verbose logging in the defined
encoder.
"""
pass
@abstractmethod
def _generate_encode_command(self, input_file, output_file):
"""
This method must the complete command for that would be
used to invoke the encoder and perform the encoding.
"""
pass
@abstractmethod
def _generate_encoding_arguments(self, input_encoding, output_encoding):
"""
This method must return the core arguments for the defined
encoder such as defining the sample rate, audio bitrate,
etc.
"""
pass
@abstractmethod
def re_encode(self, input_encoding, output_encoding):
def re_encode(self, input_file, output_file):
"""
This method must invoke FFmpeg to encode a given input
file to a specified output file.
"""
pass

View File

@@ -60,9 +60,12 @@ class EncoderFFmpeg(EncoderBase):
def set_debuglog(self):
self._loglevel = "-loglevel debug"
def _generate_encode_command(self, input_file, output_file):
input_encoding = self.get_encoding(input_file)
output_encoding = self.get_encoding(output_file)
def _generate_encode_command(self, input_file, output_file,
input_encoding=None, output_encoding=None):
if input_encoding is None:
input_encoding = self.get_encoding(input_file)
if output_encoding is None:
output_encoding = self.get_encoding(output_file)
arguments = self._generate_encoding_arguments(
input_encoding,
output_encoding
@@ -82,9 +85,20 @@ class EncoderFFmpeg(EncoderBase):
input_file,
output_file
)
returncode = subprocess.call(encode_command)
encode_successful = returncode == 0
process = subprocess.Popen(encode_command)
process.wait()
encode_successful = process.returncode == 0
if encode_successful and delete_original:
os.remove(input_file)
return process
def re_encode_from_stdin(self, input_encoding, output_file):
output_encoding = self.get_encoding(output_file)
encode_command = self._generate_encode_command(
"-",
output_file,
input_encoding=input_encoding,
)
process = subprocess.Popen(encode_command)
return process
return returncode