feat: implement flexible head file detection supporting i.mp4 and i<number>.mp4 patterns
This commit is contained in:
parent
dbbaff126c
commit
46a0bad3c3
36
cut_head.py
36
cut_head.py
|
|
@ -3,6 +3,7 @@ import json
|
|||
import os
|
||||
import glob
|
||||
import sys
|
||||
import re
|
||||
|
||||
def probe_streams(video_path):
|
||||
"""Probes the input file and returns info about its streams."""
|
||||
|
|
@ -177,16 +178,37 @@ def main():
|
|||
if target_files is None:
|
||||
target_files = glob.glob("*.mp4")
|
||||
|
||||
baseline_file = "i.mp4"
|
||||
# --- Head file detection ---
|
||||
# Priority 1: exact "i.mp4"
|
||||
# Priority 2: files matching "i<number>.mp4" (e.g. i10.mp4, i9.mp4)
|
||||
baseline_file = None
|
||||
head_pattern = re.compile(r'^i(\d+)\.mp4$', re.IGNORECASE)
|
||||
head_files = [] # all files that match head patterns (to exclude from processing)
|
||||
|
||||
# Check if baseline exists in the target directory
|
||||
if not os.path.exists(baseline_file):
|
||||
print(f"Error: '{baseline_file}' not found in the target folder: {target_dir}")
|
||||
if os.path.exists("i.mp4"):
|
||||
baseline_file = "i.mp4"
|
||||
head_files.append("i.mp4")
|
||||
print("Using head file: i.mp4")
|
||||
else:
|
||||
# Find all i<number>.mp4 candidates
|
||||
candidates = []
|
||||
for f in glob.glob("i*.mp4"):
|
||||
m = head_pattern.match(f)
|
||||
if m:
|
||||
candidates.append((int(m.group(1)), f))
|
||||
head_files.append(f)
|
||||
if candidates:
|
||||
# Sort by the numeric suffix and pick the first (smallest number)
|
||||
candidates.sort(key=lambda x: x[0])
|
||||
baseline_file = candidates[0][1]
|
||||
print(f"Using head file (fallback): {baseline_file}")
|
||||
|
||||
if not baseline_file:
|
||||
print(f"Error: No head file found (i.mp4 or i<number>.mp4) in: {target_dir}")
|
||||
return
|
||||
|
||||
# Remove the baseline file from the processing list
|
||||
if baseline_file in target_files:
|
||||
target_files.remove(baseline_file)
|
||||
# Remove all head-pattern files from the processing list
|
||||
target_files = [f for f in target_files if f not in head_files]
|
||||
|
||||
if not target_files:
|
||||
print("No other .mp4 files found to process.")
|
||||
|
|
|
|||
Loading…
Reference in New Issue