nixfiles/musicutil/utils/transcoder.py
ChaotiCryptidz 6149d05684 trans coder
2022-02-06 09:57:47 +00:00

144 lines
4.1 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 = ""
sample_rate = ""
channels = ""
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(file: File, level: str):
conf = TranscodeConfig()
if level in preset_transcode_levels.keys():
level = preset_transcode_levels["level"]
if level.startswith("g726-") and level.endswith("k"):
conf.load_from_dict({
"container": "matroska",
"file_extension": "mka",
"encoder": "g726",
"sample_rate": "8000",
"channels": "1",
"use_bitrate": True,
"bitrate": level.replace("g726-", "")
})
return conf
if level.startswith("opus-") and level.endswith("k"):
conf.load_from_dict({
"container": "ogg",
"file_extension": "opus",
"encoder": "libopus",
"use_bitrate": True,
"bitrate": level.replace("opus-", "")
})
return conf
if level.startswith("mp3-"):
conf.load_from_dict({
"container": "mp3",
"file_extension": "mp3",
"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.load_from_dict({
"container": "ogg",
"file_extension": "ogg",
"encoder": "libvorbis",
"use_quality": True,
"quality": level.replace("vorbis-q", ""),
})
return conf
if level.startswith("speex-q"):
conf.load_from_dict({
"container": "ogg",
"file_extension": "ogg",
"encoder": "libspeex",
"use_quality": True,
"quality": level.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)