Commands to create (time lapse) videos from individual frames

12 Feb 2022 – Ronny Errmann

Some of the time lapses can be found on youtube: Youtube Channel Ronny Errmann, for example an 8 hour time lapse of the night sky over La Palma. If you modify your images before creating a video, creating a working directory and copying the images into that will safe you from accidentally destroying the originals.

Create a Video from images:

mencoder mf://*.jpg -mf fps=24:type=jpg -noskip -vf scale=1156:868 -of lavf -lavfopts format=mp4 -ovc x264 -sws 9 -x264encopts nocabac:level_idc=30:bframes=0:bitrate=16384:threads=auto:turbo=1:global_header:threads=auto:subq=5:frameref=6:partitions=all:trellis=1:chroma_me:me=umh -o output.mp4

The following settings should be adapted to your images:

  • fps=24 : if the framerate of the pictures is not high enough a lower number might be better
  • scale=1156:868 : The pictures of my camera are 3 times bigger along each dimension than the values here, be sure to downsize your images without stretching one axis
  • bitrate=16384 : That bitrate gives reasonable results for my time lapses, however, you might consider larger values (better quality) or smaller values (less storage space needed)

Modify images before creating a video from them:

mogrify -resize 1156x868 IMG_20220212*.jpg

Rotate the images:

mogrify -rotate 90 -background black *.jpg

Crop the images:

To remove useless bits from the image, the values are the new size + start positions

mogrify -crop 3515x2541+142+199 *.jpg

Add exif information to the image, e.g. the time:

Sometimes it’s nice to see the real time running in the time lapse, for this the exif information can be used. In the example the modified images will be copied into “prefix_<original name>”

for img in *jpg; do convert "$img" -gravity SouthEast -pointsize 70 -fill white -annotate +30+30  %[exif:DateTimeOriginal] "prefix_""$img"; done

The following options can be changed:

  • gravity : this gives the corner in which the text should appear. Use geography, e.g. Northwest, South
  • pointsize : size of the text, probably worth to test on a subset of images first (copy images into a subfolder)
  • fill : text colour
  • annotate : distance from the chosen corner

Increase the brightness of the images:

For example, when taking night images and the video is too dark.

mogrify -modulate 200 *.jpg

Video modification for time lapse videos

6 Feb 2022 – Ronny Errmann – last modified on 31 March 2022

Some useful bits which helped me to make the time lapse videos I recorded ready to be published on youtube: Youtube Channel Ronny Errmann

Rotate an mp4 video:

For when the phone was upside down during the recording.

ffmpeg -i input.mp4 -c copy -metadata:s:v rotate="180" output.mp4

Crop an mp4 video:

When personal information is in an area of the video.

1. Get the video resolution:

ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 input.mp4

2. Modify the video:

ffmpeg -i input.mp4 -filter:v "crop=1280:600:0:120" output.mp4

Cut an mp4 video:

To keep 00:00:00 to 00:01:22, because afterwards only night was recorded as work had to be finished first.

ffmpeg -i input.mp4 -ss 00:00:00 -to 00:01:22 -c copy output.mp4

Merge/concatenate mp4 videos:

When you notice at the end of a time lapse that the sky is still amazing and you recorded a second one.

