You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何设置GStreamer JPEG图像捕获的结束时间?捕获数量异常排查

问题

计划用GStreamer每秒捕获1张JPEG图像,总共捕获10张,但实际仅生成8张图像就提前结束。

使用的命令:

gst-launch-1.0 -e v4l2src device=/dev/video0 num-buffers=10 ! queue ! videorate ! image/jpeg,framerate=1/1 ! multifilesink index=0 location=frame%04d.jpg sync=true async=false

执行日志及文件列表:

Setting pipeline to PAUSED ...
Pipeline is live and does not need PREROLL ...
Pipeline is PREROLLED ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock
Got EOS from element "pipeline0".
EOS received - stopping pipeline...
Execution ended after 0:00:08.000811937
Setting pipeline to NULL ...
Freeing pipeline ...

$ ls -al
total 19436
drwxr-xr-x  2 pi pi    4096 Mar 31 14:37 .
drwxr-xr-x 16 pi pi    4096 Mar 31 12:28 ..
-rw-r--r--  1 pi pi 1757274 Mar 31 14:37 frame0000.jpg
-rw-r--r--  1 pi pi 1688725 Mar 31 14:37 frame0001.jpg
-rw-r--r--  1 pi pi 2737480 Mar 31 14:37 frame0002.jpg
-rw-r--r--  1 pi pi 2736670 Mar 31 14:37 frame0003.jpg
-rw-r--r--  1 pi pi 2736670 Mar 31 14:37 frame0004.jpg
-rw-r--r--  1 pi pi 2737234 Mar 31 14:37 frame0005.jpg
-rw-r--r--  1 pi pi 2737572 Mar 31 14:37 frame0006.jpg
-rw-r--r--  1 pi pi 2736832 Mar 31 14:37 frame0007.jpg

疑问:如何设置GStreamer的运行时间或结束时间?


解决方法

1. 保证捕获指定数量的图像

当前问题出在num-buffers=10指定的是从摄像头获取的缓冲帧数,但经过videorate强制帧率时,可能因为同步逻辑丢弃部分帧,再加上multifilesink的同步写入,导致最终输出的图像数量不足。

可以通过两种方式修正:

  • 增大输入缓冲帧数,同时限制multifilesink的输出文件数量:
    gst-launch-1.0 -e v4l2src device=/dev/video0 num-buffers=12 ! queue ! videorate ! image/jpeg,framerate=1/1 ! multifilesink index=0 location=frame%04d.jpg sync=true async=false max-files=10
    
    这里num-buffers=12预留足够的缓冲帧,max-files=10确保只输出10张图像。
  • 移除videorate,直接让multifilesink按单帧间隔写入,配合num-buffers=10:
    gst-launch-1.0 -e v4l2src device=/dev/video0 num-buffers=10 ! queue ! image/jpeg ! multifilesink index=0 location=frame%04d.jpg sync=true async=false next-file=1
    

2. 设置固定运行时间

如果需要按时长控制管道运行,比如运行10秒以捕获10张图像,有两种常用方式:

  • 用系统timeout命令包裹GStreamer命令,简单直接:
    timeout 10 gst-launch-1.0 -e v4l2src device=/dev/video0 ! queue ! videorate ! image/jpeg,framerate=1/1 ! multifilesink index=0 location=frame%04d.jpg sync=true async=false
    
    这里timeout 10表示让命令运行10秒后强制终止。
  • 纯GStreamer方案,使用timer元素(需安装gst-plugins-good):
    gst-launch-1.0 -e v4l2src device=/dev/video0 ! queue ! videorate ! image/jpeg,framerate=1/1 ! multifilesink index=0 location=frame%04d.jpg sync=true async=false ! timer timeout=10000000000 ! fakesink signal-handoff=true
    
    timeout=10000000000代表10秒(单位为纳秒),时间到达后会发送EOS信号终止管道。

内容的提问来源于stack exchange,提问作者hslee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 12:32:09