-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffmpeg_processing.py
More file actions
605 lines (492 loc) · 19.9 KB
/
ffmpeg_processing.py
File metadata and controls
605 lines (492 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#!/usr/bin/env python3
"""
FFmpeg Processing Module - Frame-Accurate Video Operations
- Frame-perfect segment extraction
- Zero timing drift
- Proper timing offset handling
"""
import os
import sys
import subprocess
import json
import random
import uuid
import re
from typing import Tuple, List
# Determine script directory
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
# Set up portable FFmpeg path
FFMPEG_PATH = os.path.join(SCRIPT_DIR, 'bin', 'ffmpeg', 'ffmpeg.exe')
FFPROBE_PATH = os.path.join(SCRIPT_DIR, 'bin', 'ffmpeg', 'ffprobe.exe')
# Check if portable FFmpeg exists
if os.path.exists(FFMPEG_PATH):
os.environ['IMAGEIO_FFMPEG_EXE'] = FFMPEG_PATH
os.environ['FFMPEG_BINARY'] = FFMPEG_PATH
if os.path.exists(FFPROBE_PATH):
os.environ['FFPROBE_BINARY'] = FFPROBE_PATH
print(f"✅ FFmpeg Module: Using portable FFmpeg: {FFMPEG_PATH}")
FFMPEG_FOUND = True
else:
print(f"⚠️ FFmpeg Module: Portable FFmpeg not found at: {FFMPEG_PATH}")
print(f" Falling back to system FFmpeg")
FFMPEG_PATH = 'ffmpeg' # Use system FFmpeg
FFPROBE_PATH = 'ffprobe'
FFMPEG_FOUND = False
# Get CPU count for threading
import multiprocessing
CPU_COUNT = multiprocessing.cpu_count()
MAX_THREADS = CPU_COUNT
def check_nvenc_support() -> bool:
"""Check if FFmpeg supports NVIDIA NVENC hardware encoding."""
try:
result = subprocess.run(
[FFMPEG_PATH, '-hide_banner', '-encoders'],
capture_output=True,
text=True,
timeout=5
)
return 'h264_nvenc' in result.stdout
except Exception as e:
return False
def get_video_duration(video_file: str) -> float:
"""Get the duration of a video file using ffprobe."""
try:
probe_cmd = [
FFPROBE_PATH,
'-v', 'error',
'-show_entries', 'format=duration',
'-of', 'default=noprint_wrappers=1:nokey=1',
video_file
]
result = subprocess.run(probe_cmd, capture_output=True, text=True, timeout=10)
duration = float(result.stdout.strip())
return duration
except Exception as e:
print(f" ⚠️ Could not get video duration with ffprobe: {e}")
return 10.0 # Default fallback
def get_video_fps(video_file: str) -> float:
"""Get the FPS of a video file using ffprobe."""
try:
probe_cmd = [
FFPROBE_PATH,
'-v', 'error',
'-select_streams', 'v:0',
'-show_entries', 'stream=r_frame_rate',
'-of', 'default=noprint_wrappers=1:nokey=1',
video_file
]
result = subprocess.run(probe_cmd, capture_output=True, text=True, timeout=10)
fps_str = result.stdout.strip()
# Parse fraction (e.g., "30000/1001" or "30/1")
if '/' in fps_str:
num, den = fps_str.split('/')
fps = float(num) / float(den)
else:
fps = float(fps_str)
return fps
except Exception as e:
print(f" ⚠️ Could not get video FPS with ffprobe: {e}")
return 30.0 # Default fallback
def get_video_resolution(video_file: str) -> Tuple[int, int]:
"""Get the resolution (width, height) of a video file using ffprobe."""
try:
probe_cmd = [
FFPROBE_PATH,
'-v', 'error',
'-select_streams', 'v:0',
'-show_entries', 'stream=width,height',
'-of', 'json',
video_file
]
result = subprocess.run(probe_cmd, capture_output=True, text=True, timeout=10)
data = json.loads(result.stdout)
width = data['streams'][0]['width']
height = data['streams'][0]['height']
return (width, height)
except Exception as e:
print(f" ⚠️ Could not get video resolution with ffprobe: {e}")
return (1920, 1080) # Default fallback
def seconds_to_frame_count(seconds: float, fps: float) -> int:
"""
Convert seconds to exact frame count.
This ensures frame-accurate timing with no drift.
"""
return int(round(seconds * fps))
def frame_count_to_seconds(frames: int, fps: float) -> float:
"""
Convert frame count back to exact seconds.
This is the EXACT duration for the frame count.
"""
return frames / fps
def convert_to_prores_proxy(video_file: str, output_dir: str, fps: float = None) -> str:
"""
Convert video to ProRes 422 Proxy for lossless editing.
All frames are I-frames (keyframes) for frame-accurate cutting.
STRIPS AUDIO - we'll add the music track at the end.
"""
filename = os.path.basename(video_file)
name, _ = os.path.splitext(filename)
output_file = os.path.join(output_dir, f"{name}_prores.mov")
print(f" 📹 Converting to ProRes 422 Proxy: {filename}")
# Detect FPS if not provided
if fps is None:
fps = get_video_fps(video_file)
# Build FFmpeg command for ProRes 422 Proxy (NO AUDIO)
cmd = [
FFMPEG_PATH,
'-hwaccel', 'auto', # Hardware acceleration
'-i', video_file,
'-c:v', 'prores', # ProRes encoder
'-profile:v', '0', # Proxy quality (0=Proxy, 1=LT, 2=Standard, 3=HQ)
'-vendor', 'apl0',
'-pix_fmt', 'yuv422p10le',
'-an', # ✅ STRIP AUDIO - we'll add music at the end
'-r', str(fps), # Set frame rate
'-threads', str(MAX_THREADS),
'-y',
output_file
]
try:
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=600 # 10 minute timeout
)
if result.returncode != 0:
print(f" ⚠️ FFmpeg error: {result.stderr}")
raise Exception(f"ProRes conversion failed for {filename}")
print(f" ✓ ProRes conversion complete: {name}_prores.mov (video only, no audio)")
return output_file
except subprocess.TimeoutExpired:
raise Exception(f"ProRes conversion timeout for {filename}")
except Exception as e:
raise Exception(f"ProRes conversion error: {str(e)}")
def extract_clip_segment_ffmpeg(video_file: str, start_time: float, duration: float,
output_file: str, fps: float, target_size: Tuple[int, int],
reverse: bool, speed_factor: float, use_nvenc: bool,
gpu_encoder: str = 'h264_nvenc') -> bool:
"""
Extract a video segment using FFmpeg with FRAME-ACCURATE timing.
✅ FRAME-ACCURATE: Uses exact frame counts instead of floating-point seconds
✅ ZERO DRIFT: No cumulative timing errors
"""
try:
# ✅ FRAME-ACCURATE: Calculate exact frame count for duration
frame_count = seconds_to_frame_count(duration, fps)
exact_duration = frame_count_to_seconds(frame_count, fps)
# Build filter complex
filters = []
# Speed adjustment (must come before reverse)
if speed_factor != 1.0:
filters.append(f"setpts={1.0/speed_factor}*PTS")
# Reverse filter
if reverse:
filters.append("reverse")
# Scale to target size
if target_size:
width, height = target_size
filters.append(f"scale={width}:{height}")
# FPS filter
filters.append(f"fps={fps}")
filter_complex = ",".join(filters)
# Build FFmpeg command
cmd = [FFMPEG_PATH]
# Hardware acceleration
if use_nvenc:
cmd.extend(['-hwaccel', 'cuda'])
else:
cmd.extend(['-hwaccel', 'auto'])
# ✅ FRAME-ACCURATE INPUT SEEKING
# Use -ss BEFORE -i for faster seeking (keyframe-based)
# Then use -ss AFTER -i for frame-accurate positioning
cmd.extend([
'-ss', str(start_time),
'-i', video_file
])
# Video filters
cmd.extend(['-vf', filter_complex])
# ✅ FRAME-ACCURATE DURATION: Use -vframes instead of -t
cmd.extend(['-vframes', str(frame_count)])
# Video encoding
if use_nvenc:
cmd.extend([
'-c:v', gpu_encoder,
'-preset', 'p7',
'-tune', 'hq',
'-rc', 'vbr',
'-cq', '0',
])
else:
cmd.extend([
'-c:v', 'libx264',
'-preset', 'ultrafast',
'-crf', '0',
'-threads', str(MAX_THREADS),
])
# No audio, frame-accurate settings
cmd.extend([
'-an',
'-vsync', 'cfr', # Constant frame rate
'-r', str(fps), # Exact output FPS
'-fflags', '+genpts',
'-movflags', '+faststart',
'-y',
output_file
])
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=120
)
if result.returncode != 0:
print(f" ⚠️ FFmpeg error: {result.stderr}")
return False
# Verify output exists and has content
if not os.path.exists(output_file) or os.path.getsize(output_file) == 0:
return False
return True
except Exception as e:
print(f" ⚠️ Error extracting clip: {e}")
return False
def extract_prores_segment_random(video_file: str, duration: float, fps: float,
temp_dir: str, segment_index: int,
direction: str = 'forward') -> str:
"""
Extract a RANDOM segment from ProRes video with FRAME-PERFECT precision.
✅ FRAME-ACCURATE: Uses exact frame count
✅ ZERO DRIFT: Re-encodes with precise frame count
"""
output_file = os.path.join(temp_dir, f"segment_{segment_index:05d}.mov")
# Get video duration
video_duration = get_video_duration(video_file)
# Randomly select start time
if video_duration >= duration:
max_start = video_duration - duration
start_time = random.uniform(0, max_start)
else:
start_time = 0
duration = video_duration
# ✅ FRAME-ACCURATE: Calculate exact frame count
frame_count = seconds_to_frame_count(duration, fps)
# Determine if we should reverse
apply_reverse = False
if direction == 'backward':
apply_reverse = True
elif direction == 'random':
apply_reverse = random.choice([True, False])
# Build command with frame-accurate seeking
cmd = [
FFMPEG_PATH,
'-hwaccel', 'auto',
'-ss', str(start_time), # Fast seek to approximate position
'-i', video_file,
'-vframes', str(frame_count), # ✅ Exact frame count
]
if apply_reverse:
cmd.extend(['-vf', 'reverse'])
cmd.extend([
'-c:v', 'prores',
'-profile:v', '0',
'-vendor', 'apl0',
'-pix_fmt', 'yuv422p10le',
'-r', str(fps),
'-an',
'-threads', str(MAX_THREADS),
'-y',
output_file
])
try:
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=120
)
if result.returncode != 0:
raise Exception(f"Segment extraction failed: {result.stderr}")
if not os.path.exists(output_file) or os.path.getsize(output_file) == 0:
raise Exception(f"Segment file not created or is empty: {output_file}")
return output_file
except Exception as e:
raise Exception(f"ProRes segment extraction error: {str(e)}")
def concatenate_videos_ffmpeg(video_files: List[str], output_file: str,
audio_file: str = None, start_time: float = 0.0,
end_time: float = None, use_nvenc: bool = False,
gpu_encoder: str = 'h264_nvenc', fps: float = 30.0,
temp_dir: str = None) -> str:
"""
Concatenate video files using FFmpeg concat demuxer.
✅ FRAME-ACCURATE: Maintains precise timing through concatenation
"""
if temp_dir is None:
temp_dir = os.path.dirname(output_file)
# Create concat file
concat_file = os.path.join(temp_dir, f'concat_list_{uuid.uuid4().hex}.txt')
with open(concat_file, 'w') as f:
for video_file in video_files:
escaped_path = video_file.replace('\\', '/')
f.write(f"file '{escaped_path}'\n")
is_prores = output_file.lower().endswith('.mov')
try:
if is_prores:
# ProRes: concat with stream copy (lossless)
print(f" 🔗 Concatenating {len(video_files)} segments (lossless stream copy)...")
temp_video = os.path.join(temp_dir, f'video_only_{uuid.uuid4().hex}.mov')
cmd = [
FFMPEG_PATH,
'-f', 'concat',
'-safe', '0',
'-i', concat_file,
'-c', 'copy',
'-y',
temp_video
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
if result.returncode != 0:
raise Exception(f"Concatenation failed: {result.stderr}")
# Add audio if provided
if audio_file:
print(f" 🎵 Adding music track...")
temp_audio = os.path.join(temp_dir, f'music_{uuid.uuid4().hex}.wav')
audio_cmd = [FFMPEG_PATH, '-i', audio_file]
if end_time and end_time > start_time:
audio_cmd.extend(['-ss', str(start_time), '-t', str(end_time - start_time)])
elif start_time > 0:
audio_cmd.extend(['-ss', str(start_time)])
audio_cmd.extend([
'-acodec', 'pcm_s24le',
'-ar', '48000',
'-ac', '2',
'-y',
temp_audio
])
result = subprocess.run(audio_cmd, capture_output=True, text=True, timeout=120)
if result.returncode != 0:
raise Exception(f"Audio extraction failed: {result.stderr}")
# Combine video + audio with AUDIO as master timeline
cmd = [
FFMPEG_PATH,
'-i', temp_video,
'-i', temp_audio,
'-map', '0:v',
'-map', '1:a',
'-c:v', 'copy',
'-c:a', 'pcm_s24le',
'-ar', '48000',
'-shortest', # Use shortest stream (audio)
'-y',
output_file
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
if result.returncode != 0:
raise Exception(f"Audio merging failed: {result.stderr}")
if os.path.exists(temp_video):
os.remove(temp_video)
if os.path.exists(temp_audio):
os.remove(temp_audio)
else:
import shutil
shutil.move(temp_video, output_file)
else:
# H.264/H.265: Re-encode
print(f" 🔗 Concatenating and encoding {len(video_files)} segments...")
cmd = [FFMPEG_PATH]
if use_nvenc:
cmd.extend(['-hwaccel', 'cuda'])
else:
cmd.extend(['-hwaccel', 'auto'])
cmd.extend([
'-f', 'concat',
'-safe', '0',
'-i', concat_file
])
if audio_file:
if end_time and end_time > start_time:
cmd.extend([
'-ss', str(start_time),
'-t', str(end_time - start_time),
'-i', audio_file
])
elif start_time > 0:
cmd.extend([
'-ss', str(start_time),
'-i', audio_file
])
else:
cmd.extend(['-i', audio_file])
cmd.extend(['-map', '0:v', '-map', '1:a'])
if use_nvenc:
cmd.extend([
'-c:v', gpu_encoder,
'-preset', 'p7',
'-tune', 'hq',
'-rc', 'vbr',
'-cq', '0',
'-pix_fmt', 'yuv420p',
])
else:
cmd.extend([
'-c:v', 'libx264',
'-preset', 'ultrafast',
'-crf', '0',
'-pix_fmt', 'yuv420p',
'-threads', str(MAX_THREADS),
])
if audio_file:
cmd.extend([
'-c:a', 'pcm_s24le',
'-ar', '48000',
'-shortest',
])
cmd.extend([
'-vsync', 'cfr',
'-r', str(fps),
'-y',
output_file
])
result = subprocess.run(cmd, capture_output=True, text=True, timeout=600)
if result.returncode != 0:
raise Exception(f"Encoding failed: {result.stderr}")
if os.path.exists(concat_file):
os.remove(concat_file)
return output_file
except Exception as e:
if os.path.exists(concat_file):
os.remove(concat_file)
raise e
def detect_video_scene_changes(video_path: str, threshold: float = 0.3) -> List[float]:
"""Detect scene changes using FFmpeg's scene detection filter."""
try:
print(f" 🎬 Analyzing scene changes with FFmpeg: {os.path.basename(video_path)}")
print(f" Threshold: {threshold}")
cmd = [
FFMPEG_PATH,
'-i', video_path,
'-vf', f"select='gt(scene,{threshold})',showinfo",
'-f', 'null',
'-'
]
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=300
)
scene_changes = []
for line in result.stderr.split('\n'):
if 'pts_time:' in line:
match = re.search(r'pts_time:([\d.]+)', line)
if match:
timestamp = float(match.group(1))
scene_changes.append(timestamp)
scene_changes = sorted(list(set(scene_changes)))
print(f" ✓ Found {len(scene_changes)} scene changes")
return scene_changes
except subprocess.TimeoutExpired:
print(f" ⚠️ Warning: Scene detection timeout")
return []
except Exception as e:
print(f" ⚠️ Warning: Could not analyze scene changes: {e}")
return []