
摄像头采集实战:预览、编码、录制与转码专栏:GStreamer C++ 从零到工程实战· 第 14 篇 / 共 17 篇识别摄像头能力并用 Caps 选择格式,通过 tee 与 queue 同时预览和录像,掌握发送 EOS、等待完成与安全收尾的正确顺序。第 17 课:摄像头采集、编码、录制与转码这一课把前面学过的 Source、Caps、tee、queue、Encoder、Muxer 和 EOS 串成一个真实项目。一、先识别本机摄像头能力Windows 主线优先从 Media Foundation Source 开始:gst-inspect-1.0 mfvideosrc gst-device-monitor-1.0 Video/Source gst-launch-1.0-v mfvideosrc!videoconvert!autovideosink如果mfvideosrc不存在,先检查官方 Runtime/插件安装是否完整,不要直接在 C++ 中反复改代码。二、Caps 用来选择采集格式例如请求 1280×720、30 FPS:gst-launch-1.0-v mfvideosrc `!video/x-raw,width=1280,height=720,framerate=30/1 `!videoconvert!autovideosink如果摄像头不支持这个组合,Caps 协商会失败。先用-v和设备监视工具确认真实能力。三、预览与录像为什么要用 tee + queue?图:摄像头通过 tee 分成低延迟预览与编码录像两条支路两个分支速度不同;没有独立 queue,一个慢分支会直接拖住另一个分支。使用 Windows Media Foundation H.264 Encoder 的示例:gst-launch-1.0-e mfvideosrc!videoconvert!teename=t ` t.!queue!videoconvert!autovideosink ` t.!queue!video/x-raw,format=NV12 `!mfh264enc!h264parse!mp4mux `!filesink location="capture.mp4"-e会在 Ctrl+C 时向 Pipeline 发送 EOS,使 Muxer 有机会写完文件尾和索引。没有正常 EOS 的 MP4 可能无法播放或缺少时长信息。若本机没有mfh264enc,先用gst-inspect-1.0查找可用编码器,再根据它的 Sink Caps 调整格式;不要假设所有编码器 Property 相同。四、一个可正确结束录像的 C++ 程序