91 lines
2.5 KiB
Python
91 lines
2.5 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 = ""
|
||
|
bitrate = ""
|
||
|
quality = ""
|
||
|
|
||
|
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.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-v"):
|
||
|
conf.file_extension = "mp3"
|
||
|
conf.encoder = "libmp3lame"
|
||
|
conf.use_quality = True
|
||
|
conf.quality = level.replace("mp3-v", "")
|
||
|
return conf
|
||
|
elif level.startswith("mp3-") and level.endswith("k"):
|
||
|
conf.file_extension = "mp3"
|
||
|
conf.encoder = "libmp3lame"
|
||
|
conf.use_bitrate = True
|
||
|
# includes the k
|
||
|
conf.bitrate = level.replace("mp3-", "")
|
||
|
return conf
|
||
|
|
||
|
if level.startswith("vorbis-q"):
|
||
|
conf.file_extension = "ogg"
|
||
|
conf.encoder = "libvorbis"
|
||
|
conf.use_quality = True
|
||
|
conf.quality = level.replace("vorbis-q", "")
|
||
|
return conf
|
||
|
|
||
|
if level == "speex":
|
||
|
conf.encoder = "libspeex"
|
||
|
conf.file_extension = "ogg"
|
||
|
return conf
|
||
|
|
||
|
print("Unknown Level")
|
||
|
exit()
|
||
|
|
||
|
def transcode(file: File, config: TranscodeConfig, level: str, dest: str):
|
||
|
title = file.tags.title
|
||
|
artist = file.tags.artist
|
||
|
|
||
|
ffmpeg_command = [
|
||
|
ffmpeg_path,
|
||
|
"-y",
|
||
|
"-hide_banner",
|
||
|
"-loglevel", "warning",
|
||
|
"-i", file.join_path_to(),
|
||
|
]
|
||
|
|
||
|
ffmpeg_command.append("-c:a")
|
||
|
ffmpeg_command.append(config.encoder)
|
||
|
|
||
|
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)
|