feat: implement flexible head file detection supporting i.mp4 and i<number>.mp4 patterns

This commit is contained in:
tigerenwork 2026-04-13 23:25:35 +08:00
parent dbbaff126c
commit 46a0bad3c3
1 changed files with 29 additions and 7 deletions

View File

@ -3,6 +3,7 @@ import json
import os import os
import glob import glob
import sys import sys
import re
def probe_streams(video_path): def probe_streams(video_path):
"""Probes the input file and returns info about its streams.""" """Probes the input file and returns info about its streams."""
@ -177,16 +178,37 @@ def main():
if target_files is None: if target_files is None:
target_files = glob.glob("*.mp4") 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 os.path.exists("i.mp4"):
if not os.path.exists(baseline_file): baseline_file = "i.mp4"
print(f"Error: '{baseline_file}' not found in the target folder: {target_dir}") 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 return
# Remove the baseline file from the processing list # Remove all head-pattern files from the processing list
if baseline_file in target_files: target_files = [f for f in target_files if f not in head_files]
target_files.remove(baseline_file)
if not target_files: if not target_files:
print("No other .mp4 files found to process.") print("No other .mp4 files found to process.")