How do I add a delay to a live stream sourced from webcam (v4l2) with FFMPEG?

5.8k views Asked by At

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.

1

There are 1 answers

3
Gyan On BEST ANSWER

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.