4

How can I use FFMPEG to add a delay to a stream being sent from a (v4l2) webcam to a media server?

The use case here is something like a security camera where I want to be able to stream video to a server when something is detected in the video. The easiest way to ensure the event of interest is captured on the video is to use FFMPEG to stream from the camera to a virtual loopback device with an added delay. That loopback device can then be used to initiate live streaming when an even of interest occurs.

In GStreamer, I would accomplish a delay of this sort with the queue element's min-threshold-time parameter. For example the following (much-simplified) example pipeline adds a 2 second delay to the output coming from a v4l2 webcam before displaying it:

gst-launch-1.0 v4l2src device=/dev/video1 ! queue max-size-buffers=0 max-size-time=0 max-size-bytes=0 min-threshold-time=2000000000 ! xvimagesink

How do I accomplish the same thing with FFMPEG? There are some technical challenges that prevent us from using GStreamer for this.

I have investigated the itsoffset option for this, but as far as I can tell it is only usable for already-recorded files, and it is not clear what a good alternative would be.

David
  • 43
  • 1
  • 4

1 Answers1

10

With a recent git build of ffmpeg, basic template is

ffmpeg -i input -vf tpad=start_duration=5 -af adelay=5000|5000 stream-out

The tpad filter will add 5 seconds of black at the start of the video stream, and the apad filter will add 5000 milliseconds of silence to the first two channels of the audio.

Gyan
  • 85,394
  • 9
  • 169
  • 201
  • 1
    Thanks, this worked perfectly! How serendipitous that this filter just made it into master a few weeks ago (by my reading of the FFMPEG listservs). Are there any sources to which one can subscribe to see these kinds of new features without being exposed to the full torrent of development-related conversation? – David Nov 21 '18 at 17:59
  • Thanks for everything here, you've been extremely helpful. – David Nov 21 '18 at 20:35
  • `tpad` will keep the previous 5 seconds of original video or it will discard the past 5 seconds? – Muhammad May 17 '22 at 22:56