Time-lapse movie with webcam & ffmpeg

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
Wognath
Posts: 423
Joined: Sun 19 Apr 2009, 17:23

Time-lapse movie with webcam & ffmpeg

#1 Post by Wognath »

It wasn't that easy to find practical info on time-lapse photography. By google, trial and error I found some methods that work, which might be useful to someone as a starting point. Thanks in advance for improvements or better methods.

These methods work in Fatdog 702 (ffmpeg 2.1.3) or in Tahr + ffmpeg from the repo, using either the netbook built-in webcam or usb webcam.

"By using a relatively simple filter chain, FFmpeg can generate timelapse video in one step." https://tekwiki.beylix.co.uk/index.php/ ... ith_FFmpeg

Code: Select all

V="select=not(mod(n\,60)),setpts=N/(30*TB),fps=30"
ffmpeg  -i /dev/video0 -vf $V output.mov 
Filter rejects frames whose framenumber n is not divisible by 60 (so 0.5fps for my webcam input of 30fps); setpts... resets frame's timestamp (so output is a movie, not a 0.5 fps slide show); output is set to 30 fps.

Another edit :shock: This method is much simpler and appears to be more reliable on a slow computer. The -t value gives the real time and the filter the time-lapse factor. Example: a 15 second video covering 60 seconds of real time

Code: Select all

ffmpeg -t 60 -i /dev/video0 -filter:v "setpts=0.25*PTS" file.mp4
Monitor while recording (added 11/2017)

Code: Select all

V="settb=1/30,setpts=N/TB/30"
ffplay /dev/video1 &
ffmpeg -framerate 2 -f x11grab -s 640,480 -show_region 1 -i :0+0,40 -vf $V -r 30 out.mov
640x480 is the size of the screen grab=webcam resolution; show_region helps to align ffplay window with grab. -i :0+0,40 specifies display 0 with y-offset of 40 px from top left. Press q to quit ffmpeg properly. Works well in Fatdog710. In Tahr, where ffplay is not available, "avplay -f video4linux2 /dev/video0" runs, but the video lags severely.

Combine jpegs (e.g. http://lukemiller.org/index.php/2014/10 ... pse-notes/) This works well and it's easy to change the time between frames. The images can be monitored as acquired. maag or ffmpeg itself can be use to make an animated gif. A large number of jpeg files must be stored temporarily.

Code: Select all

cd /tmp
rm -f image*

for i in {0..120} ; do
file="image"$(echo "000$i" | tail -c 5)".jpg"   # file name format image%04d.jpg
ffmpeg -f v4l2 -an -i /dev/video1 -frames:v 1 -f image2 $file
sleep 5
done

ffmpeg -i image%04d.jpg MOVIE.mov
concat command in ffmpeg, https://trac.ffmpeg.org/wiki/Concatenate allows a video file to be built up frame by frame. I found it necessary to use a temporary transfer file; otherwise video freezes after a few seconds.

Code: Select all

cd /tmp
echo -e "file 'MOVIE.mov\nfile 'M1.mov'" > mylist.txt  #instructions for concat command

ffmpeg -f v4l2 -an -i /dev/video1 -frames:v 1 -y  MOVIE.mov   # first frame

for T in {0..120} ; do
sleep 5
ffmpeg -f v4l2 -an -i /dev/video1 -frames:v 1 -y M1.mov  # new single frame
ffmpeg -f concat -i /tmp/mylist.txt -y -c copy temp.mov  # transfer file
mv -f temp.mov MOVIE.mov
done
mov, avi, mpg, mp4 and ts all work. Not very familiar with relative advantages of different formats. mov yields the smallest file. Timing: on my system, sleep 5 -> ~5.5 s per frame, default output is 29.4 frames/s.

Post Reply