Creating time-lapse videos with the Nikon D7000 — Part 2 of 2

Ok, this is part 2/2 of my post on how to create time-lapse videos with the Nikon D7000. This part explains how to create a video from a folder filled with photos you took with your camera. Of course, this will work for arbitrary images and is NOT limited to the D7000.

If you want to know how to get suitable input images using your Nikon D7000, be sure to read the first part of this post.

Note that we will do this using open source software only. The method described here requires some technical knowledge — you have to start shell scripts. It’s easy though and I’ll explain it in detail. This method was created under Linux, but it will work under Windows as well if you install some additional software. Here we go!

Install required software

All software we use is free and open source. Windows users need to install Cygwin first. It is a POSIX environment for Windows (it’s not important what that means and it’s rather complicated, and what matters to us is only that this will give you a bash shell and an easy way to install the other software you need). During the Cygwin installation, you can search for and select extra packages. Be sure to install bash (should be on by default), php, imagemagick and ffmpeg.

Linux users should install these packages from their package system.

Put your photos and the shell scripts from this website in the same directory

This method uses three steps (shell scripts) to get the video from the photos. You can download all scripts in a zip, but I also list them here:

NOTE: If you copy and paste the scripts from here, you have to add the php tags to before the first and after the last line of the first script. (The reason is that WordPress won’t let me add PHP code in here.) If you don’t know what that means, just get the zip.

The PHP script s01_rename.php:



$files = scandir(".");
$outdir = "renamed";
if(!file_exists($outdir)) {
mkdir($outdir);
}
$i = 1;
foreach ($files as $file) {
if (preg_match("/DSC_[0-9]{4}.JPG/", $file)) {
$digit = sprintf("%03d", $i);
copy($file, $outdir . "/" . "frame_" . $digit . ".jpg");
$i++;
}
}


The BASH script ‘s02_rescale.sh’:



