FFmpeg at your fingertips (updated on 2022/11/24)

A few years ago, I wrote an article " Technical house learns a few tricks for FFmpeg ". I still think this small article is very practical, and I will turn it out from time to time, copy the FFmpeg command line according to the usage scenario, run it directly or simply modify the parameters, and the task can be completed, which is very convenient! Sometimes, I feel that times have changed, and the usage scenarios covered in this article are too thin, so I urgently need to add it. After all, I also changed to a new working environment and encountered some new needs. Thus, there is this article.

We still classify according to usage scenarios, divided into video/image, audio, other tools, etc. I will continue to update the content of this article depending on the situation - remember to bookmark the link of this article!

one. video/image

Case 100】Starting from the most basic scene, extract a frame of image at the specified time point of the video file. The target file format is controlled by the extension, which can be .png, .jpg, .webp, etc.:

ffmpeg -ss 5 -i D:\input.mp4 -frames:v 1 D:\snapshot.png

If you need a thumbnail, or to save storage space, you need to scale the image. You can specify the width and height of the image you want through the scale filter, or specify only one width or height, and replace the other with "-1" to maintain the original aspect ratio of the image (to ensure that the image content is not distorted ) :

ffmpeg -ss 5 -i D:\input.mp4 -frames:v 1 -vf scale=120:-1 D:\snapshot.jpg

Note: -vf means simple filter. By "simple" I mean that the filter can only have one input and one output.

Case 101】Challenge, another kind of thumbnail that "needs both...": It is necessary to limit the width and height (such as 120x80) while maintaining the original aspect ratio of the image. The area outside the image content is used Black fill:

ffmpeg -ss 5 -i D:\input.mp4 -frames:v 1 -vf "scale='min(120/iw, 80/ih)'*iw:'min(120/iw, 80/ih)'*ih, pad=120:80:(ow-iw)/2:(oh-ih)/2:black" D:\snapshot.webp

To explain: we used two filters. First use the scale filter, iw and ih represent the width and height of the source image respectively, relative to the target image size 120x80, respectively calculate the scaling ratio of the width and height, and select the smaller ratio of the two to scale the original image. Then use the pad filter to fill: the first two parameters specify the width and height of the target image (think of it as a black canvas), and the next two parameters are used to specify the starting point for the actual image to be rendered on the "canvas" ( upper left corner) coordinates x:y, and the last parameter is the fill color. ow and oh refer to the width and height of the output image respectively, which are actually 120 and 80 in this example.

Case 102】How to extract a series of thumbnails from a video file at a certain interval (such as 10fps)?

ffmpeg -i D:\input.mp4 -r 10 -vf "scale='min(120/iw, 80/ih)'*iw:'min(120/iw, 80/ih)'*ih, pad=120:80:(ow-iw)/2:(oh-ih)/2:black" -y D:\Thumbnails\Pic-%05d.jpeg

[ Case 103 ] The video taken by the front camera of some mobile phones is upside down, and a horizontal flip is required:

ffmpeg -i D:\input.mp4 -vf hflip D:\flipped.mp4

"Buy one get one free", get a vertical flip: ffmpeg -i D:\input.mp4 -vf vflip D:\flipped.mp4

So, how to do horizontal and vertical flip at the same time? -vf can be followed by multiple filters, separated by commas, pay attention to add a pair of quotation marks:

ffmpeg -i D:\input.mp4 -vf "hflip, vflip" D:\flipped2.mp4

There is also a trick I learned before, which is summarized here: rotate 90 degrees (the radian value is positive for clockwise, and negative for counterclockwise), remember to swap the width and height of the output image:

ffmpeg -i D:\input.mp4 -vf "rotate=a='90*PI/180':ow=ih:oh=iw" D:\rotate90.mp4

Of course, the above processing for video files is also applicable to image files.

Case 104】With the popularization of smartphones and the popularity of UGC short video content, vertical videos can be seen everywhere now. If you want to use a horizontal version of the video in some scenarios, how can you cut out a piece from the vertical version of the video? The crop filter is here! For example, to center and capture an area that is as wide as the source video and has an aspect ratio of 16:9:

