Most Useful FFmpeg Commands

FFmpeg is a name of a free software project for the multimedia handling licensed under GNU General Public License. The most popular part of the project is ffmpeg command line tool for video and audio encoding/decoding and its main features are the high speed, quality of output and small file sizes. “FF” from FFmpeg means Fast Forward – a control button on media players, and “mpeg” is an abbreviation of the Moving Pictures Experts Group

In this artical we will know the FFmpeg most useful command that usually we use in our FFmpeg command

Resizing video

The width and height of the output video can be set with -s option preceding the output filename. The video resolution is entered in the form wxh, where w is the width in pixels and h is the height in pixels. For example, to resize the input from initial resolution to 320×240 value, we can use the command

ffmpeg -i input_file -s 320×240 output_file

ffmpeg
Resizing video

Cropping Video

To crop the video means to select wanted rectangular area from the input to the output without a remainder. Cropping is often used with resizing, padding and other editing

The value of ow can be derived from oh and vice versa, but cannot be derived from x and y, because these values are evaluated after ow and oh. The value of x can be derived from the value of y and vice versa. For example to crop the left third, middle third and right third of the input frame we can use the commands

ffmpeg -i input -vf crop=iw/3:ih:0:0 output
ffmpeg -i input -vf crop=iw/3:ih:iw/3:0 output
ffmpeg -i input -vf crop=iw/3:ih:iw/3*2:0 output

Cropping Video

Padding Video

To pad the video means to add an extra area to the video frame to include additional content. Padding video is often needed, when the input should be played on a display with a different aspect ratio

ffmpeg -i photo.jpg -vf pad=860:660:30:30:pink framed_photo.jpg

Padding Video

Flipping and Rotating Video

Flipping and rotating of the video frame are common visual operations, that can be used to create various interesting effects like mirrored versions of the input

Horizontal flip

A horizontally mirrored video version – horizontal flip is created with a hflip filter

ffplay -f lavfi -i testsrc -vf hflip

Vertical flip

ffplay -f lavfi -i rgbtestsrc -vf vflip

Rotation by 90 degrees counterclockwise and flip vertically

ffmpeg -i CMYK.avi -vf transpose=0 CMYK_transposed.avi

Rotation by 90 degrees clockwise

ffmpeg -i CMYK.avi -vf transpose=1 CMYK_transposed.avi

Blur video effect

A blur effect is used to increase the quality of certain type of noise in images (video frames), where each output pixel value is calculated from the neighboring pixel values. For instance, the blur effect can improve images scanned from a printed half-tone pictures. To blur an input video we can use a boxblur filter

For example to create a blur effect on the input video where luma radius value is 1.5 and luma power value is 1, we can use the next command

ffmpeg -i input.mpg -vf boxblur=1.5:1 output.mp4

Sharpen video

The sharpen filter can be used as a common unsharp mask and a Gaussian blur. For example to sharpen the input with default values we can use the command

ffmpeg -i input -vf unsharp output.mp4

Command structure for overlay

The structure of the command for video overlay is below, input1 is the video background and input2 is the foreground

ffmpeg -i input1 -i input2 -filter_complex overlay=x:y output

Command structure for overlay

Logo in one of corners

To keep the content visible, logo is often placed in one of four corners on the screen. The next 4 examples use the pair.mp4 video as the first input that contains a wedding pair and the second input is a red heart containing the text M+P (for example, Mary and Peter). The video resolution is 1280×720 pixels and the logo size is 150×140 pixels, but we do not need this sizes to calculate the logo position. The proper location of the logo’s top-left corner (x and y coordinate) is derived from the width and height values of the background and foreground

W, H – width and height of background (video)
w, h – width and height of foreground (logo)

Logo in top-left corner

ffmpeg -i pair.mp4 -i logo.png -filter_complex overlay pair1.mp4

Logo in top-left corner

Horizontal text movement on Video

To move the text horizontally across the video frame, we include the t variable to the expression for the x parameter, for instance to move provided text every second by n pixels in a right-to-left direction, we use an expression x=w-tn. To change the movement to the left-to-right direction, x=w+tn expression is used. For example, to show “Dynamic RTL text” string moving at the top, we use the command