#!/bin/sh
## This script makes the images smaller. it requires the mogrify-command from imagemagick.
OUTDIR="rescaled"
if [ ! -d "$OUTDIR" ]; then
mkdir "$OUTDIR"
fi
mogrify -path "$OUTDIR" -resize 60x60% -quality 85 -format jpg renamed/*.jpg


The BASH script ‘s03_create_video.sh’:



#!/bin/sh
## This script create the video from all the frames (renamed, rescaled photos)
ffmpeg -f image2 -i rescaled/frame_%03d.jpg -s 800x600 -vcodec libx264 out.mp4


You have to put the scripts (named as shown above, or just get the zip which contains them with correct file names) into a directory that contains all the photos for your video.

Ensure the scripts are executable and run them in BASH / Cygwin

Start a Bash shell (under Windows, double-click the Cygwin-Shortcut to do this). The change to the directory with the scripts and photos:

cd /path/to/photos_and_scripts/

Ensure that the scripts are executable:
(This command adds the ‘executable’ permission (+x) to all files which start with ‘s0’. This includes all 3 script files ‘s01_rename.php’, ‘s02_rescale.sh’ and ‘s03_create_video.sh’.)
chmod +x s0*

Run the scripts in the correct order (each script is gonna take while, maybe more than 10 minutes if you have many photos):

php ./s01_rename.php

OK, the renaming is done. Let’s scale them down…

./s02_rescale.sh

All photos rescaled, now encode the video:

./s03_create_video.sh

You are done. The video is stored in the file ‘out.mp4’ in the same directory. Feel free to mess with the ffmpeg options in the scipt to change the output format and quality.

An example video I created with this method can be found here:
Evening primrose timelapse video (@ Youtube)

Links to sources and similar articles:
FFMpeg Documentation
WikiBook on FFMPeg
miscdebris.net blog post

Advertisements

About dfspspirit

PhD student in bioinformatics, interested in photography, level design, digital image manipulation, architecture and, of course, bioinformatics.
This entry was posted in Photography and tagged , , , , , . Bookmark the permalink.

14 Responses to Creating time-lapse videos with the Nikon D7000 — Part 2 of 2

  1. Pingback: Creating time-lapse videos with the Nikon D7000 — Part 1 of 2 | spirit's spinney

  2. Evening,
    When I get to the last step of creating the time lapse file I get the following error:
    Oracle@Owner-PC /cygdrive/c/cygwin64
    $ ./s03_create_video.sh
    ./s03_create_video.sh: line 4: ffmpeg: command not found

    I did the following command to find the file:

    Oracle@Owner-PC /cygdrive/c/cygwin64
    $ which ffmpeg
    which: no ffmpeg in (/usr/local/bin:/usr/bin:/cygdrive/c/app/Oracle/product/11.2.0/dbhome_1/bin:/cygdrive/c/Program File s/Common Files/Microsoft ==> many files listed

    All the files are located in the cygwin 64 directory

    I am on a windows 7 laptop.

    Any suggestions would be appreciated.

    thanks

    al

    • dfspspirit says:

      It seems you are missing ffmpeg. I just checked with the latest cygwin distribution, and the installer does not seem to offer an ffmpeg package anymore. That should not be a big problem though — it should also work to just install the windows binary from the official ffmpeg website (https://ffmpeg.org/). The windows binaries link from that website currently points here: http://ffmpeg.zeranoe.com/builds/

      Make sure you put in into your PATH if the installer does not do that.

      • Allen Faltynowicz says:

        I sort of realized that. I found that site before but they want to charge $30 for the download. Is there someplace else where this or something similar can be found? If not I will just purchase it.

        Thanks for getting back so quickly on my question.

        Regards

        al

      • dfspspirit says:

        @Allen: For unknown reasons, WP shows no ‘reply’ button for me under your last post, so I reply to my own comment instead.

        FFmpeg is free software (as in free speech), and the download on their website
        is also free (as in free beer) — I just downloaded it, the 64bit Windows static
        version from
        http://ffmpeg.zeranoe.com/builds/win64/static/ffmpeg-20151004-git-84b4998-win64-static.7z.
        If the download site asks you to pay for it, you are at the wrong website I
        guess — did you maybe click the button where you can ask them for a custom build? You do not need that, just download one of the builds available on the website.

      • Allen Faltynowicz says:

        Hi

        When I was doing my research I saw a website charging for it. I didn’t down load of course. I am new at this time lapse stuff but am familiar with unix. I appreciate your thoughtfullness in sending me the correct web site.

        Regards

        al

      • Evening,
        Once I downloaded and saved the file I clicked on it and it took me to a web site to choose what software I will use to open it up of course for a charge– Winzip 7Z they want me to pay $30 for it.

        Maybe I am not understanding something properly here. I believe you said it was free.

        Thanks
        al

      • dfspspirit says:

        Ok, it’s about extracting 7zip files. Free tools are available for that, too.

        Under unix, install the ‘p7zip’ package, it should be provided by your distribution (may be named slightly different, just search for ‘7zip’ in your package manager). Then run from command line: ‘7z x packagefile.7z’.

        Under Windows, use the (free software) tool from http://www.7-zip.org/. (Winzip would also work but is commercial — they used to have a free test version, but I am not familiar with it, I prefer open source software.)

      • Allen Faltynowicz says:

        Morning,
        Thanks for the guidance I will do this tonight. I have a concern though last night when I downloaded I don’t know if I hit the wrong down load button but when it started downloading the ffmpeg zip file from the ffmpeg site it wanted to load up a software game and who knows what other malware onto my laptop. I thought it was harmless until the download was completed then I couldn’t shut this game off or uninstall it. Fortunately I had a recent prior restore point and used that. That eliminated the software. To make sure my laptop was clean I ran a complete Norton scan which took a few hours.
        This zip file I am about to unzip per your directions only has the ffmpeg file and nothing else, correct? I don’t want to go through again what I did last night. I hope you can understand this.
        I will let you know how things turn out tonight after I proceed.
        I appreciate the help.
        Thanks
        Al

      • dfspspirit says:

        Downloading software from the internet is always a risk, and I cannot vouch in any way for 3rd-party web sites of course. Sadly, even some good open source projects have recently been known for bundling adware.

        I just downloaded the FFmpeg 7zip file again though, and the version the server gave me did not contain any adware. It did not even contain an installer, just the application itself in a zip. You can put it where ever you want yourself, but you will have to add it to your PATH environment variable manually due to the lack of an installer of course (be sure to add the ‘bin’ sudirectory, which contains the binary file ffmpeg.exe, to the PATH).

        To check if FFmpeg was installed correctly and you added it to the %PATH% correctly, you can click Start Menu => Run command, enter ‘cmd.exe’, then run ‘ffmpeg’ on the command line. You should get output from ffmpeg (instead of something like ‘command not found’). If this is not the case, FFmpeg is not installed properly (missing in PATH or incorrect location), and the script cannot work.

      • Allen Faltynowicz says:

        Afternoon,
        I never ment to imply blame to you. Thanks for your advice I will follow your suggestions. I will reply after I get this done hopefully tonight.

        Have a great day!
        al

      • Evening DF,
        I made progress. I managed to download all the utilities and execute them. I still receive an error though when I run s03 as the final step. It can’t find the ffmpeg executable. Also as a note it was chmod’ d in the original steps. I didn’t over look that.

        I have that exe in the path as seen below:

        I managed to download and extract the zip utility. I then unzip the ffmpeg file into the cygwin64 folder.

        excerpt:

        Extracting ffmpeg-20151006-git-d1c8368-win64-static/presets/libvpx-720p50_60.ffpreset
        Extracting ffmpeg-20151006-git-d1c8368-win64-static/bin/ffmpeg.exe
        Extracting ffmpeg-20151006-git-d1c8368-win64-static/bin/ffplay.exe

        I still get the following error.

        ./s03_create_video.sh: line 4: ffmpeg: command not found

        Here is my path:

        C:\cygwin64\ffmpeg-20151006-git-d1c8368-win64-static\bin; <===I will rename the long file later.

        and I also tried it this way with a final slash at the end:

        C:\cygwin64\ffmpeg-20151006-git-d1c8368-win64-static\bin\;

        Any suggestions that you may have would be appreciated. I am running Windows 7 professional on an HP.

        Thanks for your help in advance and have a good evening.
        Regards
        al

  3. Hi Everyone,

    I got the system to work. This file ffmpeg-20151006-git-d1c8368-win64-static has all the executable in it. I also placed this file in the path and it did not originally work. Today the first thing I did was change the name of that file to simply ffmpeg in the path. I then tried the s03*.sh to create the out.mp4 file.

    Wouldn’t you know it? It worked.

    Just wanted to finish up this string so that other stumped people may draw from it any information/ insides they may need.

    Bye

  4. Allen Faltynowicz says:

    Good Morning Df,
    I looked at my problem as you suggested and it was in fact the regular expression that was messing up. Here is my solution of the script. It now runs properly. I just want to say that the compiler was edited to point to the proper location too.

    This is what was inserted into the script:

    if (preg_match(“/\d{1,4}.JPG$/”, $file))
    The /\d{1,4} part handles the digits up to 4 of them and the “$” / tells the compiler to start looking at the end of the file for the digit pattern.

    $i = 1;
    foreach ($files as $file) {
    if (preg_match(“/\d{1,4}.JPG$/”, $file)) {
    $digit = sprintf(“%03d”, $i);
    copy($file, $outdir . “/” . “frame_” . $digit . “.jpg”);
    $i++;
    }

    Output before:
    2015-10-11 2015-10-11 001 001.JPG
    2015-10-11 2015-10-11 001 002.JPG
    2015-10-11 2015-10-11 001 003.JPG

    Output after processing:

    frame_001.jpg frame_092.jpg frame_183.jpg
    frame_002.jpg frame_093.jpg frame_184.jpg
    frame_003.jpg frame_094.jpg frame_185.jpg
    frame_004.jpg frame_095.jpg frame_186.jpg
    frame_005.jpg frame_096.jpg frame_187.jpg

    PROBLEM: the 3rd script s03_create_video kicks out the following error:

    ERROR MESSAGE:
    [NULL @ 00000000005a2b20] Unable to find a suitable output format for ‘./s03_create_video.sh’
    ./s03_create_video.sh: Invalid argument

    Details:

    Inside script:

    ffmpeg -f image2 -i rescaled/frame_%03d.jpg -s 800×600 -vcodec libx264 out.mp4

    I am thinking that input parameter frame_%03d.jpg is wrong and should maybe written as : frame_%04d.jpg but that didn’t do anything either. After reviewing the docs I tried several variations but with no luck.

    Your thoughts on this?

    Thanks in advance.

    al

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s