ffmpeg -i D:\input.mp4 -vf "crop=in_w:(in_w*9/16):0:(in_h-in_w*9/16)/2" D:\cutout.mp4

Case 105】Picture-in-picture (or overlaying another video or picture on top of one video) is a common application, and it is very simple to implement, just use the overlay composite filter . I found a good article by chance , which mentioned the implementation of superimposing a timer on the video. I think it is very clever, so I will also record it here. His idea is divided into two steps: first cut out a timer video from a video with a time stamp, and then superimpose the timer video on the main video. Let's walk through it again.

Step 1: Using a video source called testsrc, the output video looks like this:

(Execute ffplay -f lavfi -i testsrc to play it and you will know...)

The starting coordinates of the timestamp can be located at (224, 94), the width of the cropped two-digit image is 61, the height is 52, and a 30-second timer video timer.mp4 is generated. The complete instruction is as follows:

ffmpeg -f lavfi -i testsrc -vf crop=61:52:224:94 -t 30 D:\timer.mp4

Step 2: If you just want to simply overlay on the upper left corner of the main video, do this:

ffmpeg -i D:\input.mp4 -i D:\timer.mp4 -filter_complex overlay D:\output1.mp4

If you want to scale the timer video and then superimpose it, such as enlarging the width to 80 (specifying the height as -1 can maintain the aspect ratio of the original image), you need to use a simple filter called movie (by the way, practice the position of the superimposition to the lower left corner), as follows:

ffmpeg -i D:\input.mp4 -vf "movie=timer.mp4, scale=80:-1[tm]; [in][tm]overlay=0:H-h" D:\output2.mp4

Note : The filename parameter behind the movie cannot have a file path, so in order for ffmpeg to find the resource smoothly, the working directory of the console must be set to the place containing timer.mp4.

Finally, I would like to add that a more perfect solution should be to first execute ffprobe -i D:\input.mp4 to check the duration of the main video, then make a corresponding timer video of equal duration, and then do video overlay.

Case 106】It was found that the 60fps video was directly stuck when playing at double speed on some mobile phones. It is estimated that the frame rate is too high and the CPU is too busy! At this time, you can try to reduce the frame rate by -r.
ffmpeg -i D:\input.mp4 -r 30 D:\out30fps.mp4

Assuming that the H.264 encoder is used, the image quality can also be adjusted through profile and level. The optional profiles are: baseline, extended, main, high, and the bit rate is controlled by level ( here is a more detailed comparison table ). According to different application fields, the baseline profile is mostly used in the real-time communication field, the main profile is mostly used in the streaming media field, and the high profile is mostly used in the broadcasting and storage fields. Also note that some low-end or earlier devices can only decode baseline.
ffmpeg -i D:\input.mp4 -profile:v baseline -level 3.0 D:\output1.mp4
ffmpeg -i D:\input.mp4 -profile:v main -level 4.2 D:\output2.mp4
ffmpeg -i D :\input.mp4 -profile:v high -level 5.1 D:\output3.mp4

two. audio

[Case 200] The data format accepted by the Matlab algorithm in the laboratory may be limited to .wav, and the material provided by the customer may be in any compression format, which requires a conversion:

ffmpeg -i D:\input.m4a D:\output.wav

So simple, the data format can be controlled by the extension of the output file.wav. Of course, if you want to generate .mp3 or .ogg compression format, just change the output file name to output.mp3 or output.ogg in the same way. If the obtained material is a video file and you only want to extract the audio, the usage is similar (-vn means discarding video data, this parameter is optional):

ffmpeg -i D:\input.mp4 -vn D:\output.wav

[Case 201] Common audio sampling frequencies are 11025Hz, 22050Hz, 24000Hz, 44100Hz (CD quality), 48000Hz, 96000Hz, etc. How to make a change?

ffmpeg -i D:\input.m4a -ar 48000 D:\output.wav

[Case 202] What if the material provided by the customer is 5.1-channel, but we can only process mono or dual-channel?

ffmpeg -i D:\input.m4a -ac 1 D:\output.wav

If the output is a pure PCM data file, you need to use -f to limit the format:

ffmpeg -i D:\input.m4a -ac 1 -acodec pcm_s16le -f s16le D:\output.pcm

