Monday, February 6, 2012

ffmpeg: The video toolbox



ImageJ is rather limited to import various video formats. However, there is a very powerful toolbox for video conversions: ffmpeg.
 For beginners, this tool seems rather intimidating to use because it's only available as a command line (there is no graphical user interface). In this post, I'll show you some of the options needed to convert any video...


The best way to import videos in ImageJ without any information loss is to convert it in a series of images and then to import them as an image sequence.

Update Feb., 2 2017: In some Linux distributions,  ffmpeg is not available and is replaced by a fork of this project called avconv. This is exactly the same syntax as ffmpeg. Just replace in the command line ffmpeg by avconv.

1- Installing ffmpeg

1-1 Linux users
In a classical linux distribution, ffmpeg is already installed. Open a terminal (in Ubuntu, use the keys combination Ctrl + Alt + T), and type ffmpeg. If you see something like that ...

ffmpeg version 0.7.3-4:0.7.3-0ubuntu0.11.10.1, Copyright (c) 2000-2011 the Libav developers
  built on Jan  4 2012 16:21:50 with gcc 4.6.1
[...]

.....
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

ffmpeg is correctly installed, otherwise... install it with the command:

sudo apt-get install ffmpeg
1-2- Windows users
TODO
Download ffmpeg here [Link].

2- Converting a video into an image series

ffmpeg as many tools working in command line follows a specific syntax as shown below.


ffmpeg [options] [[input_file options] -i input_file]... {[output_file options] output_file}


Each option begins with a "-" (hyphen) followed by a character. Sometimes, just after the option, a value (a string or a number) is required  and must be separated by a space. For example:

 -i<space>myvideo.mp4 -ss<space>100

2-1- Getting some useful information of the video.
If you want the properties of your video, just type:

ffmpeg -i myvideo.mp4

And the features of the video is displayed.

  Metadata:
    duration        : 146
    starttime       : 0
    totalduration   : 146
    width           : 640
    height          : 480
    videodatarate   : 321
    audiodatarate   : 129
    totaldatarate   : 458
    framerate       : 30
    bytelength      : 8368098
    canseekontime   : true
    sourcedata      : xxxxxxxxxxxxxxxxxxxxxxxxxx
    purl            :
    pmsg            :
  Duration: 00:02:26.00, start: 0.000000, bitrate: 460 kb/s
    Stream #0.0: Video: h264 (Main), yuv420p, 640x480, 328 kb/s, 29.97 tbr, 1k tbn, 59.94 tbc
    Stream #0.1: Audio: aac, 44100 Hz, stereo, s16, 131 kb/s
At least one output file must be specified

Among all these parameters, some of them are important:
  • the duration in seconds (146 in this example) 
  • the framerate in frames per second or fps (30)
  • the frame size: width and height expressed in pixels (640x480). 

That gives you an idea of the number of frames in the video. In this example, the number of frames is:
146 x 30 = 4380 images of 640x480 (yes !!! you need space in your hard disk).
2-2- Extracting PNG images from the video
In this example, a video entitled myvideo.mp4 is located in the directory project. A subdirectory frames was created (as usual with our favorite OS) and the video 'll be converted into images located in frames directory and respectively named:
frame0001.png
frame0002.png
....

frame4380.png

Open a terminal, go to the directory project and type the following command line (i) to extract all the video frames and (ii) to save them in PNG format:


 What does it mean?
  • Option -i is the input video name
  • Option -f  is the output file format. Here image2 means that the output consists of individual images (each frame is converted in an image).

Then, the output destination is written according to this scheme:
directory/prefix<index_displayformatting>.extension

Note 1: The directory is optional but that's better to save the frames in a separate directory.
Note 2: From the extension (png, jpg, etc. ), ffmpeg automatically detects the output image file format.

%04d defines formatting rule of the frame index.
  • The percent sign '%' announces specific formatting rules [Link] (like printf in C language)
  • 'd' means that the index is an integer.
  • 4 corresponds to the maximum number of digits used to display the index.
  • 0 (zero) indicates that the index is filled by zero if it has less than four digits. Ex: 12 'll be displayed as 0012


Don't forget that 4380 images are extracted...

3- Tuning ffmpeg

In many cases, only a small part of the video is interesting and there is absolutely no interest to fill your hard disk of ten of  thousands of images. You have to cut (or edit) your video.
3-1 Start time
The first  interesting option of ffmpeg concerns the start time -ss
Here is an example of a conversion starting from the 120th second of the video.
ffmpeg -i myvideo.mp4 -ss 120  -f image2 frames/frame%04d.png
3-2 Duration time
The second interesting option of ffmpeg -usuallyu combined with -ss - concerns the duration time -t.
Here is an example of a conversion starting from the 120th second of the video during 10 seconds.
ffmpeg -i myvideo.mp4 -ss 120 -t 10  -f image2 frames/frame%04d.png
3-3 Skipping frames
If you want to skip frames, you have to modify the input framerate of the video. In our example, the video is recorded at a speed of 30 fps, if you want to skip one image, define a framerate of 15fps, this is possible with the option -r.

Here is an example...
ffmpeg -i myvideo.mp4 -ss 120 -r 15  -f image2 frames/frame%04d.png



Hope that helps...

<<  Previous: TOC  Next: Importing Images >>

3. Other crazybiocomputing posts

Further readings are available in ...
  • Video Series  [Link]
  • Image Processing TOC [Link]

No comments:

Post a Comment