Timelapse video using gstreamer

13/10/2017 - 14:17
No votes yet

Timelapse videos can easily be created using gstreamer. This tutorial will show various options, explain and demonstrate how to do timelapse videos with gstreamer CLI tools. A basic knowledge of gstreamer is assumed.

We will capture images at some set interval, these images are then later combined into a video file. Command lines are split into multiple lines, enter them as one line only.

Note: This tutorial is still work in progress and we will be adding more examples in the future.

Gstreamer elements we will be using

We will be using the following gstreamer tools and elements, their purpose will be explained after the list.

  • v4l2src
  • videorate
  • jpegenc
  • multifilesink
  • timeoverlay
  • clockoverlay
  • queue
  • autovideosink

Video source element

v4l2src is the video source element we will be using in these examples. This can be something else too, for example a live rtsp/hls feed from a network video camera or even just a video file.

Timelapse capture examples

Timelapse capture, 1 second interval

This command will capture video frame into multiple picture files, every 1 second.

gst-launch-1.0 v4l2src device=/dev/video0 ! queue ! videorate ! video/x-raw,framerate=1/1 ! jpegenc ! multifilesink location="./frame%06d.jpg"

Timelapse capture, 1 second interval, live video view

This command will capture video frame into multiple picture files, every 1 second. It will also show a realtime video in a window with full framerate.

gst-launch-1.0 v4l2src device=/dev/video0 ! tee name=t ! queue ! autovideosink t. ! queue ! videorate ! video/x-raw,framerate=1/1 ! jpegenc ! multifilesink location="./frame%06d.jpg"

With timestamp and clock overlays, with live view

gst-launch-1.0 v4l2src device=/dev/video0 ! timeoverlay halignment=right valignment=bottom ! clockoverlay halignment=left valignment=bottom time-format="%Y/%m/%d %H:%M:%S" ! tee name=t ! queue ! autovideosink t. ! queue ! videorate ! video/x-raw,framerate=1/1 ! jpegenc ! multifilesink location="./frame%06d.jpg"

Change capture interval

Adjust the framerate setting in the command lines, for example to capture every 10 seconds, use "framerate=1/10"

Creating timelapse video from images

Create OGG Theora video

This will create an OGG Theora encoded video, with the framerate of 30 frames per second

gst-launch-1.0 multifilesrc location="frame%06d.jpg" caps="image/jpeg,framerate=30/1" ! jpegdec ! theoraenc ! oggmux ! filesink location="out.ogg"

Create H264 AVI

gst-launch-1.0 multifilesrc location="frame%06d.jpg" caps="image/jpeg,framerate=30/1" ! jpegdec ! x264enc ! avimux ! filesink location="out.avi"

Anonymize faces in the video

You might need to anonymize people in the resulting video. Luckily gstreamer comes with element that can be used for this purpose, faceblur. It will slow down the encoding process considerably but will do the trick.

gst-launch-1.0 multifilesrc location="frame%06d.jpg" caps="image/jpeg,framerate=30/1" ! jpegdec ! videoconvert ! videorate ! faceblur ! videoconvert ! theoraenc ! oggmux ! filesink location="out_faceblur.ogg"

Create video using ffmpeg

The following command will create a h264 mp4 video with the framerate of 30 frames per second from the images captured using any of the above methods. Resolution of the resulting video will be the same as for the image files.

ffmpeg -framerate 30 -i frame%06d.jpg out.mp4

If you would like to change the output resolution, add a video scale filter:

ffmpeg -framerate 30 -i frame%06d.jpg -vf scale=800:480 out_scaled.mp4
Keywords: 
gstreamer
timelapse
ffmpeg
video