[Case 203] Common sampling precision/bit depths include 8 bits, 16 bits, 24 bits, 32 bits, etc., that is, how many Bits are used to represent each sampling point of a single channel. If you want to change the bit depth, you need to use -acodec to specify the encoder (if you want to check which encoders are currently available, see Case 300), the optional values ​​are: pcm_u8, pcm_s16le, pcm_s24le, pcm_s32le, etc., where "u" Indicates an unsigned number, "s" indicates a signed number, and "le" indicates little-endian (that is, a multi-byte data format with low byte first):

ffmpeg -i D:\input.m4a –acodec pcm_s24le D:\output.wav

Note: -acodec is equivalent to -c:a.

【Case 204】 How to change the volume? Use -af audio filter, volume=1 means normal volume, =0.5 means change to half, =2 means change to 2 times. The value of volume can also bring the unit of dB. A positive number means that the volume increases by xxdB, and a negative number means that the volume decreases by xxdB:

ffmpeg -i D:\input.m4a -af volume=2 D:\output.wav

ffmpeg -i D:\input.m4a -af volume=-2dB D:\output.wav

[Case 205] If the target file is a compressed format, we may also want to use -ab to specify its code rate/bitrate (CBR-constant code rate) to control the sound quality (the higher the code rate, the better the sound quality):

ffmpeg -i D:\input.m4a -ab 128k D:\output.mp3

Note: -ab is equivalent to -b:a.

Or you can use -aq (depending on the specific encoder, VBR-variable bit rate, the smaller the value, the higher the bit rate):

ffmpeg -i D:\input.m4a -aq 2 D:\output2.mp3

【Case 206】 How to combine multiple audio files end to end into one? Suppose there are four source files, namely input1.m4a, input2.wav, input3.mp3, input4.mp4 (in any format), and then use -filter_complex composite filter (-lavfi is its equivalent form), "concat "The previous [0:0] indicates the 0th output of the first input source (note: the index starts from 0, for the source file with multiple outputs, the 0th output is the video stream, and the 1st output is the audio stream), [1:0] means the 0th output of the second input source, and so on; n=4 after "concat" means that a total of 4 streams participate in the synthesis, v=0 means discarding video data, a= 1 means to synthesize one audio stream:

ffmpeg -i D:\input1.m4a -i D:\input2.wav -i D:\input3.mp3 -i D:\input3.mp4 -filter_complex "[0:0][1:0][2:0][3:1]concat=n=4:v=0:a=1" –ab 128k –y D:\concat.mp3

Imagine this picture:

【Case 207】 Continue to discuss the scene of multi-file mixing, what if what we want is not end-to-end connection, but sound mixing? Then use the amerge composite filter:

ffmpeg -i D:\input1.m4a -i D:\input2.wav -filter_complex "[0:0][1:0]amerge=inputs=2" –ab 128k D:\merge.mp3

It should be noted that the length of the synthesized file is based on the shorter one - this is a bit unacceptable! A "stupid" way is to use -stream_loop to loop the output of the shorter input file, like this:

ffmpeg -stream_loop 2 -i D:\input1.m4a -i D:\input2.wav -filter_complex "[0:0][1:0]amerge" –ab 128k -y D:\merge.mp3

Very awkward! Let's take a look at the more powerful amix filter, which can specify the duration of the target file through duration (optional values ​​are longest, shortest, first), and can also specify the mix of each stream through weights Weight (both are 1/N by default):

ffmpeg -i D:\input1.m4a -i D:\input2.wav -i D:\input3.mp3 -filter_complex "[0:0][1:0][2:0]amix=inputs=3:duration=longest:weights='0.1 0.1 0.8'" –ab 128k D:\mix.mp3

【Case 208】 Continue to in-depth discussion: When mixing, is it possible to specify the start time on the timeline for each sound effect file? The answer is yes. This time we take the longest main audio file as the first input source, then delay the start of the second sound effect file by 5 seconds through the adelay filter , and delay the third sound effect file to the start of the 30th second (delays parameter value, When there is no unit, the default is milliseconds, the lowercase s means seconds, and the uppercase S means the number of sampling points; the delay time can be specified for each channel separately, or the same delay can be specified for all channels through all=1):

