5

How can I convert a video file (mpeg, for example) into a collection of images?

Ideal answer would cover both C++ and Java using available libraries, and also how to manually strip the individual frames out of a video file for some common video format.

Anonymous
  • 3,334
  • 3
  • 35
  • 50
  • Current answer by Don Reba is good and I will accept it unless someone provides a more thorough one. Clearly I can just look at ffmpeg's source code to see the details, but if there's a reference that's easier to read than raw source code that would be appreciated. Also, is there any library in Java that does this? – Anonymous Sep 19 '11 at 03:56

3 Answers3

6

To extract all frames losslessly, use

ffmpeg -i "$input_file" -f image2 "outdir/%05d.png"

If you prefer a different output format, just change .png; by default ffmpeg will infer the file type from the extension.

The option -f image2 tells ffmpeg to write to a series of images. The "outdir/%05d.png" gives a filename pattern, in this case "5-digit frame number.png".

If you only want to extract n frames per second, add the option -r n after "$input_file". (I think n can be floating-point.)

In the case that your video is Motion JPEG (mjpeg), instead use:

ffmpeg -i "$input_file" -vcodec copy -f image2 "outdir/%05d.jpg"

This unpacks the frames directly from the video stream, which is faster and obviously uses less disk space.

For more information/other options, see the man page or the documentation (search for image2).

Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
3

You can do this using ffmpeg with -vcodec png. For example, this would convert my_video.avi into frames with five-digit numbers:

ffmpeg -i my_video.avi -vcodec png frame%05d.png

You can also need to specify desired framerate and resolution. You can run ffmpeg -codecs to see all the available formats.

Don Reba
  • 13,814
  • 3
  • 48
  • 61
0

Probably you can use apiexample.c inside the ffmpeg's libavcodec library that will allow you to see how to take a frame buffer to encode it in the mpeg stream easily.

Alternatively you can use Gstreamer which is a much neater framework.

For Java, try to use Java Media Framework.

Dipan Mehta
  • 2,110
  • 1
  • 17
  • 31