Ffmpeg reference

From Phonlab
Jump to navigationJump to search

ffmpeg is a command line tool for working with video files. In the Lab we use it to automate the creation of video stimulus files--adding features such as lead-in still frames or replacing the audio track with processed audio.

Note that some commands are not compatible with older versions of ffmpeg. These are know to work with version 1.0.7.

Visit the ffmpeg site for full documentation of the tool.

Extracting video

To extract video from a certain time, use the -ss and -t flags to specify start time and duration, respectively:

ffmpeg -ss HH:MM:SS[.CS] INFILE -t DURATION OUTFILE

This command, for example, would extract a 1.5-second clip starting at 5.3 seconds into 'longfile.avi' and save it to 'shortfile.avi':

ffmpeg -ss 00:00:05.30 longfile.avi -t 1.5 shortfile.avi


Here's one strategy for extracting video from MXF files that has worked:

1) convert to a format that has a small GOP (group of pictures) value -g 1 and keep a high video bit rate (-b:v) to keep detail in the video:

ffmpeg -i my.MXF -g 1  -b:v 35000k temp.MXF

2) seek in the temp.MXF file to just before your target start time for the extracted video - the first seek (ss) in this command jumps to 1 second before the target very quickly and the second seek steps over frames until it gets to exactly your target time. Using a small GOP value in step one makes this quick jump possible:

ffmpeg -ss target-1 -i temp.MXF -ss 1 -q 2 -t duration_of_chunk out.mpg

Bitrate and MPEG compression

Set the bitrate of the output file with the -b:v argument. Add this argument somewhere in the command, set off by spaces--for example, right before specifying the output file. Set it closer to the original file's bitrate to reduce the amount of compression.

-b:v 30000k

Unfortunately, this parameter is not available in older versions of ffmpeg; you can use -vcodec copy to copy over the video from the original file without re-encoding it. This will override any encoding-related settings.

-c:v copy     # for newer versions of ffmpeg only
-vcodec copy  # for all versions of ffmpeg

The -q parameter can also be used to control the quality of the video output. Set -q 2 to get a pretty high quality video bitrate.

Changing the video resolution (scaling)

Select the output video resolution with the video filter parameter:

-vf scale=WIDTH:HEIGHT

Note that the video image will be stretched horizontally or vertically if the output width/height ratio is different from the ratio in the input file.

Avoiding audio compression

Many compressed video outputs will also include compressed audio by default. Since the normal goal is to use movies in a listening experiment, we usually want to avoid making unintentional changes to the audio channels when processing video files. In particular, we want to avoid applying lossy compression (perhaps multiple times) when applying MPEG compression.

To ensure that audio channels are simply copied without additional compression from input to output video files, specify copy as the output audio codec.

-c:a copy    # for newer versions of ffmpeg only
-acodec copy # for all versions of ffmpeg

The copy codec tells ffmpeg to skip the default compression (when converting to MPEG or another format with default audio compression) and to pass through the audio with the same format it has in the input file.


Add text to a video

At one point we wanted to add text to a video - creating catch trials in which subjects were instructed 'do not respond' so we can see that they have their eyes open during the experiment! The 'drawtext' filter will do this.

ffmpeg -i input.mpg -vb 100000k -y -vf drawtext="fontfile=/usr/share/fonts/truetype/freefont/FreeSans.ttf:text='Do not respond':fontsize=70:box=0:x=800:y=700:fontcolor=yellow" output.mpg

Note that we have to refer to a specific font file, and the location of the font may differ from computer to computer.