ffmpeg -i D:\input1.m4a -i D:\input2.wav -i D:\input3.mp3 -filter_complex "[1:a]adelay=delays=5s:all=1[a1]; [2:a]adelay=delays=30s:all=1[a2]; [0:a][a1][a2] amix=inputs=3:duration=first:weights='0.6 0.2 0.2'" –ab 128k -y D:\mix.mp3

It is worth noting that this time we changed a labeling method: [1:a] indicates the audio output of the second input source, and after adelay processing, the output stream is marked as [a1]; [2:a] indicates the audio output of the second input source. The audio output of the three input sources, after adelay processing, marks the output stream as [a2]; then the audio output stream [0:a] of the first input source is fed to the amix filter together with [a1] and [a2] Mix it up. The duration of the target file is based on the first input source. The mixing ratio of each stream is 60%, 20%, and 20% respectively (the sum is equal to 100%).

[Case 209] For a stereo audio stream, clear (mute) the data of one of the channels, and the output is still stereo.
Mute the right channel: ffmpeg -i D:\input.wav -af "pan=stereo|c0=c0" D:\right_muted.wav
Mute the left channel: ffmpeg -i D:\input.wav -af " pan=stereo|c1=c1" D:\left_muted.wav

【Case 210】 How to extract one channel data from a stereo audio stream, and then generate a mono audio file? Could have used -map_channel, but this parameter is deprecated. The official website recommends using the pan filter . The usage is as follows:
only the left channel is taken: ffmpeg -i D:\input.wav -af "pan=1c|c0=c0" D:\mono_left.wav
only the right channel is taken: ffmpeg - i D:\input.wav -af "pan=1c|c0=c1" D:\mono_right.wav
rain and dew (left 90% + right 10%): ffmpeg -i D:\input.wav -af "pan= 1c|c0=0.9*c0+0.1*c1" D:\mono_right.wav
​​​​​​​​Note
: Compared with Case 202, -ac 1 will automatically perform multi-channel mixing.

【Case 211】 It is easy to use the showwavespic filter to draw the waveform of an audio file . Use the s parameter to specify the width and height of the output image (image width affects the density of the waveform); the value of split_channels is 1, which means that each channel is drawn separately, and the value of 0 means that all channels are overlapped and drawn together: ffmpeg -i D
: \input.m4a -filter_complex showwavespic=s=1280x720:split_channels=1 D:\waveform.png

three. Other tools

[Case 300] View the codec and file container format that FFmpeg comes with:

ffmpeg –codecs
ffmpeg –decoders
ffmpeg –encoders
ffmpeg –formats

【Case 301】 Sometimes I need to record the screen, but I am too lazy to install a third-party screen recording software. Can be like this:

ffmpeg -f gdigrab -i desktop D:\screenshots.mp4

Note: Press the "Q" key or Ctrl+C to stop recording.

[Case 302] Based on the above screen recording command, add a text watermark in the lower right corner of the video:

ffmpeg -f gdigrab -i desktop -vf drawtext="fontsize=80:fontcolor=white:text='TEST':x=(w-text_w-10):y=(h-text_h-10)" -y D:\screenshots.mp4

[Case 303] Based on the above screen recording command, add a picture watermark to the upper right corner of the video:

ffmpeg -f gdigrab -i desktop -i D:\logo.png -filter_complex "[0:0][1:0]overlay=x=main_w-overlay_w-10:y=0" -y D:\screenshots.mp4

【Case 304】 Collect a video through the built-in camera of the laptop. The name of the camera may not always be "Integrated Camera", it may be different for different models, you can check it under "Device Manager | Camera" in the Windows system (or execute ffmpeg -list_devices true -f dshow -i dummy ):

ffmpeg -f dshow -i video="Integrated Camera" D:\capture.mp4

Note: Press the "Q" key or Ctrl+C to stop the collection.

========
Official website information:

http://ffmpeg.org/ffmpeg.html#Audio-Options

 http://ffmpeg.org/ffmpeg.html#Video-Options

http://ffmpeg.org/ffmpeg-filters.html

Guess you like

Origin blog.csdn.net/happydeer/article/details/125868830