Common operations of ffmpeg

1. Download ts fragment with ffpeg

ffmpeg -i  -c copy d:\save\1.mp4

The parameter - i here is followed by the URL of m3u8 and - C is the code of copy, which is then stored in the save directory of disk d (it's best to build a directory here, otherwise it's hard to find)

2. Change the format of a video

ffmpeg -i total.mp4 -c copy total.flv

Here I put this The mp4 file is placed in the directory where ffmpeg is installed, and then it can be converted to flv format

3. Extract only sound from a video

ffmpeg -i total.mp4 -vn 1.mp4

-vn stands for no video processing

4. Extract the video picture, but there is no sound

ffmpeg -i total.mp4 -an 1.mp4

-an means no audio processing

5. Clip video

ffmpeg -ss 00:00:00 -t 00:00:15 -i total.mp4 -c copy 1.mp4

-ss stands for the start time

-t stands for the number of seconds to cut

6. Video coding conversion

For example, the coding of a video is MPEG4. What if you want to use H264 coding?

ffmpeg -i total.mp4 -vcodec h264 1.mp4

-vcodec video coding

7. Adjust picture resolution

Set the resolution of a video 1920 * 1080 to 960 * 540

ffmpeg -i total.mp4 -vf scale=960:540  1.mp4

-vf represents the image filter

If 960: [540] is written as 960: - 1 without writing, ffmpeg will notify the scaling filter to maintain the original aspect ratio height when outputting.

8. Merge videos

First, create a new one txt text document, enter file + 'file name' (Note: be sure to use single quotation marks, otherwise an error will be reported)

ffmpeg -f concat -i test.txt -c copy 2.mp4

-f stands for mandatory input and input file format


9. Compressed video

(1) Change the frame rate of the video

ffmpeg -i total.mp4 -r 20 2.mp4
  • -r 20: indicates that the frame rate is set to 20fps

(2) Specify file size


ffmpeg -i total.mp4 -fs 20  2.mp4


  • fs 20: indicates that the maximum file size is 20MB

(3) Change the bit rate of video

ffmpeg -i 1.mp4 -b:v 1.5m 2.mp4


  • -b:v 1.5M: Specifies the bit rate
  • -b:v: Specifies the bit rate of the video
  • -b:a: specify the bit rate of the audio
  • 1.5M: the value of code rate 1.5M means 1.5Mb/s
When the bit rate is set to less than 1.5Mb/s, the definition of the video will be greatly reduced


10. Add logo to the picture

ffmpeg -i 1.mp4  -i 1.jpg -filter_complex overlay 2.mp4

-overlay represents the location of the watermark

Syntax: overlay[=x:y[[:rgb={0, 1}]] parameters X and y are optional, and the default is 0. Parameter RGB parameter is also optional. Its value is 0 or 1, and the default is 0. Parameter Description: X is the horizontal coordinate from the upper left corner, the default value is 0, y is the vertical coordinate from the upper left corner, the default value is 0, and RGB value is 0, which means that the input color space does not change, and the default value is 0; A value of 1 indicates that the input color space is set as 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 enter (foreground window) width overlay_h or h overlay input (foreground window) height


Upper right corner:
./ffmpeg -i input.mp4 -i logo.png -filter_complex overlay=W-w output.mp4
 lower left quarter:
./ffmpeg -i input.mp4 -i logo.png -filter_complex overlay=0:H-h output.mp4
 Lower right corner:
./ffmpeg -i input.mp4 -i logo.png -filter_complex overlay=W-w:H-h output.mp4

11. Capture a frame in the video and convert it into a picture

ffmpeg -ss 00:00:00 -i 1.mp4  -vframes 1  1.jpg

-vframes 1 represents the first frame of the intercepted video

(1) Take a picture every second

ffmpeg -i 1.mp4 -vf fps=1 %d.jpg

In particular,

  • -vf fps=1 means that frame per second intercepts one
  • %d.png will generate similar 1 jpg 2. Jpg and other documents.

(2) Take a picture every once in a while

Capture the video into a collection of pictures

ffmpeg -i input.mp4  -ss 00:00:10 -t 3  -vcodec mjpeg  pic/%04d.jpg

Description from the tenth second of the video, intercept the video in 3 seconds to generate pictures

You must create a pic picture folder and ensure that the video duration is greater than 10 seconds

12. Picture synthesis video

ffmpeg -f image2 -i D:\Download File\wallpaper\%d.jpg 2.mp4

-f image2 means that the format of the input or output file is image2 format, - f means format.

13. Add text watermark to the video

ffmpeg -i 1.mp4 -vf "drawtext=fontfile=simhei.ttf: text='I'm such a father':x=10:y=10:fontsize=24:fontcolor=white:shadowy=2" 2.mp4

Added by statrat on Fri, 25 Feb 2022 15:35:36 +0200