62 lines
1.5 KiB
Python
62 lines
1.5 KiB
Python
from pathlib import Path
|
|
from typing import Tuple, List, Optional
|
|
import subprocess
|
|
import json
|
|
|
|
def run(cmd):
|
|
"""
|
|
Run a subprocess command.
|
|
|
|
Args:
|
|
cmd: Command and arguments as a list.
|
|
|
|
Raises:
|
|
subprocess.CalledProcessError: If the command exits with a non-zero status.
|
|
"""
|
|
try:
|
|
result = subprocess.run(
|
|
cmd,
|
|
check=True,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
text=True
|
|
)
|
|
return result
|
|
except subprocess.CalledProcessError as e:
|
|
print(e.stdout)
|
|
raise
|
|
|
|
|
|
def _get_chapters(file: Path) -> List[Tuple[float, float]]:
|
|
"""Return list of (start_time, duration) for each chapter."""
|
|
cmd = [
|
|
"ffprobe",
|
|
"-i", str(file),
|
|
"-print_format", "json",
|
|
"-show_chapters",
|
|
"-loglevel", "error"
|
|
]
|
|
|
|
result = run(cmd)
|
|
data = json.loads(result.stdout)
|
|
|
|
chapters = []
|
|
for ch in data.get("chapters", []):
|
|
start = float(ch["start_time"])
|
|
end = float(ch["end_time"])
|
|
duration = end - start
|
|
chapters.append((start, duration))
|
|
|
|
return chapters
|
|
|
|
if __name__ == '__main__':
|
|
file = "/home/honney/projects/mkv/output_dir/08.mkv"
|
|
chapters = _get_chapters(Path(file))
|
|
time = 90
|
|
min_len = time / 2
|
|
max_len = time * 2
|
|
filtered = [
|
|
(start, dur) for start, dur in chapters
|
|
if min_len <= dur <= max_len
|
|
]
|
|
print(filtered) |