| name | run2_ffmpeg_robust_extraction |
| description | Robust FFmpeg video frame extraction with error handling, alternative methods, and comprehensive frame selection strategies. |
Robust FFmpeg Frame Extraction
Overview
Extract frames from video files using FFmpeg with multiple extraction strategies, comprehensive error handling, and verification.
Installation & Verification
ffmpeg -version
Frame Extraction Strategies
Strategy 1: Key Frame Only (I-Frames) - Best for Edits
Extracts only I-frames (key frames that contain complete frame data):
ffmpeg -i input.mp4 -vf "select='eq(pict_type,I)'" -vsync 0 output_%03d.png
- Best for: Videos with clear scene changes
- Pros: Minimal output files, lowest disk usage
- Cons: May miss fast-moving objects between keyframes
Strategy 2: Fixed Frame Rate - Best for Smooth Motion
Extract frames at a fixed rate (e.g., 1 FPS, 5 FPS):
ffmpeg -i input.mp4 -vf fps=1 output_%03d.png
- Best for: Consistent time-based sampling
- Pros: Regular time intervals, predictable output count
- Cons: May be too many frames for long videos
Strategy 3: Every Nth Frame
Extract every Nth frame from the video:
ffmpeg -i input.mp4 -vf "select='isnan(prev_selected_t)+gte(t\-prev_selected_t,2)'" -vsync 0 output_%03d.png
- Best for: Uniform frame sampling
- Pros: Captures motion details
- Cons: Requires calculation for desired interval
Python Wrapper with Error Handling
import subprocess
import os
import glob
def extract_frames(input_video, output_pattern, method='keyframe'):
"""
Extract frames from video with error handling
Args:
input_video: Path to input video file
output_pattern: Output path pattern (e.g., '/root/frames_%03d.png')
method: 'keyframe' (I-frames only) or 'fps1' (1 frame per second)
Returns:
List of extracted frame paths, or None on error
"""
if not os.path.exists(input_video):
print(f"Error: Video file not found: {input_video}")
return None
if method == 'keyframe':
vf = "select='eq(pict_type,I)'"
vsync = "-vsync 0"
elif method == 'fps1':
vf = "fps=1"
vsync = ""
else:
print(f"Error: Unknown method: {method}")
return None
cmd = f'ffmpeg -i "{input_video}" -vf "{vf}" {vsync} "{output_pattern}" 2>&1'
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if result.returncode != 0:
()
output_dir = os.path.dirname(output_pattern)
output_prefix = os.path.basename(output_pattern).replace(, )
frames = (glob.glob(os.path.join(output_dir, output_prefix.replace(, )+)))
frames:
()
()
frames
frames = extract_frames(, , method=)
Video Information Query
Get video details before extraction:
ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate,duration -of default=noprint_wrappers=1 input.mp4
Best Practices
- Always validate input file before running FFmpeg
- Check available disk space - frames require significant storage
- Verify output files after extraction completes
- Use absolute paths to avoid working directory issues
- Quote file paths with spaces properly