2023-04-05 14:11:30 +00:00
|
|
|
import ffmpeg
|
|
|
|
|
2023-04-05 15:40:22 +00:00
|
|
|
from ..utils.shared import BITRATE, AUDIO_FORMAT, CODEX_LOGGER as LOGGER
|
2023-04-05 14:11:30 +00:00
|
|
|
from ..objects import Target
|
|
|
|
|
2023-04-05 15:40:22 +00:00
|
|
|
|
|
|
|
def correct_codec(target: Target, bitrate_kb: int = BITRATE, audio_format: str = AUDIO_FORMAT):
|
2023-04-05 14:11:30 +00:00
|
|
|
if not target.exists:
|
|
|
|
LOGGER.warning(f"Target doesn't exist: {target.file_path}")
|
2023-04-05 15:40:22 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
bitrate_b = int(bitrate_kb / 1024)
|
|
|
|
|
|
|
|
output_target = Target(
|
|
|
|
path=target._path,
|
|
|
|
file=str(target._file) + "." + audio_format
|
2023-04-05 14:11:30 +00:00
|
|
|
)
|
2023-04-05 15:40:22 +00:00
|
|
|
|
|
|
|
stream = ffmpeg.input(target.file_path)
|
|
|
|
stream = stream.audio
|
|
|
|
stream = ffmpeg.output(
|
|
|
|
stream,
|
|
|
|
str(output_target.file_path),
|
|
|
|
audio_bitrate=bitrate_b,
|
|
|
|
format=audio_format
|
|
|
|
)
|
|
|
|
out, err = ffmpeg.run(stream, quiet=True, overwrite_output=True)
|
|
|
|
if err != "":
|
|
|
|
LOGGER.debug(err)
|
|
|
|
|
|
|
|
output_target.copy_content(target)
|
|
|
|
output_target.file_path.unlink()
|