Version 1 with the concat protocoll (https://trac.ffmpeg.org/wiki/Concatenate)

Several lines of shell script, run with sh:

for f in $(ls input*.mp4); do
ffmpeg -i $f -c copy -bsf:v h264_mp4toannexb -f mpegts $f.ts
done
CONCAT=$(echo $(ls *.ts) | sed -e "s/ /|/g")
ffmpeg -i "concat:$CONCAT" -c copy -bsf:a aac_adtstoasc output.mp4
rm *.ts

Use Version 2 if this fails with:

Codec 'mpeg4' (12) is not supported by the bitstream filter 'h264_mp4toannexb'

Version 2 with the concat demuxer (https://trac.ffmpeg.org/wiki/Concatenate)

for f in input*.mp4; do echo "file '$f'" > mylist.txt; done
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
rm mylist.txt

Change the speed of mp4 videos:

You took a time lapse, but the viewers are bored, because changes are too small. Time to speed the video up.

ffmpeg -i input.mp4 -r 16 -filter:v "setpts=0.50*PTS" output.mp4

0.5*PTS mean twice the speed, 0.1*PTS would mean ten times the speed, and 2*PTS would mean half the speed.

How to code, an additional note

(by Ronny Errmann)

My last post could be read quite negatively, in the way that I didn’t make any progress. So I wanted to add some notes to counteract that feeling. When I looked into new methods actively, because I had a problem, I learned a lot. And I easily remember a few bits from the last years:

  • Replacing loops and conditions with numpy operations on the whole array or part of the array. This made execution so much faster. For example using
array[array > 0] = numpy.average(another_array, axis=1)
  • Making my program compatible to Windows. It’s just a few things to keep Python code compatible to different operating systems.
  • Adding multiprocessing to the program.

All of these helped me to learn new concepts and improve.

How to code, to my younger me

(by Ronny Errmann)

Every time I prepared for interviews or when I started a new role, I learned so many new concepts. Unfortunately, once working in a scientific role, I found it harder to keep this learning up. Making the time to learn about concepts and to talk about code was quite low on my priority list. While reviewing code is something, which I still think is difficult, learning new concepts is something that can easily done. For example, freeing up one hour once or twice a week can make a big difference. Recently I look into C++ methods and just by looking through a few pages of a tutorial (e.g. about classes), I feel I increased my knowledge by a large amount. So there is really no reason to use that as an excuse.

Discussion about code is a bit more difficult. Being the sole programmer in a team doesn’t make things easier, discussion of code means I take time from a different project. Furthermore, in a scientific environment, most people were also self-taught and, like me, only learned the necessary information and were goal driven. But of course, it is important to discuss code and the few experiences, that I have had in the last months, together with the explanation how companies practise these discussions, have changed my view a lot. It is worth the effort, and try to find a group to discuss things can be really beneficial. So similarly to my last post, I would answer to the question, if I would do things differently now with a clear yes.

A beginners code

(by Ronny Errmann)

As mentioned in my last post, I started coding without asking for advice. Which meant I would do things inefficiently. For example to calculate the standard deviation I used the following code:

mittel=sum(l for l in temp)/len(temp)
stdev=math.sqrt(sum((l-mittel)**2 for l in temp)/(len(temp)-1))

Later I learned about numpy, which made things a bit easier:

stdev = numpy.std(temp, ddof=1)

This is also quite a bit more efficient in computing time as it is run in compiled code and not has the overhead of an interpreter language.

Looking back I can smile about the code. In an interview I was once asked about that program if I would do things differently now. To which my answer was a clear yes.

How I came to writing code

(by Ronny Errmann)

The first device I wrote my first programs was the CASIO CFX-9850G, a graphical calculator. We needed it for school (I think 8th grade) and to get it for a lower price, were suggested to buy it even half a year earlier. That was in the mid 90s and, out of interest and out of boredom, I read the manual to see what one can do with it. And by talking to friends, being showed programs from others, I started to understand other people’s code and then tried to solve smaller questions. With time the programs got bigger. In the end I programmed a battleship game and 4 wins, ready to be played in boring lessons against the person sitting next to you. While the programs are long gone (maybe the hand-copied code is still in a folder at my parents place), the calculator is still a part of my desk, although, primarily just as a calculator.

A few years later (or at the same time?) we learned Delphi in School. It was only one year with one or two hours a week, so it couldn’t go into details, however, I know I tried to improve the programs and write more complex ones after school or at home. The GUI made some parts easy, however, looking back it also prevented me from understanding that there is a big benefit for writing back-end code.

After School my interest in coding decreased, mostly because I thought I wasn’t good enough or didn’t see the application. And interest in learning background theory was limited at that age. When I started my Physics studies live was also busy enough. To analyse the laboratory experiments I would work with Excel/OpenOffice Calc and Origin. The built-in functions were enough to solve everything, not elegant, but efficient (in terms of my time).

In my masters project I found the limitations of OpenOffice Calc. To do calculations I just needed so many columns that I would crash it on a regular basis. However, it took until I had to do the first analysis of observational data, that I noticed I need to learn programming. Other people at the institute suggested Python as a start. And so my first basic program was just a python script that would do the steps I would do by hand by itself. It was very procedural, and when I look back, quite funny in how I wrote it. I definitely didn’t search for advice. Luckily this has changed over the years.