refactor(reframe): clean up the v1 fallback loop and get_video_resolution
The v1 frame loop has been the rarely-taken fallback since the v2 engine shipped in July; give it the same shape as the rest of the file while it sticks around as the REFRAME_ENGINE=v1 rollback: - get_video_resolution releases its capture in a finally block - staging files named .v1video/.v1audio next to the final output, cleaned with one loop instead of three inline conditionals - the rawvideo pipe spells out demuxer options (-video_size/-framerate/ pipe:0) instead of the short aliases, and drops the redundant -vcodec - encoder/reader names instead of ffmpeg_process/cap; single -c copy mux command built by list concatenation - dead script_start_time assignment removed Behavior is unchanged: verified end to end with REFRAME_ENGINE=v1 on a 6s clip (180 frames, 1080x1920 h264+aac, faststart, scrubbed metadata).
This commit is contained in:
parent
f8bc461c8e
commit
d83e0dddbf
1 changed files with 54 additions and 66 deletions
120
main.py
120
main.py
|
|
@ -565,13 +565,14 @@ def detect_scenes(video_path):
|
||||||
return scene_detection.detect_scenes(video_path)
|
return scene_detection.detect_scenes(video_path)
|
||||||
|
|
||||||
def get_video_resolution(video_path):
|
def get_video_resolution(video_path):
|
||||||
cap = cv2.VideoCapture(video_path)
|
probe = cv2.VideoCapture(video_path)
|
||||||
if not cap.isOpened():
|
try:
|
||||||
raise IOError(f"Could not open video file {video_path}")
|
if not probe.isOpened():
|
||||||
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
raise IOError(f"cannot open video: {video_path}")
|
||||||
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
return (int(probe.get(cv2.CAP_PROP_FRAME_WIDTH)),
|
||||||
cap.release()
|
int(probe.get(cv2.CAP_PROP_FRAME_HEIGHT)))
|
||||||
return width, height
|
finally:
|
||||||
|
probe.release()
|
||||||
|
|
||||||
|
|
||||||
# Byte budget for the sanitized video title used as the stem of every derived
|
# Byte budget for the sanitized video title used as the stem of every derived
|
||||||
|
|
@ -1053,8 +1054,6 @@ def process_video_to_vertical(input_video, final_output_video, aspect_ratio=ASPE
|
||||||
engine only — the v1 loop below has no layout concept beyond its own
|
engine only — the v1 loop below has no layout concept beyond its own
|
||||||
classifier).
|
classifier).
|
||||||
"""
|
"""
|
||||||
script_start_time = time.time()
|
|
||||||
|
|
||||||
# v2 engine: analyze downscaled, render natively in ffmpeg. Any failure
|
# v2 engine: analyze downscaled, render natively in ffmpeg. Any failure
|
||||||
# falls back to the v1 frame loop below so a v2 edge case can't kill jobs.
|
# falls back to the v1 frame loop below so a v2 edge case can't kill jobs.
|
||||||
if os.environ.get("REFRAME_ENGINE", "v2").strip().lower() != "v1":
|
if os.environ.get("REFRAME_ENGINE", "v2").strip().lower() != "v1":
|
||||||
|
|
@ -1078,28 +1077,27 @@ def process_video_to_vertical(input_video, final_output_video, aspect_ratio=ASPE
|
||||||
print(f" ⚠️ Reframe v2 failed ({type(e).__name__}: {e}) — "
|
print(f" ⚠️ Reframe v2 failed ({type(e).__name__}: {e}) — "
|
||||||
f"falling back to v1 frame loop")
|
f"falling back to v1 frame loop")
|
||||||
|
|
||||||
# Define temporary file paths based on the output name
|
# The v1 loop stages its work next to the final file: a silent video track
|
||||||
base_name = os.path.splitext(final_output_video)[0]
|
# first, then the source audio, muxed together at the end.
|
||||||
temp_video_output = f"{base_name}_temp_video.mp4"
|
stem = os.path.splitext(final_output_video)[0]
|
||||||
temp_audio_output = f"{base_name}_temp_audio.aac"
|
silent_video_path = stem + ".v1video.mp4"
|
||||||
|
audio_track_path = stem + ".v1audio.aac"
|
||||||
# Clean up previous temp files if they exist
|
for stale in (silent_video_path, audio_track_path, final_output_video):
|
||||||
if os.path.exists(temp_video_output): os.remove(temp_video_output)
|
if os.path.exists(stale):
|
||||||
if os.path.exists(temp_audio_output): os.remove(temp_audio_output)
|
os.remove(stale)
|
||||||
if os.path.exists(final_output_video): os.remove(final_output_video)
|
|
||||||
|
|
||||||
print(f"🎬 Processing clip: {input_video}")
|
print(f"🎬 Processing clip: {input_video}")
|
||||||
print(" Step 1: Detecting scenes...")
|
print(" Step 1: Detecting scenes...")
|
||||||
scenes, fps = detect_scenes(input_video)
|
scenes, fps = detect_scenes(input_video)
|
||||||
|
|
||||||
if not scenes:
|
if not scenes:
|
||||||
|
# Scene detection found nothing: treat the whole video as one scene.
|
||||||
print(" ❌ No scenes were detected. Using full video as one scene.")
|
print(" ❌ No scenes were detected. Using full video as one scene.")
|
||||||
# If scene detection fails or finds nothing, treat whole video as one scene
|
probe = cv2.VideoCapture(input_video)
|
||||||
cap = cv2.VideoCapture(input_video)
|
span = int(probe.get(cv2.CAP_PROP_FRAME_COUNT))
|
||||||
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
probe.release()
|
||||||
cap.release()
|
|
||||||
from scenedetect import FrameTimecode
|
from scenedetect import FrameTimecode
|
||||||
scenes = [(FrameTimecode(0, fps), FrameTimecode(total_frames, fps))]
|
scenes = [(FrameTimecode(0, fps), FrameTimecode(span, fps))]
|
||||||
|
|
||||||
print(f" ✅ Found {len(scenes)} scenes.")
|
print(f" ✅ Found {len(scenes)} scenes.")
|
||||||
|
|
||||||
|
|
@ -1123,17 +1121,18 @@ def process_video_to_vertical(input_video, final_output_video, aspect_ratio=ASPE
|
||||||
|
|
||||||
print("\n ✂️ Step 4: Processing video frames...")
|
print("\n ✂️ Step 4: Processing video frames...")
|
||||||
|
|
||||||
command = [
|
# Raw BGR frames stream down a pipe into ffmpeg, which encodes the silent
|
||||||
'ffmpeg', '-y', '-f', 'rawvideo', '-vcodec', 'rawvideo',
|
# video track; the audio is muxed back in afterwards.
|
||||||
'-s', f'{OUTPUT_WIDTH}x{OUTPUT_HEIGHT}', '-pix_fmt', 'bgr24',
|
encoder = subprocess.Popen(
|
||||||
'-r', str(fps), '-i', '-',
|
['ffmpeg', '-y',
|
||||||
*video_encode_args(QUALITY_FAST), '-an', temp_video_output
|
'-f', 'rawvideo', '-pix_fmt', 'bgr24',
|
||||||
]
|
'-video_size', f'{OUTPUT_WIDTH}x{OUTPUT_HEIGHT}',
|
||||||
|
'-framerate', str(fps), '-i', 'pipe:0',
|
||||||
|
*video_encode_args(QUALITY_FAST), '-an', silent_video_path],
|
||||||
|
stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
|
||||||
|
|
||||||
ffmpeg_process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
|
reader = cv2.VideoCapture(input_video)
|
||||||
|
frame_total = int(reader.get(cv2.CAP_PROP_FRAME_COUNT))
|
||||||
cap = cv2.VideoCapture(input_video)
|
|
||||||
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
||||||
|
|
||||||
frame_number = 0
|
frame_number = 0
|
||||||
current_scene_index = 0
|
current_scene_index = 0
|
||||||
|
|
@ -1150,9 +1149,9 @@ def process_video_to_vertical(input_video, final_output_video, aspect_ratio=ASPE
|
||||||
stage_seconds = {'detect': 0.0, 'write': 0.0}
|
stage_seconds = {'detect': 0.0, 'write': 0.0}
|
||||||
loop_started = time.time()
|
loop_started = time.time()
|
||||||
|
|
||||||
with tqdm(total=total_frames, desc=" Processing", file=sys.stdout) as pbar:
|
with tqdm(total=frame_total, desc=" Processing", file=sys.stdout) as pbar:
|
||||||
while cap.isOpened():
|
while reader.isOpened():
|
||||||
ret, frame = cap.read()
|
ret, frame = reader.read()
|
||||||
if not ret:
|
if not ret:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
@ -1204,7 +1203,7 @@ def process_video_to_vertical(input_video, final_output_video, aspect_ratio=ASPE
|
||||||
output_frame = cv2.resize(frame, (OUTPUT_WIDTH, OUTPUT_HEIGHT), interpolation=cv2.INTER_LINEAR)
|
output_frame = cv2.resize(frame, (OUTPUT_WIDTH, OUTPUT_HEIGHT), interpolation=cv2.INTER_LINEAR)
|
||||||
|
|
||||||
t_wr = time.time()
|
t_wr = time.time()
|
||||||
ffmpeg_process.stdin.write(output_frame.tobytes())
|
encoder.stdin.write(output_frame.tobytes())
|
||||||
stage_seconds['write'] += time.time() - t_wr
|
stage_seconds['write'] += time.time() - t_wr
|
||||||
frame_number += 1
|
frame_number += 1
|
||||||
pbar.update(1)
|
pbar.update(1)
|
||||||
|
|
@ -1216,52 +1215,41 @@ def process_video_to_vertical(input_video, final_output_video, aspect_ratio=ASPE
|
||||||
f"encode-wait {stage_seconds['write']:.1f}s, "
|
f"encode-wait {stage_seconds['write']:.1f}s, "
|
||||||
f"decode+render {other:.1f}s ({frame_number} frames)")
|
f"decode+render {other:.1f}s ({frame_number} frames)")
|
||||||
|
|
||||||
ffmpeg_process.stdin.close()
|
encoder.stdin.close()
|
||||||
stderr_output = ffmpeg_process.stderr.read().decode()
|
encode_log = encoder.stderr.read().decode()
|
||||||
ffmpeg_process.wait()
|
encoder.wait()
|
||||||
cap.release()
|
reader.release()
|
||||||
|
|
||||||
if ffmpeg_process.returncode != 0:
|
if encoder.returncode != 0:
|
||||||
print("\n ❌ FFmpeg frame processing failed.")
|
print("\n ❌ FFmpeg frame processing failed.")
|
||||||
print(" Stderr:", stderr_output)
|
print(" Stderr:", encode_log)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
print("\n 🔊 Step 5: Extracting audio...")
|
print("\n 🔊 Step 5: Extracting audio...")
|
||||||
audio_extract_command = [
|
|
||||||
'ffmpeg', '-y', '-i', input_video, '-vn', '-acodec', 'copy', temp_audio_output
|
|
||||||
]
|
|
||||||
try:
|
try:
|
||||||
subprocess.run(audio_extract_command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
|
subprocess.run(
|
||||||
|
['ffmpeg', '-y', '-i', input_video, '-vn', '-c:a', 'copy', audio_track_path],
|
||||||
|
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
print("\n ❌ Audio extraction failed (maybe no audio?). Proceeding without audio.")
|
print("\n ❌ Audio extraction failed (maybe no audio?). Proceeding without audio.")
|
||||||
pass
|
|
||||||
|
|
||||||
print("\n ✨ Step 6: Merging...")
|
print("\n ✨ Step 6: Merging...")
|
||||||
if os.path.exists(temp_audio_output):
|
mux = ['ffmpeg', '-y', '-i', silent_video_path]
|
||||||
merge_command = [
|
if os.path.exists(audio_track_path):
|
||||||
'ffmpeg', '-y', '-i', temp_video_output, '-i', temp_audio_output,
|
mux += ['-i', audio_track_path]
|
||||||
'-c:v', 'copy', '-c:a', 'copy', *METADATA_SCRUB,
|
mux += ['-c', 'copy', *METADATA_SCRUB, '-movflags', '+faststart', final_output_video]
|
||||||
'-movflags', '+faststart', final_output_video
|
|
||||||
]
|
|
||||||
else:
|
|
||||||
merge_command = [
|
|
||||||
'ffmpeg', '-y', '-i', temp_video_output,
|
|
||||||
'-c:v', 'copy', *METADATA_SCRUB,
|
|
||||||
'-movflags', '+faststart', final_output_video
|
|
||||||
]
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
subprocess.run(merge_command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
|
subprocess.run(mux, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
|
||||||
print(f" ✅ Clip saved to {final_output_video}")
|
print(f" ✅ Clip saved to {final_output_video}")
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print("\n ❌ Final merge failed.")
|
print("\n ❌ Final merge failed.")
|
||||||
print(" Stderr:", e.stderr.decode())
|
print(" Stderr:", e.stderr.decode())
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Clean up temp files
|
for leftover in (silent_video_path, audio_track_path):
|
||||||
if os.path.exists(temp_video_output): os.remove(temp_video_output)
|
if os.path.exists(leftover):
|
||||||
if os.path.exists(temp_audio_output): os.remove(temp_audio_output)
|
os.remove(leftover)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def transcribe_video(video_path):
|
def transcribe_video(video_path):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue