FFmpeg is a powerful and extensible open-source software tool designed for audio and video processing. It allows us to manipulate, convert, transform, apply filters, among a large number of multimedia formats efficiently and accurately.
FFmpeg provides a command-line interface for performing complex tasks programmatically. And there are many many many (many!) possible combinations.
Of course, I never remember most commands, so I have my cheat sheet. I share it with you in case it is useful to you, for those of us with a fish memory 🐠. I hope it is useful for you!
To obtain information
Get system information: Get available codecs in FFmpeg.
ffmpeg -codecs
ffmpeg -formats
Get video information: Show detailed information about a video file, such as duration, resolution, audio and video codecs.
ffmpeg -i video.mp4
Get information without copyright banner: Display information of a video file without showing the copyright banner.
ffmpeg -i video.flv -hide_banner
Save information to a json file: Save detailed information of a video file in JSON format.
ffprobe -i video.flv -print_format json -hide_banner
To convert videos
Convert between video formats: Convert a video file from one format to another.
ffmpeg -i video_original.avi output.mp4
Recompress video: Recompress a video using the libx264 codec to improve quality and reduce file size.
ffmpeg -i youtube.flv -c:v libx264 filename.mp4
Concatenate multiple videos: Merge multiple video files into a single one without the need to re-encode them.
ffmpeg -i "concat:video1.mp4|video2.mp4|video3.mp4" -c copy output.mp4
Extract video segment: Cut a specific segment from a video without re-encoding.
ffmpeg -i video.mp4 -ss 00:01:30 -t 00:00:30 -codec copy output.mp4
Change video speed: Adjust the video speed by changing the frames per second.
ffmpeg -i video.mp4 -filter:v "setpts=0.5*PTS" output.mp4
To transform videos
Scale video: Change the resolution of a video while maintaining the aspect ratio.
ffmpeg -i video.mp4 -vf scale=640:360 output.mp4
Proportional scale video: Adjust the height of a video while maintaining the original aspect ratio.
ffmpeg -i video.mp4 -vf scale=320:-1 output.mp
Rotate video: Rotate a video clockwise.
ffmpeg -i video.mp4 -vf transpose=clock output.mp4
Flip video: Flip a video horizontally or vertically.
ffmpeg -i video.mp4 -vf hflip output.mp4
ffmpeg -i video.mp4 -vf vflip output.mp4
Crop video: Crop a video to a specific resolution and position.
ffmpeg -i video.mp4 -filter:v "crop=640:480:200:150" output.mp4
Where
- 640:400, width and height
- 200:150, X, Y coordinates of the crop
Add watermark: Add a logo as a watermark to a video.
ffmpeg -i video.mp4 -i logo.png -filter_complex overlay=10:10 output.mp4
To manage audio
Volume
Increase audio volume: Increase the volume of a video.
ffmpeg -i video.mp4 -af 'volume=0.5' output.mp4
Mute video: Remove the audio track from a video.
ffmpeg -i video.mp4 -an output.mp4
Add or remove tracks
Extract audio from a video: Extract the audio track from a video file.
ffmpeg -i video.mp4 -vn output.mp3
Add audio track: Add an audio track to an existing video.
ffmpeg -i video.mp4 -i audio.mp3 -map 0:v -map 0:a -map 1:a -vcodec copy output.mp4
Conversion and resampling
Convert between audio formats: Convert an audio file from one format to another.
ffmpeg -i audio.mp3 -acodec pcm_s16le output.wav
Trim audio: Trim an audio segment without re-encoding.
ffmpeg -i audio.mp3 -ss 00:01:30 -t 30 -acodec copy output.mp3
Recompress audio: Recompress an audio file with a bit rate of 128.
ffmpeg -i audio.mp3 -ab 128 output.mp3
Resample audio: Change the sampling frequency of an audio file.
ffmpeg -i audio.mp3 -ar 16000 output.mp3
Image related
Extract images from video
Extract frames from a video: Extract a specific frame from a video.
ffmpeg -i video.mp4 -ss 00:00:30 -vframes 1 output.png
Extract images every 15 seconds (fps 1/15): Extract images from a video at specific intervals.
ffmpeg -i video.mp4 -vf "fps=1/15,select='not(mod(n,1))'" output_%d.png
Create a gif from a video: Convert a video to an animated GIF.
ffmpeg -i video.mp4 -vf scale=300:-1 -t 10 -r 10 output.gif
Create video from images
Combine images into a video: Combine static images into a video.
ffmpeg -i video.mp4 -r 0.25 output_%04d.png
Create a video with a static image and audio: Create a video with a static image and an audio file.
ffmpeg -loop 1 -y -i image.png -i audio.mp3 -shortest output.mp4
Concatenate images into video: Combine JPG images into a video.
cat *.JPG | ffmpeg -f image2pipe -r 0.3 -vcodec mjpeg -i – -vcodec libx264 output.mp4