2023-06-14 10:38:36 +00:00
|
|
|
from typing import List, Tuple
|
2023-04-05 14:11:30 +00:00
|
|
|
import ffmpeg
|
2023-06-14 13:25:28 +00:00
|
|
|
import subprocess
|
2023-04-05 14:11:30 +00:00
|
|
|
|
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
|
|
|
|
2023-06-14 10:38:36 +00:00
|
|
|
def remove_intervals(target: Target, interval_list: List[Tuple[float, float]]):
|
|
|
|
if not target.exists:
|
|
|
|
LOGGER.warning(f"Target doesn't exist: {target.file_path}")
|
|
|
|
return
|
2023-06-14 15:43:20 +00:00
|
|
|
|
|
|
|
output_target = Target(
|
2023-06-14 13:25:28 +00:00
|
|
|
path=target._path,
|
2023-06-14 15:43:20 +00:00
|
|
|
file=str(target._file) + "." + AUDIO_FORMAT
|
2023-06-14 13:25:28 +00:00
|
|
|
)
|
|
|
|
|
2023-06-14 10:38:36 +00:00
|
|
|
# https://stackoverflow.com/questions/50594412/cut-multiple-parts-of-a-video-with-ffmpeg
|
|
|
|
aselect_list: List[str] = []
|
|
|
|
|
|
|
|
start = 0
|
2023-06-14 15:43:20 +00:00
|
|
|
next_start = 0
|
2023-06-14 10:38:36 +00:00
|
|
|
for end, next_start in interval_list:
|
|
|
|
aselect_list.append(f"between(t,{start},{end})")
|
|
|
|
|
|
|
|
start = next_start
|
|
|
|
|
|
|
|
aselect_list.append(f"gte(t,{next_start})")
|
|
|
|
|
|
|
|
select = f"aselect='{'+'.join(aselect_list)}',asetpts=N/SR/TB"
|
2023-06-14 15:43:20 +00:00
|
|
|
|
|
|
|
r = subprocess.run(["ffmpeg", "-i", str(target.file_path), "-af", select, str(output_target.file_path)])
|
2023-06-14 13:25:28 +00:00
|
|
|
if r.stderr != "":
|
|
|
|
LOGGER.debug(r.stderr)
|
2023-06-14 15:43:20 +00:00
|
|
|
|
|
|
|
output_target.copy_content(target)
|
|
|
|
output_target.delete()
|
2023-06-14 10:38:36 +00:00
|
|
|
|
|
|
|
|
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)
|
2023-06-14 13:25:28 +00:00
|
|
|
output_target.delete()
|