Using ffmpeg to convert a set of images into a video 2016-04-05

Original 2012-11-16, Updated 2016-04-05: cleanup and information about overlaying images.

When using ffmpeg to compress a video, I recommend using the libx264 codec, from experience it has given me excellent quality for small video sizes. I have noticed that different versions of ffmpeg will produce different output file sizes, so your mileage may vary.

To take a list of images that are padded with zeros (pic0001.png, pic0002.png…. etc) use the following command:

ffmpeg -r 60 -f image2 -s 1920x1080 -i pic%04d.png -vcodec libx264 -crf 25  -pix_fmt yuv420p test.mp4

where the %04d means that zeros will be padded until the length of the string is 4 i.e 0001002000302000 and so on. If no padding is needed use something similar to pic%d.png or %d.png.

  • -r is the framerate (fps)
  • -crf is the quality, lower means better quality, 15-25 is usually good
  • -s is the resolution
  • -pix_fmt yuv420p specifies the pixel format, change this as needed

the file will be output (in this case) to: test.mp4

Specifying start and end frames

ffmpeg -r 60 -f image2 -s 1920x1080 -start_number 1 -i pic%04d.png -vframes 1000 -vcodec libx264 -crf 25  -pix_fmt yuv420p test.mp4
  • -start_number specifies what image to start at
  • -vframes 1000 specifies the number frames/images in the video

Batch Crop And Join Sets Of Images 2016-03-10

Recently I needed to take two different animations and join them together to create a video using ffmpeg the result is here.

I needed to take an image with a size of 1920x1080 and crop it on the top and bottom to 1920x540. The simplest way to do this is to use convert from ImageMagick.

For a single image this looked like:

convert input.png -gravity Center -crop 1920x540+0+0 +repage output.png

By specifying the output size after cropping and to keep the image centered, convert automatically removed the top and bottom equally.

In batch the simplest way is to write a for loop or use mogrify

Utility functions for 3D Grid indexing 2016-03-07

Dealing with grid like data structures is pretty common in collision detection tasks or eulerian/grid based solvers. This post is meant to provide helper functions such cases.

Compute the bounds of a grid given point data. In this case Thrust is used for its Transformed Reduce operation.

Combining Sequentially Numbered Text Files 2016-03-02

I have a list of sequentially numbered files and I would like to combine them while preserving their sequential ordering.

Example Input:


The process can be broken into three steps:

  • List the files which we want to combine.
  • Sort the listed files by their version and not name, this preserves the ordering.
  • Cat sorted files into a single file.
ls foo*.txt |sort --version-sort | xargs cat > foo_all.txt

To combine files from a specific sequence the following can be used

for i in {1..12}; do cat foo_$i.txt>>foo_all.txt; done

Reverse Mapping Between Two Lists Using Thrust 2016-03-01

The goal of this post is to provide a simple example for a common scenario in physics engines. Say I have a list of cells in 3d space and a list of points in 3D space. It is simple to compute for each point what cell it belongs in but much more costly to compute the points for a given cell. Reversing this mapping is a useful way to be able to iterate in parallel over cells or points as necessary.

The following code example will continue to use the cell/point analogy

Input: point_cell_mapping is a vector of cell indices, one index for each point

First we create a new vector called point_number which is a sequence from 0->number of points - 1

thrust::sequence(point_number.begin(), point_number.end());

Then we sort the input list

thrust::sort_by_key(point_cell_mapping.begin(), point_cell_mapping.end(), point_number.begin());