Remove vbr and cbr options for now; Add temporal files

This commit is contained in:
vitiko98 2021-03-15 16:56:54 -04:00
parent b42b6196c4
commit 001a042e69
1 changed files with 29 additions and 68 deletions

View File

@ -1,9 +1,11 @@
import logging
import os
import shutil
import subprocess
from tempfile import gettempdir
from typing import Optional
from .exceptions import BadEncoderOption, ConversionError
from .exceptions import ConversionError
logger = logging.getLogger(__name__)
@ -15,8 +17,6 @@ class Converter:
codec_lib = None
container = None
lossless = False
cbr_options = None
vbr_options = None
default_ffmpeg_arg = ""
def __init__(
@ -26,7 +26,6 @@ class Converter:
sampling_rate: Optional[int] = None,
bit_depth: Optional[int] = None,
copy_art: bool = False,
**kwargs,
):
"""
:param ffmpeg_arg: The codec ffmpeg argument (defaults to an "optimal value")
@ -42,12 +41,11 @@ class Converter:
self.filename = filename
self.final_fn = f"{os.path.splitext(filename)[0]}.{self.container}"
self.tempfile = os.path.join(gettempdir(), os.path.basename(self.final_fn))
self.sampling_rate = sampling_rate
self.bit_depth = bit_depth
self.copy_art = copy_art
self.overwrite = kwargs.get("overwrite")
if ffmpeg_arg is None:
logger.debug("No arguments provided. Codec defaults will be used")
self.ffmpeg_arg = self.default_ffmpeg_arg
@ -57,7 +55,7 @@ class Converter:
logger.debug("Ffmpeg codec extra argument: %s", self.ffmpeg_arg)
def convert(self, custom_fn: Optional[str] = None, remove_source: bool = False):
def convert(self, custom_fn: Optional[str] = None, remove_source: bool = True):
"""Convert the file.
:param custom_fn: Custom output filename (defaults to the original
@ -68,8 +66,6 @@ class Converter:
"""
if custom_fn:
self.final_fn = custom_fn
else:
self.final_fn = self.filename.replace(self.ext, self.container)
self.command = self._gen_command()
logger.debug("Generated conversion command: %s", self.command)
@ -78,10 +74,12 @@ class Converter:
process.wait()
if os.path.isfile(self.final_fn):
if remove_source:
logger.debug("Source removed: %s", self.filename)
os.remove(self.filename)
logger.debug("Source removed: %s", self.filename)
logger.debug("OK: %s -> %s", self.filename, self.final_fn)
shutil.move(self.tempfile, self.final_fn)
logger.debug("Moved: %s -> %s", self.tempfile, self.final_fn)
logger.debug("Converted: %s -> %s", self.filename, self.final_fn)
else:
raise ConversionError("No file was returned from conversion")
@ -103,23 +101,23 @@ class Converter:
command.extend(self.ffmpeg_arg.split())
if self.lossless:
if self.sampling_rate:
if isinstance(self.sampling_rate, int):
command.extend(["-ar", str(self.sampling_rate)])
if int(self.bit_depth) == 16:
command.extend(["-sample_fmt", "s16"])
elif int(self.bit_depth) in (24, 32):
command.extend(["-sample_fmt", "s32"])
else:
raise ValueError("Bit depth must be 16, 24, or 32")
if self.overwrite:
command.append("-y")
if isinstance(self.bit_depth, int):
if int(self.bit_depth) == 16:
command.extend(["-sample_fmt", "s16"])
elif int(self.bit_depth) in (24, 32):
command.extend(["-sample_fmt", "s32"])
else:
raise ValueError("Bit depth must be 16, 24, or 32")
command.append(self.final_fn)
command.extend(["-y", self.tempfile])
return command
def _is_command_valid(self):
# TODO: add error handling for lossy codecs
if self.ffmpeg_arg is not None and self.lossless:
logger.debug(
"Lossless codecs don't support extra arguments; "
@ -128,51 +126,18 @@ class Converter:
self.ffmpeg_arg = self.default_ffmpeg_arg
return
arg_value = self.ffmpeg_arg.split()[-1].strip().replace("k", "")
try:
arg_value = int(self.ffmpeg_arg.split()[-1].strip())
except ValueError:
raise BadEncoderOption(f"Invalid bitrate argument: {self.ffmpeg_arg}")
logger.debug("Arg value provided: %d", arg_value)
options = []
if self.ffmpeg_arg.startswith("-b:a"):
if self.cbr_options is None:
raise BadEncoderOption("This codec doesn't support constant bitrate")
options = self.cbr_options
if self.ffmpeg_arg.startswith("-q:a"):
if self.vbr_options is None:
raise BadEncoderOption("This codec doesn't support variable bitrate")
options = self.vbr_options
if arg_value not in options:
raise BadEncoderOption(
f"VBR value is not in the codec range: {', '.join(options)}"
)
@property
def ext(self):
return self.filename.split('.')[-1]
class LAME(Converter):
"""
Class for libmp3lame converter. See available options:
Class for libmp3lame converter. Defaul ffmpeg_arg: `-q:a 0`.
See available options:
https://trac.ffmpeg.org/wiki/Encode/MP3
"""
codec_name = "lame"
codec_lib = "libmp3lame"
container = "mp3"
lossless = False
# Blatantly assume nobody will ever convert CBR at less than 96
cbr_options = tuple(range(96, 321, 16))
vbr_options = tuple(range(10))
default_ffmpeg_arg = "-q:a 0" # V0
@ -186,22 +151,21 @@ class ALAC(Converter):
class Vorbis(Converter):
"""
Class for libvorbis converter. See available options:
Class for libvorbis converter. Default ffmpeg_arg: `-q:a 6`.
See available options:
https://trac.ffmpeg.org/wiki/TheoraVorbisEncodingGuide
"""
codec_name = "vorbis"
codec_lib = "libvorbis"
container = "ogg"
lossless = False
vbr_options = tuple(range(-1, 11))
default_ffmpeg_arg = "-q:a 6" # 160, aka the "high" quality profile from Spotify
class OPUS(Converter):
"""
Class for libopus. Currently, this codec takes only `-b:a` as an argument
but, unlike other codecs, it will convert to a variable bitrate.
Class for libopus. Default ffmpeg_arg: `-b:a 128 -vbr on`.
See more:
http://ffmpeg.org/ffmpeg-codecs.html#libopus-1
@ -210,21 +174,18 @@ class OPUS(Converter):
codec_name = "opus"
codec_lib = "libopus"
container = "opus"
lossless = False
cbr_options = tuple(range(16, 513, 16))
default_ffmpeg_arg = "-b:a 128k" # Transparent
class AAC(Converter):
"""
Class for libfdk_aac converter. See available options:
Class for libfdk_aac converter. Default ffmpeg_arg: `-b:a 256k`.
See available options:
https://trac.ffmpeg.org/wiki/Encode/AAC
"""
codec_name = "aac"
codec_lib = "libfdk_aac"
container = "m4a"
lossless = False
cbr_options = tuple(range(16, 513, 16))
# TODO: vbr_options
default_ffmpeg_arg = "-b:a 256k"