ffmpeg-opencv-integration

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

FFmpeg + OpenCV Integration Guide

FFmpeg + OpenCV 集成指南

Use this skill when FFmpeg handles video I/O and OpenCV handles image processing. This SKILL is a lean orchestrator; full pipe patterns, library examples, and Modal recipes are preserved in
references/opencv-pipelines-and-libraries.md
.
当FFmpeg负责视频I/O、OpenCV负责图像处理时,可使用本技能。本SKILL是一个精简的编排器;完整的管道模式、库示例和Modal 方案保存在
references/opencv-pipelines-and-libraries.md
中。

When to Use

使用场景

  • Decode with FFmpeg and process frames with OpenCV
  • Encode OpenCV-generated/processed frames with FFmpeg
  • Compare
    cv2.VideoCapture
    , subprocess pipes, PyAV, ffmpegcv, VidGear, and Decord
  • Fix RGB/BGR color bugs or
    (height, width)
    dimension-order bugs
  • Preserve audio while replacing processed video frames
  • Build GPU-assisted video I/O around CPU OpenCV processing
  • 用FFmpeg解码,用OpenCV处理帧
  • 用FFmpeg编码OpenCV生成/处理的帧
  • 对比
    cv2.VideoCapture
    、子进程管道、PyAV、ffmpegcv、VidGear和Decord
  • 修复RGB/BGR颜色错误或
    (height, width)
    维度顺序错误
  • 替换处理后的视频帧时保留音频
  • 围绕CPU端OpenCV处理构建GPU辅助的视频I/O

Library Selection

库选择

NeedBest optionWhy
Simple local file
cv2.VideoCapture
Built-in and simple
Full FFmpeg format/protocol supportsubprocess pipeExact CLI behavior
GPU video I/OffmpegcvNVDEC/NVENC with OpenCV-like API
Network/RTSP streamingVidGearThreaded capture and stream helpers
ML batch loadingDecordFast random/batch frame access
Frame-level libav controlPyAVDirect FFmpeg library access
需求最佳选项原因
简单本地文件
cv2.VideoCapture
内置且易用
完整FFmpeg格式/协议支持子进程管道实现与CLI完全一致的行为
GPU视频I/Offmpegcv带有类OpenCV API的NVDEC/NVENC支持
网络/RTSP流处理VidGear线程化捕获和流处理辅助工具
机器学习批量加载Decord快速随机/批量帧访问
帧级libav控制PyAV直接访问FFmpeg库

Critical Gotchas

关键陷阱

  1. OpenCV is BGR. FFmpeg/PyAV/PIL/Decord often produce RGB. Convert explicitly.
  2. NumPy dimensions are
    (height, width, channels)
    .
    Pixel access is
    img[y, x]
    , not
    img[x, y]
    .
  3. Video filters can drop audio. Preserve or remux original audio intentionally.
  4. Release resources. Always close
    VideoCapture
    , pipes, writers, and PyAV containers.
  5. Rawvideo pipes require exact frame size.
    width * height * channels
    must match
    -pix_fmt
    .
  1. OpenCV采用BGR格式。FFmpeg/PyAV/PIL/Decord通常生成RGB格式,需显式转换。
  2. NumPy维度为
    (height, width, channels)
    。像素访问使用
    img[y, x]
    ,而非
    img[x, y]
  3. 视频滤镜可能丢失音频。需有意保留或重新复用原始音频。
  4. 释放资源。务必关闭
    VideoCapture
    、管道、写入器和PyAV容器。
  5. Rawvideo管道要求精确帧大小
    width * height * channels
    必须与
    -pix_fmt
    匹配。

Minimal Pipe Patterns

最简管道模式

FFmpeg to OpenCV using BGR frames:
python
cmd = [
    "ffmpeg", "-i", input_path,
    "-f", "rawvideo", "-pix_fmt", "bgr24", "-"
]
OpenCV to FFmpeg using BGR frames:
python
cmd = [
    "ffmpeg", "-y",
    "-f", "rawvideo", "-vcodec", "rawvideo",
    "-s", f"{width}x{height}", "-pix_fmt", "bgr24",
    "-r", str(fps), "-i", "-",
    "-c:v", "libx264", "-preset", "fast", "-crf", "23",
    "-pix_fmt", "yuv420p", output_path
]
使用BGR帧实现FFmpeg到OpenCV的传输:
python
cmd = [
    "ffmpeg", "-i", input_path,
    "-f", "rawvideo", "-pix_fmt", "bgr24", "-"
]
使用BGR帧实现OpenCV到FFmpeg的传输:
python
cmd = [
    "ffmpeg", "-y",
    "-f", "rawvideo", "-vcodec", "rawvideo",
    "-s", f"{width}x{height}", "-pix_fmt", "bgr24",
    "-r", str(fps), "-i", "-",
    "-c:v", "libx264", "-preset", "fast", "-crf", "23",
    "-pix_fmt", "yuv420p", output_path
]

Core Workflow

核心工作流

  1. Probe input dimensions, fps, duration, and audio streams.
  2. Pick the I/O library based on the selection table.
  3. Lock pixel format at boundaries (
    bgr24
    for OpenCV pipes;
    yuv420p
    for final H.264 output).
  4. Process frames in generators/batches; avoid loading full videos unless they are small.
  5. Preserve or re-encode audio explicitly.
  6. Verify output duration, fps, resolution, codec, and A/V sync.
  1. 探测输入的维度、帧率、时长和音频流信息。
  2. 根据选择表挑选I/O库。
  3. 在边界处锁定像素格式(OpenCV管道使用
    bgr24
    ;最终H.264输出使用
    yuv420p
    )。
  4. 以生成器/批量方式处理帧;除非视频很小,否则避免加载完整视频。
  5. 显式保留或重新编码音频。
  6. 验证输出的时长、帧率、分辨率、编解码器和音视频同步。

Reference Map

参考映射

  • references/opencv-pipelines-and-libraries.md
    - Full preserved reference: color/dimension gotchas, cleanup patterns, FFmpeg-to-OpenCV and OpenCV-to-FFmpeg pipes, bidirectional pipeline, ffmpegcv, VidGear, Decord, PyAV, Modal.com examples, GPU pipeline, cheat sheets, sources.
  • references/opencv-pipelines-and-libraries.md
    - 完整保留的参考文档:颜色/维度陷阱、清理模式、FFmpeg到OpenCV和OpenCV到FFmpeg的管道、双向流水线、ffmpegcv、VidGear、Decord、PyAV、Modal.com示例、GPU流水线、速查表、来源。

Related Skills

相关技能

  • ffmpeg-python-integration-reference
    - Type-safe parameters, colors, time units
  • ffmpeg-pyav-integration
    - PyAV API details
  • ffmpeg-hardware-acceleration
    - GPU decode/encode and filter pipelines
  • ffmpeg-python-integration-reference
    - 类型安全的参数、颜色、时间单位
  • ffmpeg-pyav-integration
    - PyAV API详情
  • ffmpeg-hardware-acceleration
    - GPU解码/编码和滤镜流水线