FFmpeg command line, from getting started with beginners to collecting dust - a complete set of functions, there is always what you need!

Study/Work/Summary and summarize documents and continuously update them. . . Please refer to the basic summary first 

1. Obtain video information, including network URL

ffmpeg -i input.mp4
ffmpeg -i http://xxx.com/videofiles/xxxx.flv

2. Separate audio and video streams

ffmpeg -i input_file -vcodec copy -an output_file_video // The video stream is encoded in the original format and the audio stream is disabled.
ffmpeg -i input_file -acodec copy -vn output_file_audio // The audio stream is in the original encoding format, and the video stream is disabled.

If you want to change the separated code stream part, then -codec must be accompanied by the encoding format alias supported by ffmpeg.

besides! The specific output package file suffix name needs to match the audio encoding. For example,
the flv package format supports h.264, flashvideo + aac/mp3/ac3, and you need to understand that mp3 is an audio encoding format: mpeg1--audio(layer1 ,2,3)
The mp4 encapsulation format supports almost all encoding formats of audio and video streams.

3. Extract h264 code stream with spspps

ffmpeg -i input.mp4 -vcodec copy -an -bsf: h264_mp4toannexb -f h264 output.264 //Extract 264 code stream, only ffplay -i can be used to play
ffmpeg -i input.mp4 -vcodec copy -an -bsf: h264_mp4toannexb -f mp4 output.mp4 //Extract the 264 code stream and encapsulate it into mp4, so that VLC and other video software can play it
// Note that there must be a space after -bsf!

// The h264 format is divided into: VCL(avc1), NALU(annexb)
// VCL(avc1): MP4 file, a file has only one header (sps, pps)
// NALU(annexb): A nalu comes with a spspps head

4. Convert mp4 to ts

ffmpeg -i ..\test\guide.mp4 -c copy -f mpegts ..\test\guide.ts //MP4 conversion ts
ffmpeg -i ..\test\test.h264 -vcodec copy -f mpegts output.ts //h264 video to ts video stream
ffmpeg -i ..\test\test.h264 -vcodec copy -f mp4 output.mp4 //h264 video to mp4

5. Change the video bit rate, frame rate, gop, width and height; number of audio channels and sampling rate

ffmpeg -i test.mp4 -b:v 640k output.flv       
ffmpeg -i test.mp4 -acodec copy -vcodec copy -f flv output.flv     
ffmpeg431 -i test.mp4    -s 176x144      -vcodec libx264      -r 25 -g 100      -acodec aac      -b 200k     -ac 1   -ab 32   -ar 8000   -f avi  output.avi

-s: Specify width and height (widthxheight: must and must be an even number, if it is not an even number, it will become an even number) -r
: Frame rate 25, 30, 50, 60
-g: gop image group, how many frames have a key frame
-b : Code rate, bps (bits per second)
-ac: Number of channels
-ar: Sampling rate, how many times per second (44.1KHz=44100, 48KHz=48000, 80KHz)
-ab: Number of sampling bits

6. Faststart conversion of MP4 format

ffmpeg -i http://xxxx/154202543368.m3u8 -c copy -movflags +faststart  output.mp4    //m3u8转mp4

/// In the encapsulation format of mp4, the header information is at the end of the file by default. In this way, when parsing MP4, the entire file must be read before normal parsing, which seriously affects the efficiency.
/// -movflags +faststart, put the header information of the mp4 file at the head of the file, so that it does not affect the efficiency, and it can also transfer the network stream to the local MP4 format during normal use. 

7. Generate video thumbnails

ffmpeg -i input_ test.mp4 -r 1 -f image2 output_ image-%03d.jpeg
ffmpeg -i ..\test\guide.mp4 -ss 8 -t 4     -s 320*240 ..\test\t1_image.gif

-r: 1 frame per second, -f: The output format is image2, output_image-%03d.jpeg formatted output image

-ss: start time from 8s, -t: last 4s (control the time value of -t within the display time of each frame, it is a static image, if it is greater than the display time of each frame, it is an animated image), -s : Output size 320*240

8. Merge thumbnails into videos