ffmpeg -f lavfi -i color=c=#abcdef -vf drawtext=^ “fontfile=arial.ttf:text=’Dynamic RTL text’:x=w-t*50:^ fontcolor=darkorange:fontsize=30” output

Horizontal text movement on Video

Vertical text movement

Text scrolling vertically from the bottom to the top is often used at the end of the video to show the name of the producer, actors, date, etc. To move the text vertically, the t variable is included to the expression for the y parameter, for instance to move provided text every second by n pixels from top to bottom, we use an expression y=tn. To scroll from bottom to top, y=h-tn expression is used. The next command shows the content of the Credits file moving from bottom up with the speed 100 pixels per second. The content of the Credits file including the spaces at the line beginning is

ffmpeg -i palms.avi -vf drawtext=”fontfile=arial.ttf:textfile=Credits:^ x=(w-tw)/2:y=h-t*100:fontcolor=white:fontsize=30″ clip.mp4

Overwriting same named output files

If the file with the specified name in ffmpeg command already exists, the console asks for “y” (yes) or “n” (no) to overwrite the old file. To avoid this question, -n option can be used to cancel processing and -y option is used to set the overwriting without asking. For example to overwrite the old output file by default

we can use the command:

ffmpeg -y -i input.avi output.mp4

Duration of audio and video

ffmpeg -i music.mp3 -t 180 music_3_minutes.mp3

Metadata and Subtitles

Metadata are often used in MP3 files and media players commonly display from them items like the song title, artist, album, etc. For example, to display the metadata from the file Kalimba.mp3, located in a Sample Music folder on Windows 7 (users with other OS can select other media file with metadata that are always present in officially distributed music and video) we can use the command

ffplay -i “/Users/Public/Music/Sample Music/Kalimba.mp3”

Screenshots from videos

ffmpeg -i input -ss t image.type

Example

The -ss option can be used also before the input file, but the result is less accurate. For example to take a screenshot in the time 1 hour 23 minutes 45 seconds from the file videoclip.avi, we can use the command

ffmpeg -i videoclip.avi -ss 01:23:45 image.jpg

Conversion between image types

Almost all supported image types can be converted one to another, exceptions are EXR, LJPEG, PIC and PTX file types that can be decoded only. The syntax for the conversion is

ffmpeg -i image.type1 image.type2

For example, to convert a PNG image to the JPG image format, we can use the command

ffmpeg -i illustration.png illustration.jpg

Creating video from images

Video from one image

To convert a still image to a video is easy and can be used to create slideshows, where short videos from images (with added text) are joined together, joining videos is described in the chapter 23. For example to create a 10-second video from the photo.jpg file, we include a -loop boolean option with a value true or 1 like in the command

ffmpeg -loop 1 -i photo.jpg -t 10 photo.mp4

Video from many images

To create a video from multiple images, their filenames must end with a number, where these numbers correspond with the sequence in which the images will be encoded to the video file. In this case the media format is specified before the input and it is an image2 format. For example, from 100 images named img1.jpg, img2.jpg, …, img100.jpg can be created a 4-second video with 25 fps frame rate using the command

ffmpeg -f image2 -i img%d.jpg -r 25 video.mp4

If the image numbers start with zeros, for example img001.jpg, img002.jpg, etc. to provide the same filename length, then the command is

ffmpeg -f image2 -i img%3d.jpg -r 25 video.mp4

The number after the % sign must be the same as the number of digits in the image filenames

Split a video into images

$ ffmpeg -i video.flv image%d.jpg

Convert video into animated gif

$ ffmpeg -i video.flv animated.gif.mp4

Convert mpg video file to flv

$ ffmpeg -i video.mpg -ab 26k -f flv video1.flv

Extract audio from video file

$ ffmpeg -i video.mpg -ab 26k -f flv video1.flv

Add subtitles to a Movie

$ ffmpeg -i video.mp4 -i subtitles.srt -map 0 -map 1 -c copy -c:v libx264 -crf 23 -preset veryfast video-output.mkv

Leave a Comment