119 lines
3.4 KiB
Python
119 lines
3.4 KiB
Python
from ..transcode_levels import transcode_levels, preset_transcode_levels
|
|
from ..types import File
|
|
from ..meta import ffmpeg_path
|
|
|
|
from subprocess import run as run_command
|
|
|
|
class TranscodeConfig:
|
|
use_quality = False
|
|
use_bitrate = False
|
|
encoder = ""
|
|
file_extension = ""
|
|
container = ""
|
|
bitrate = ""
|
|
quality = ""
|
|
|
|
def load_from_dict(self, data):
|
|
if "use_quality" in data:
|
|
self.use_quality = data["use_quality"]
|
|
if "use_bitrate" in data:
|
|
self.use_bitrate = data["use_bitrate"]
|
|
if "encoder" in data:
|
|
self.encoder = data["encoder"]
|
|
if "file_extension" in data:
|
|
self.file_extension = data["file_extension"]
|
|
if "container" in data:
|
|
self.container = data["container"]
|
|
if "bitrate" in data:
|
|
self.bitrate = data["bitrate"]
|
|
if "quality" in data:
|
|
self.quality = data["quality"]
|
|
return self
|
|
|
|
def get_transcode_config(file: File, level: str):
|
|
conf = TranscodeConfig()
|
|
if level in preset_transcode_levels.keys():
|
|
level = preset_transcode_levels["level"]
|
|
|
|
if level.startswith("opus-") and level.endswith("k"):
|
|
conf.container = "ogg"
|
|
conf.file_extension = "opus"
|
|
conf.encoder = "libopus"
|
|
conf.use_bitrate = True
|
|
# includes the k at end
|
|
conf.bitrate = level.replace("opus-", "")
|
|
return conf
|
|
|
|
if level.startswith("mp3-"):
|
|
conf.container = "mp3"
|
|
conf.file_extension = "mp3"
|
|
conf.encoder = "libmp3lame"
|
|
|
|
if level.startswith("mp3-v"):
|
|
conf.use_quality = True
|
|
conf.quality = level.replace("mp3-v", "")
|
|
return conf
|
|
elif level.startswith("mp3-") and level.endswith("k"):
|
|
conf.use_bitrate = True
|
|
conf.bitrate = level.replace("mp3-", "")
|
|
return conf
|
|
|
|
if level.startswith("vorbis-q"):
|
|
conf.container = "ogg"
|
|
conf.file_extension = "ogg"
|
|
conf.encoder = "libvorbis"
|
|
conf.use_quality = True
|
|
conf.quality = level.replace("vorbis-q", "")
|
|
return conf
|
|
|
|
if level.startswith("speex-q"):
|
|
conf.container = "ogg"
|
|
conf.file_extension = "ogg"
|
|
conf.encoder = "libspeex"
|
|
conf.use_quality = True
|
|
conf.quality = level.replace("speex-q", "")
|
|
return conf
|
|
|
|
print("Unknown Level")
|
|
exit()
|
|
|
|
def transcode(file: File, config: TranscodeConfig, dest: str):
|
|
title = file.tags.title
|
|
artist = file.tags.artist
|
|
|
|
ffmpeg_command = [
|
|
ffmpeg_path,
|
|
"-y",
|
|
"-hide_banner",
|
|
"-loglevel", "warning",
|
|
"-i", file.join_path_to(),
|
|
]
|
|
|
|
if len(config.encoder) != 0:
|
|
ffmpeg_command.append("-c:a")
|
|
ffmpeg_command.append(config.encoder)
|
|
|
|
if len(config.container) != 0:
|
|
ffmpeg_command.append("-f")
|
|
ffmpeg_command.append(config.container)
|
|
|
|
if config.use_quality:
|
|
ffmpeg_command.append("-q:a")
|
|
ffmpeg_command.append(config.quality)
|
|
elif config.use_bitrate:
|
|
ffmpeg_command.append("-b:a")
|
|
ffmpeg_command.append(config.bitrate)
|
|
else:
|
|
pass
|
|
|
|
# Add Metadata
|
|
ffmpeg_command.append("-metadata")
|
|
ffmpeg_command.append(f"title=\"{title}\"")
|
|
ffmpeg_command.append("-metadata")
|
|
ffmpeg_command.append(f"artist=\"{artist}\"")
|
|
|
|
ffmpeg_command.append(dest)
|
|
|
|
# TODO: check for errors
|
|
run_command(ffmpeg_command)
|