Collection of useful ffmpeg commands for processing audio and video files.
FFMPEG is an extremely useful library for manipulating audio and video files. It is free and available for Windows, Mac and Linux.
Here are some ffmpeg commands that I use frequently in my work (stored here so that I do not lose them). Some of the commands have been lifted directly from other sources (mostly from stack overflow), others have been modified. Some include simple bash programming so that they can be applied to an entire folder at a time.
Disclaimer: The examples here often do not explain all the different option flags available. If you require a full thorough list, please read the documentation here, or use the terminal command man ffmpeg-all
.
This list will be updated with more commands as and when I use them. Contributions are welcome.
To display media file details (audio/video file information), run
ffmpeg -i video.mp4
To check the list of supported formats by FFmpeg, run
ffmpeg -formats
If you have ffplay installed, you can run
ffplay video.mp4
to preview or test video or audio files in the terminal itself. On a mac, run brew install ffmpeg --with-sdl2
to install ffmpeg with ffplay.
This command will extract the video frame at the 15s mark and saves it as a 800px wide JPEG image. You can also use the -s switch (like -s 400×300) to specify the exact dimensions of the image file though it will probably create a stretched image if the image size doesn’t follow the aspect ratio of the original video file.
ffmpeg -ss 00:00:15 -i video.mp4 -vf scale=800:-1 -vframes 1 image.jpg
Extracts frames at a frame rate of 1 frame per minute. Most TV shows and other videos are interlaced, this command includes the deinterlacing flag. By default ffmpeg tries to use as many threads as it can, the -threads flag can be set to 1 to force it to only use a single thread.
ffmpeg -i video.mp4 -threads 1 -deinterlace -q:v 1 -vf fps=1/60 frame%03d.jpg
Bash script to do it for an entire dataset assuming the folder structure has two levels:
#!/bin/bash
for file in videos/*; do
for file2 in "$file/"*.avi; do
destination="frames/$(basename $file)/$(basename $file2)";
mkdir -p "$destination";
ffmpeg -i "$file2" -vf "scale=-1:300,fps=25" "$destination/%03d.jpg";
done
done
You can use the -vcodec parameter to specify the encoding format to be used for the output video.
ffmpeg -i youtube.flv -c:v libx264 filename.mp4
If you want to speed up the process you can force a preset (however this will degrade the quality of the output video).
ffmpeg -i video.wmv -c:v libx264 -preset ultrafast video.mp
for i in *.avi;
do name=`echo $i | cut -d'.' -f1`;
echo $name;
ffmpeg -i "$i" -vf yadif "${name}.mp4";
done
You can use the time offset parameter (-ss) to specify the start time stamp in HH:MM:SS.ms format while the -t parameter is for specifying the actual duration of the clip in seconds.
ffmpeg -i input.mp4 -ss 00:00:50.0 -codec copy -t 20 output.mp4
ffmpeg -f concat -i file-list.txt -c copy output.mp4
This command converts videos into animated GIFs. Use the scale filter to specify the width of the GIF, the -t parameter to specify the duration and -r to specify the frame rate (fps).
ffmpeg -i video.mp4 -vf scale=500:-1 -t 10 -r 10 image.gif
frame rate specified by the -r flag, -start_number to specify the fist frame of the desired output video. This commands creates a video with fps = 25.
ffmpeg -r 25 -start_number 9999 -i 000%05d.jpg -c:v libx264 -vf fps=25 -pix_fmt yuv420p out2.mp4
The -vn switch extracts the audio from a video, the -ab switch saves the audio as a 256kbps (bit rate) audio file.
ffmpeg -i video.mp4 -vn -ab 256 audio.mp3
Useful additional flags:
-vn – Remove video from the output file.
-ar – Set the audio frequency of the output file. Commonly used values: 22050, 44100, 48000 Hz.
-ac – Set the number of audio channels.
-ab – Set the audio bitrate.
Convert all wav files in a folder to single channel 16kHz.
for i in *.wav;
do name=`echo $i | cut -d'.' -f1`;
echo $name;
echo $i;
ffmpeg -i $i -acodec pcm_s16le -ac 1 -ar 16000 “${name}.wav";
done
You can call ffmpeg from within a python script using os.system
. This can make it easy to run through a bunch of filenames stored in a text file for example, in case you do not want to use bash. Here is an example to cut videos from a particular start time specified in a text file (in seconds). The text file format is:
PATH STARTTIME
import os
f = open("videolist.txt", "r")
duration = 10*60 #cut into 10 minute segments
for x in f:
input = x.split()
path = input[0]
name = 'chopped ' + input[0]
start = int(input[1])
os.system('ffmpeg -ss {} -t {} -i {} {}'.format(start,duration,path,name))
f.close()