ffmpeg -f image2 -t 30 -r 1   -i ..\test\output_image-%03d.jpeg ..\test\out_compjpeg.mp4

-r: 1 frame per second, -t: 30 images, lasting 30s in total

9. Filter watermark, static layer overlay

Overlay technology is also called video overlay technology. Overlay video technology is widely used. Common examples include the TV station logo displayed in the upper right corner of the TV screen and the picture-in-picture function. Picture-in-picture means that there is a small playback window in a large video playback window, and different video contents in the two windows are played at the same time. Overlay technology involves two windows. The larger window is usually called the background window, and the smaller window is called the foreground window. Videos or pictures can be played in either the background window or the foreground window. Use the overlay filter in FFmpeg to achieve video overlay effects. See examples

/// -vf, video filter: simple filter
/// Note 1 : The third parameter of overlay, the default is 0, if 1 forces the color space to be rgb, but it is easy to cause problems, it is best to omit the third parameter
/ // Note 2 : -vf, codec cannot use copy, the encoding format needs to be respecified
//Upper right corner
ffmpeg -i guide.mp4 -vf "movie=logo.png[logo]; [in][logo] overlay=Ww: 10:0 [out]" output.mp4
//middle
ffmpeg -i guide.mp4 -vf "movie=logo.png[logo]; [in][logo]overlay=W/2-w/2:H/2 -h/2[out]" -vcodec libx264 -acodec aac output.mp4
//Lower right corner
ffmpeg -i guide.mp4 -vf "movie=logo.png[logo]; [in][logo]overlay=Ww:Hh [out]" -vcodec libx264 -acodec aac output.mp4 -y

// [logo] is the tag name of input logo.png, [in] is the tag name of input guide.mp4.

