1.What is ffmpeg
ffmpeg is not only an audio and video encoding and decoding tool, but also a set of audio and video encoding and decoding development kits. As a encoding and decoding development kit, it provides developers with a rich calling interface for audio and video processing.
ffmpeg provides encapsulation and decapsulation of a variety of media formats, including multiple audio and video encodings, streaming media with multiple protocols, multiple color format conversions, multiple sampling rate conversions, multiple code rate conversions, etc.; the ffmpeg framework provides multiple A rich set of plug-in modules, including encapsulation and decapsulation plug-ins, encoding and decoding plug-ins, etc.
FFmpeg is developed under the Linux platform, but it can also be compiled and run in other operating system environments, including Windows, Mac OS X, etc. This project was first initiated by Fabrice Bellard and was mainly maintained by Michael Niedermayer from 2004 to 2015. Many FFmpeg developers come from the MPlayer project, and currently FFmpeg is also placed on the server of the MPlayer project team. The name of the project comes from the MPEG video coding standard, with the "FF" in front of it standing for "Fast Forward". The FFmpeg encoding library can use GPU acceleration.
ffmpeg Chinese documentation
2.ffmpeg module:
libavformat
Used for the generation and parsing of various audio and video encapsulation formats, including functions such as obtaining information required for decoding to generate decoding context structures and reading audio and video frames.
libavcodec
Used for various types of sound/image codecs, including most commonly used codec formats in the multimedia field.
it is useless
Contains some public utility functions
ibswscale
Used for video scene scaling and color mapping conversion
libpostproc
for post effects processing
libavfilter
A general filter processing framework for audio, video, subtitles, etc., which can be used to do some audio and video processing, such as audio and video doubling, horizontal flipping, cropping, adding boxes, overlaying text, etc.
libavdevice
Can read data from multimedia devices on computers (or other devices), or output to designated multimedia devices
libswresample
Provides a high-level audio resampling API
3. Common command templates
3.1. Convert format
Convert video format, this command converts MP4 format video to AVI format.
ffmpeg -i input.mp4 output.avi
Convert audio format. This command converts audio in MP3 format to Ogg format.
ffmpeg -i input.mp3 output.ogg
Extract audio from video, this command extracts audio from the input video and saves it in AAC format.
ffmpeg -i input.mp4 -vn -acodec copy output.aac
Convert video to GIF, this command converts the input video into an animated GIF, first generates a color palette, and then applies it to the video.
ffmpeg -i input.mp4 -vf "scale=320:-1:flags=lanczos,palettegen" -y palette.pngffmpeg -i input.mp4 -i palette.png -filter_complex "scale=320:-1:flags=lanczos[x];[x][1:v]paletteuse" -y output.gif
Extract video frames, this command extracts one frame per second from the input video and saves these frames as an image file in PNG format.
ffmpeg -i input.mp4 -r 1 -f image2 output-%03d.png
Extract a certain frame. This command extracts a frame at the 30th second from the input video and saves these frames as image files in PNG format.
ffmpeg -i input.mp4 -ss 00:00:30 -frames:v 1 output-30.png
3.2. Modify video
Resize video, this command adjusts the resolution of the input video to 640x360 and saves the output in MP4 format.
ffmpeg -i input.mp4 -vf scale=640:360 output.mp4
Add Watermark, this command adds a watermark to the upper left corner of the input video and saves the output in MP4 format.
ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=10:10" output.mp4
Adjust the audio volume. This command will adjust the input audio volume to 2 times the original volume and save the output in MP3 format.
ffmpeg -i input.mp3 -af "volume=2" output.mp3
Compress video, this command compresses the input video to a resolution of 640x? video and encoded using the H.264 encoder. The compression quality is controlled by the CRF parameter. The smaller the value, the higher the compression quality. The Preset parameter controls the encoding speed. The smaller the value, the faster the encoding speed, but the compression quality may be reduced.
ffmpeg -i input.mp4 -vf "scale=640:-1" -c:v libx264 -crf 23 -preset veryfast -c:a copy output.mp4
Add subtitles, this command combines the input video with a subtitle file, encodes the subtitle file into mov_text format, and saves the output into MP4 format.
ffmpeg -i input.mp4 -i subtitle.srt -c:v copy -c:a copy -c:s mov_text -metadata:s:s:0 language=eng output.mp4
Rotate video, this command will rotate the input video 90 degrees counterclockwise and save the output in MP4 format.
ffmpeg -i input.mp4 -vf "transpose=1" -c:a copy output.mp4
Modify the frame rate of the video. This command modifies the frame rate of the input video to 30 frames per second, and then uses the H.264 encoder for encoding.
ffmpeg -i input.mp4 -r 30 -c:v libx264 -crf 23 -preset veryfast -c:a copy output.mp4
3.3. Splicing clips
Crop the video. This command starts cropping from the 10th second of the input video to 20 seconds, and then saves the cropped video in MP4 format.
ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:20 -c copy output.mp4
Merge multiple video files, this command will merge two video files in MP4 format into one file and save it as MP4 format.
ffmpeg -i "concat:input1.mp4|input2.mp4" -c copy output.mp4
Separate the audio and video streams of the video. This command saves the audio stream and video stream of the input video into MP3 and MP4 format files respectively.
ffmpeg -i input.mp4 -vn audio.mp3ffmpeg -i input.mp4 -an -vcodec copy video.mp4
Merge audio and video
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -strict experimental output.mp4
If the video already contains audio, you can also replace the audio in the video at this time, using the following command line.
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -strict experimental-map 0:v:0 -map 1:a:0 output.mp4
Splice audio files. This command splices two audio files in MP3 format into one file and saves it in MP3 format.
ffmpeg -i "concat:input1.mp3|input2.mp3" -acodec copy output.mp3
Trim the audio. This command starts trimming from the 30th second of the input audio to a length of 2 minutes, and then saves the trimmed audio in MP3 format.
ffmpeg -i input.mp3 -ss 00:00:30 -t 00:02:00 -acodec copy output.mp3