Tuesday, July 14, 2015

Rough plan for timelapse movie creation

Setup:

Install these pre-requisite tools:
(These are command-line tools that I use on Linux, but there are Windows versions as well, and hopefully all the commands will work on it equally well.)

ImageMagick: http://www.imagemagick.org/script/binary-releases.php#windows (check if your computers are running a 64-bit or 32-bit OS... I bet it's 32-bit.)

FFMPEG: http://ffmpeg.zeranoe.com/builds/  (download the appropriate 32 or 64-bit "static" version)

General plan:

IMPORTANT:  1) Make sure you have a COMPLETE backup of your timelapse photos somewhere.  They are irreplaceable.  2) Also copy a couple images to a new testing folder, so you can try out running commands there without messing up your original files.  If you specify the file names in the wrong order with ImageMagick, you can accidentally overwrite your input files...

EDIT: Pre-processing step for the timelapse photos.  All of the photos will need cropped (and possibly rescaled) identically.  Imagemagick has a command that can do this for us.  (Note: usually "mogrify" changes the original files, but if you start with .JPG and give it the "-format png" option, it will turn each XYZ.JPG file into an XYZ.png file.

In a folder that contains only the timelapse photos that you want to crop, run something like this:

mogrify -format png -crop WxH+X+Y -resize <NEW_WIDTH>x *.JPG

where you replace W and H with the width/height of the cropped region, and X and Y with the pixel offsets.  The -resize <NEW_WIDTH> part is optional, and you may or may not want to do it, depending on how high-resolution of movie you'd like to output in the end... it will take the resulting crop and scale it down so that the image has the width you specify.
Be careful to leave the "x" and "+" signs in the command shown above.

Run NetLogo model to create a bunch of PNG files that match the timelapse photos... should create the same number of image frames here.  (EDIT: you may need to play around with the "pixels per patch" setting in NetLogo to get the exported images to be the same size as the cropped timelapse photos.)

In MatLab, write a script that takes the starting frame number from the timelapse images as an input, and goes through a loop that stacks (vertically? horizontally?) each model image with the paired real image, to make a new image called movie_frame_####.png.  To do this, have your MatLab script execute the external "montage" program (comes with ImageMagick, Google for examples) to stack two images together.

After you have all the movie_frame_####.png files, you can create a movie out of them using the FFMPEG software.  Something like this (all one line) should do the job:

ffmpeg -r 15 -i movie_frame_%04d.png -vcodec libx264 -crf 20 -pix_fmt yuv420p -r 30 sand_grid_movie.mp4

Change the first "15" above to control the frame rate (frames per second) for the frames going into the movie.  (Leave the later -r 30, which controls the frame rate for the output movie, and it seems like it's best to output at 30 FPS to be more compatible with different movie players...)

If the MP4 won't play in your windows movie player, for WMV output try:

ffmpeg -r 15 -i movie_frame_%04d.png -q:v 2 -r 30 sand_grid_movie.wmv

Lastly, I've done all of this stuff before, so if you run into problems, let me know, and I can help!

(Side note: both ImageMagick and FFMPEG have a myriad of other features, which you can find out more about on the web if you wish.)

No comments:

Post a Comment