156 lines
4.3 KiB
Python
156 lines
4.3 KiB
Python
from ..transcode_presets import preset_transcode_presets
|
|
from ..types import File
|
|
from ..meta import ffmpeg_path
|
|
|
|
from yaml import load as load_yaml_file
|
|
from yaml import Loader as YamlLoader
|
|
from subprocess import run as run_command
|
|
|
|
|
|
class TranscodeConfig:
|
|
use_quality = False
|
|
use_bitrate = False
|
|
encoder = ""
|
|
file_extension = ""
|
|
container = ""
|
|
bitrate = ""
|
|
quality = ""
|
|
sample_rate = ""
|
|
channels = ""
|
|
|
|
def load_from_file(self, file):
|
|
self.load_from_dict(
|
|
load_yaml_file(
|
|
file, Loader=YamlLoader))
|
|
|
|
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"]
|
|
if "sample_rate" in data:
|
|
self.sample_rate = data["sample_rate"]
|
|
if "channels" in data:
|
|
self.channels = data["channels"]
|
|
return self
|
|
|
|
|
|
def get_transcode_config(preset: str):
|
|
conf = TranscodeConfig()
|
|
if preset in preset_transcode_presets.keys():
|
|
preset = preset_transcode_presets[preset]
|
|
|
|
if preset.startswith("g726-") and preset.endswith("k"):
|
|
conf.load_from_dict({
|
|
"container": "matroska",
|
|
"file_extension": "mka",
|
|
"encoder": "g726",
|
|
"sample_rate": "8000",
|
|
"channels": "1",
|
|
"use_bitrate": True,
|
|
"bitrate": preset.replace("g726-", "")
|
|
})
|
|
return conf
|
|
|
|
if preset.startswith("opus-") and preset.endswith("k"):
|
|
conf.load_from_dict({
|
|
"container": "ogg",
|
|
"file_extension": "opus",
|
|
"encoder": "libopus",
|
|
"use_bitrate": True,
|
|
"bitrate": preset.replace("opus-", "")
|
|
})
|
|
return conf
|
|
|
|
if preset.startswith("mp3-"):
|
|
conf.load_from_dict({
|
|
"container": "mp3",
|
|
"file_extension": "mp3",
|
|
"encoder": "libmp3lame",
|
|
})
|
|
|
|
if preset.startswith("mp3-v"):
|
|
conf.use_quality = True
|
|
conf.quality = preset.replace("mp3-v", "")
|
|
return conf
|
|
elif preset.startswith("mp3-") and preset.endswith("k"):
|
|
conf.use_bitrate = True
|
|
conf.bitrate = preset.replace("mp3-", "")
|
|
return conf
|
|
|
|
if preset.startswith("vorbis-q"):
|
|
conf.load_from_dict({
|
|
"container": "ogg",
|
|
"file_extension": "ogg",
|
|
"encoder": "libvorbis",
|
|
"use_quality": True,
|
|
"quality": preset.replace("vorbis-q", ""),
|
|
})
|
|
return conf
|
|
|
|
if preset.startswith("speex-q"):
|
|
conf.load_from_dict({
|
|
"container": "ogg",
|
|
"file_extension": "ogg",
|
|
"encoder": "libspeex",
|
|
"use_quality": True,
|
|
"quality": preset.replace("speex-q", ""),
|
|
})
|
|
return conf
|
|
|
|
print("Unknown Level")
|
|
exit()
|
|
|
|
|
|
def transcode(
|
|
file: File,
|
|
config: TranscodeConfig,
|
|
dest: str):
|
|
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 len(config.sample_rate) != 0:
|
|
ffmpeg_command.append("-ar")
|
|
ffmpeg_command.append(config.sample_rate)
|
|
|
|
if len(config.channels) != 0:
|
|
ffmpeg_command.append("-ac")
|
|
ffmpeg_command.append(config.channels)
|
|
|
|
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
|
|
|
|
ffmpeg_command.append(dest)
|
|
print(ffmpeg_command)
|
|
# TODO: check for errors
|
|
run_command(ffmpeg_command)
|