Syntax: overlay[=x:[[:rgb={0, 1}]]
Parameters x and y are optional and default to 0.
Parameter rgb parameter is also optional, its value is 0 (YUV) or 1 (RGB), and the default is the original input color format.
Parameter description
: _ The color space is set to RGB variable description: The following variables can be used in the expressions of x and y main_w or W Main input (background window) width main_h or H Main input (background window) height overlay_ w or w overlay input (foreground window) Width overlay_ h or h overlay input (foreground window) height






///You can also use another method -filter_complex overlay=parameter
ffmpeg -i guide.mp4 -i logo.png -filter_complex overlay=W/2-w/2:H/2-h/2 test1.mp4

10. Filter watermark, enter text/current time

ffmpeg -i guide.mp4 -vf "drawtext=fontfile=simhei.ttf:text='xsfdsx':x=100:y=10:fontsize=24:fontcolor=yellow:shadowy=2" drawtext.mp4 //Add
text Watermark command drawtext=key=value:key=value:key=value

● line_h, Ih
the height of each text line
Main_h, h, H
the input height
Main_w, w, W
the input width
n
the number of input frame, starting from 0 Which frame to start
● rand(min, max)
return a random number included between min and max Random number
● mod(a, b)
to find the remainder, a%b, 5%2== 1
● sar
The input sample aspect ratio. The aspect ratio of the input sample, the aspect ratio of the yuv pixel.
● t
timestamp expressed in seconds, NAN if the input timestamp is unknown timestamp, unit: seconds
● text_h, th
the height of the rendered text text The height of the text: pixels
● text_w, tw
the width of the rendered text The width of the text: pixels
● fontfile font style
● fontsize font size
● fontcolor font color, support #336699

// Display the current time in the upper right corner. For analysis, the colon of the parameter is displayed in Chinese characters.

ffmpeg guide.mp4 -vf drawtext="fontfile=D\\:/test/font/font.ttf:x=W-tw:y=10:fontcolor=black:fontsize=30:text='%{localtime\:%H\\\:%M\\\:%S}" -y output.mp4

// In the command line, if there is ":" in the v of k:v input in drawtext, you need to escape "\:"
// If the fontfile uses the absolute path D:\test\font\font.ttf, you need to escape it. into fontfile=D\\:/test/font/font.ttf, the slash\ in the path needs to be changed to a backslash/
// When writing code, you must pay attention to the escaping operation of double slash\\.

// The output format of localtime, no slash is required! ! !
// java code, pay attention to escaping
String strCmdLine = " ....ext='%{localtimne\\:%H\\\\\\:%M\\\\\\:%S}'... ";
// cpp, pay attention to escape
char strCmdLine[1024]= {0};
strcpy(strCmdLine, " ....xt='%{localtime\\:%H\\\\\\:%M\\ \\\\:%S}'... " );

11. Text marquee effect

Inherit the basis of drawtext above.

Left to right: n is the current image frame number mod (a, b) commas need to be escaped
ffmpeg -i ..\test\guide.mp4 -vf "drawtext=fontfile=simeittf:text='hello, Zhige Blog': x=(mod(2*n\,w+tw)-tw):y=10:fontcolor=#FF6600:fontsize=60" -f mp4 -y ..\test\lr_lamp.mp4right to left:
ffmpeg
- i ..\test\guide.mp4 -vf "drawtext=fontfile=simeittf:text='hello, Brother Zhi's blog':x=wt*w/10:y=10:fontcolor=#FF6600:fontsize=60" - f mp4 -y ..\test\lr_lamp.mp4

12. Text flashing display effect

Inherit the basis of drawtext above.

ffmpeg -i ..\test\guide.mp4 -vf "drawtext=fontfile=simeittf:text='hello,志哥博客':x=W-tw-50:y=10:enable=lt(mod(t\,5)\,2):fontcolor=#FF6600:fontsize=60" -f mp4 -y ..\test\flash.mp4

lt:letter than <
gt:greater than >
enable:available visualization

13. Video rotation

There are two ways to rotate. The first one is to add attributes to the configuration metadata. The original content of the video will not change in any way. The conversion efficiency is high, but the decoding and rendering efficiency is reduced.

// -90 顺时针
ffmpeg -i ..\test\guide.mp4 -metadata:s:v rotate="-90" -codec copy ..\test\meta_rotate_-90.mp4
// 90 逆时针
ffmpeg -i ..\test\guide.mp4 -metadata:s:v rotate="90" -codec copy ..\test\meta_rotate_90.mp4

 The second method is to directly change the display content of the input video, and the conversion process is slow; there is no change in the decoding and rendering efficiency after conversion.

// 0: Rotate 90 degrees counterclockwise and flip vertically
ffmpeg -i ..\test\guide.mp4 -vf "transpose=0" ..\test\transpose_0.mp4

// 1: Rotate 90 degrees clockwise
ffmpeg -i ..\test\guide.mp4 -vf "transpose=1" ..\test\transpose_1.mp4

// 2: Rotate 90 degrees counterclockwise
ffmpeg -i ..\test\guide.mp4 -vf "transpose=2" ..\test\transpose_2.mp4

// 3: Rotate 90 degrees clockwise and flip vertically
ffmpeg -i ..\test\guide.mp4 -vf "transpose=3" ..\test\transpose_3.mp4

// 4: Flip the video horizontally
ffmpeg -i ..\test\guide.mp4 -vf hflip guide_hflip.mp4

// 5: Flip the video vertically
ffmpeg -i ..\test\guide.mp4 -vf vflip guide_vflip.mp4

PS: Rotate 180 degrees with transpose=2, transpose=2

14. Things to note when extracting images from videos

ffmpeg -i test.avi -r 1 -f image2 image- %3d.jpeg
// -r 1 One frame per second, extract video images.
ffmpeg -ss 0:1:30 -t 0:0:20 -i input.avi -vcodec copy -acodec copy output.avi
// -ss start time, -t duration
// Start cutting at 10s and continue 15 seconds (this method sometimes encounters a black screen in the first frame of the video, that is, the cover is black when it is not played, because the key frame 1 is not located) ffmpeg -i test.mp4 -ss
10 -t 15 -codec copy cut.mp4
// Start cutting at 10s and last for 15 seconds (this method is an optimized method. This method of cutting video can solve the black screen problem in the first frame, but the cutting time may be slightly off. The loss is not accurate, but it should be within the error range)
ffmpeg -ss 10 -t 15 -i test.mp4 -codec copy cut.mp4
// Note: If the video file is large, 2GB, duration: 02: Before 00:00
: -i xxx.mp4 -ss 01:10:05 -t 00:15:20, large videos will be very slow because they need to decode the entire video and then locate the time point
after: -ss 01:10:05 -t 00:15:20 -i xxx.mp4, faster because it is seekto

15. Play audio and video backwards, accelerate and decelerate playback

// The video is played backwards and the audio remains unchanged
ffmpeg -i ..\test\guide.mp4 -vf reverse ..\test\guide_v_reverse.mp4

// The audio is played backwards and the video remains unchanged
ffmpeg -i ..\test\guide.mp4 -c:v copy -af areverse ..\test\guide_a_reverse.mp4

// Play audio and video in reverse at the same time -preset superfast fast encoding, video quality is reduced
ffmpeg -i ..\test\guide.mp4 -vf reverse -af areverse -preset superfast ..\test\guide_av_reverse.mp4

// Slow down playback, pts is increased to 2 times, and the sound is 0.5 times the original speed
ffmpeg -i ..\test\guide.mp4 -vf setpts=PTS*2 -af atempo=0.5 ..\test\guide_speed0.5 .mp4

// Accelerate playback, pts is shortened to 1/2 of the original, and the sound is 2 times the original speed
ffmpeg -i ..\test\guide.mp4 -vf setpts=PTS*0.5 -af atempo=2 ..\test\guide_speed2. mp4

16. Video splicing

The method I know is a bit stupid, please tell me if you know of a more convenient method!

Use ts stream as the medium, because ts stream can be concatenated, first encapsulate mp4 into ts, then use the concate command to merge the ts stream, and finally splice the ts stream into mp4.

// Content review, -vbsf h264_mp4toannexb, bring sps and pps before the I frame

ffmpeg -i 10s.mp4 -vcodec libx264 -acodec aac -vbsf h264_mp4toannexb 10s.ts

ffmpeg -i guide.mp4 -vcodec libx264 -acodec aac -vbsf h264_mp4toannexb guide.ts

ffmpeg -i "concat:..\test\10s.ts|..\test\guide.ts" -acodec copy -vcodec copy -absf aac_adtstoasc ..\test\concat_10s_guide.mp4

When converting AAC code streams to some other formats, special configuration of the code stream filter is required (-absf aac_adtstoasc)

When encapsulating the original code stream (ADTS header + ES stream) encoded by the AAC encoder into MP4, FLV or MOV formats, you need to convert the ADTS header to MPEG-4 AudioSpecficConfig (extract the audio-related codec parameters) first. And remove the ADTS header from the original code stream (only the ES stream remains)

17. Video and audio merging 

The merging of two videos (note that the parameter hstack represents horizontal merging, that is, left and right merging. The image of horizontal merging is analogous to "Chuan"; if it is vertical merging, that is, up and down merging, use the parameter vstack, vertical or vertical merging. Image analogy is "three")

Note: The video output.mp4 processed by this command will only retain the audio of the first video input1.mp4 

ffmpeg -i input1.mp4 -i input2.mp4 -lavfi hstack output.mp4

ffmpeg -i input1.mp4 -i input2.mp4 -lavfi vstack output.mp4

Merge the audio file mp3 into the video file mp4 (that is, add background music to the original video)

ffmpeg -i video.mp4 -i bg_music.mp3 -c:v copy -c:a aac  -strict experimental    merge.mp4

18. Video area cropping

ffmpeg -i ..\test\guide.mp4 -vf crop=400:400:0:0 -y  ..\test\guide_crop.mp4

The parameter format of the command crop is w:h:x:y; w and h are the width and height of the output video, x and y mark a certain point in the input video, use this point as the reference point, and crop to the lower right to obtain the output video. If xy is not written, it will be cut in the center by default.

 19. Video zoom

ffmpeg -i ..\test\guide.mp4 -vf scale=iw*2:ih*2 ..\test\guide_scale2.mp4 // Double the video width and height

ffmpeg -i ..\test\guide.mp4 -vf scale=iw*0.5:ih*0.5 ..\test\guide_scale0.5.mp4 // Reduce the video width and height by half

 

Guess you like

Origin blog.csdn.net/a360940265a/article/details/111479474