Files
spotify-downloader/spotdl/encode/encode_base.py
Ritiek Malhotra 29005f24ed Refactor exceptions
* Suffix names for custom exceptions with "Error"
* Introduce exceptions for when the coressponding encoder isn't found
2020-03-17 03:09:56 +05:30

66 lines
1.7 KiB
Python

import shutil
import os
from abc import ABC
from abc import abstractmethod
from spotdl.encode.exceptions import EncoderNotFoundError
"""
NOTE ON ENCODERS
================
* A comparision between FFmpeg, avconv, and libav:
https://stackoverflow.com/questions/9477115
* FFmeg encoders sorted in descending order based
on the quality of audio produced:
libopus > libvorbis >= libfdk_aac > aac > libmp3lame
* libfdk_aac encoder, due to copyrights needs to be compiled
by end user on MacOS brew install ffmpeg --with-fdk-aac
will do just that. Other OS? See:
https://trac.ffmpeg.org/wiki/Encode/AAC
"""
class EncoderBase(ABC):
@abstractmethod
def __init__(self, encoder_path, loglevel, additional_arguments):
if shutil.which(encoder_path) is None:
raise EncoderNotFoundError(
"{} executable does not exist or was not found in PATH.".format(
encoder_path
)
)
self.encoder_path = encoder_path
self._loglevel = loglevel
self._additional_arguments = additional_arguments
@abstractmethod
def set_argument(self, argument):
self._additional_arguments += argument.split()
@abstractmethod
def get_encoding(self, filename):
_, extension = os.path.splitext(filename)
# Ignore the initial dot from file extension
return extension[1:]
@abstractmethod
def set_debuglog(self):
pass
@abstractmethod
def _generate_encode_command(self, input_file, output_file):
pass
@abstractmethod
def _generate_encoding_arguments(self, input_encoding, output_encoding):
pass
@abstractmethod
def re_encode(self, input_encoding, output_encoding):
pass