Thursday, July 3, 2014

Time-lapse video on Linux using ffmpeg or avconv


I recently did a bit of a tutorial on creating a timelapse video from a series of images using free software for Windows. I did this because, many people still use it as their operating system. (Clearly they are unaware it's no longer the best choice.)

My primary OS is Linux and so I thought I would follow up that article with a simpler one on how to create a time-lapse video from images using avconv or ffmpeg on Linux.

Keeping things relatively simple, the avconv command would look something like this.

avconv -f image2 -r 25 -i DSC_%04d.jpg -vcodec libx264 timelapse.mp4


and for ffmpeg

ffmpeg -r 25 -i DSC_%04d.jpg -q:v 0 -s hd1080 -vcodec libx264 -crf 25 timelapse.mp4



I won't go into detail on the differences, suffice to say that avconv forked from ffmpeg and is now the default included in Ubuntu Linux. What you use really will come down to personal choice and distro availability.

To explain in a little more detail. For both commands, any arguments prior to the -i, are arguments for the input data. In the cases above the -f image2 tells avconv the input is in image format, whilst the -r 25 sets the input framerate at 25 frames per second.

Any arguments after the -i are output arguments. (With the exception of the input files specification which is part of the -i flag)

The input file name as shown above (DSC_%04d.jpg) tells the application to match any files starting with DSC_ and then containing 4 numerical digits before the extension. (DSC_1024.jpg, DSC_1025.jpg, DSC_1026.jpg .......)

If we were to use a %d instead of %04d, then it would look for input files in numerical sequence. (DSC_1.jpg, DSC_2.jpg, .... DSC_123.jpg, ... etc)

The %04d allows us to specify that there will always be 4 digits. For example: DSC_0001.jpg, DSC_0002.jpg....)

-vcodec is to set the video output codec. Unless you have a specific need to use something else I highly recommend using libx264

-crf 25 sets the output framerate to 25 frames per second. (I believe this is default anyway if not specified)

-q:v 0 is to set the video quality scale. 0 being excellent quality.

-s hd1080 is setting the resolution of the video to Full HD (1920x1080). -s hd720 should therefore be pretty self explanitory.

Finally, the filename.... I called it (very imaginatively) timelapse.mp4, but you can set whatever name you deem appropriate.

Please note, this is not taking into account any potential need to deflicker the images or anything else like that.

It is by no means an exhaustive list of options available to control either of these applications, but the available options are extensive so if you want more in-depth control I suggest you look at the documentation on the website or read the man page.

Additionally, I would highly recommend you put this into a bash script to make future timelapse creation much easier, and to help facilitate other functions such as deflickering prior to encoding, etc.

A simple bash script could be:

#!/bin/bash
if[-n "$1"] then ffmpeg -r 25 -i $1/DSC_%04d.jpg -q:v 0 -s hd1080 -vcodec libx264 -crf 25 $1/timelapse.mp4
else
ffmpeg -r 25 -i DSC_%04d.jpg -q:v 0 -s hd1080 -vcodec libx264 -crf 25 timelapse.mp4
fi


This is a simple example, it checks to see if you have passed a directory as the first argument and uses it, otherwise it assumes you are getting input and output from the current directory. It should be easy to expand upon this as required. This certainly saves you having to retype the command all the time and even helps prevent getting multiple copies of the script all over the place.

Remember to set it as executable (chmod +x your_script_filename)

Enjoy.

2 comments:

  1. I used avconv but when I play the resulting video it's just a still image for 12 seconds, no movement...
    $ avconv -f image2 -r 10 -start_number 344 -i IMAG%04d.JPG -vcodec libx264 -s hd720 -crf 25 tlfullhiqual.avi

    ReplyDelete
  2. My bad... I used a deflicker perl script which was creating the same images... avconv did it's job well finally.

    ReplyDelete