Files
Kronic-Krusaders/transcode_planner.py
2026-11-25 20:20:27 -06:00

51 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from dataclasses import dataclass
import re
@dataclass
class TranscodePlan:
input_path: str
video_codec: str
audio_codec: str
target_container: str = "mp4"
# Example rules:
# - If not H.264 or not AAC → transcode
# - If resolution > 1080p → downscale
def plan_transcodes(media_items):
plans = []
for item in media_items:
needs_transcode = False
if item.video_codec != "AVC": # pymediainfo may report H.264 as AVC
needs_transcode = True
if item.audio_codec != "AAC":
needs_transcode = True
if item.width and item.width > 1920:
needs_transcode = True
if needs_transcode:
print(f"Planning transcode for: {item.path}")
plans.append(
TranscodePlan(
input_path=str(item.path),
video_codec="h264",
audio_codec="aac"
)
)
return plans
def sanitize_filename(name: str) -> str:
"""
Make filename filesystem-safe and add _dup before extension.
"""
name = name.replace(":", "-").replace("@", "-").replace("–", "-")
name = re.sub(r"\s+", " ", name).strip()
if "." in name:
base, ext = name.rsplit(".", 1)
return f"{base}_dup.{ext}"
else:
return f"{name}_dup"