ffmpeg command line usage

View video information

ffmpeg -i video name


Enter the first few characters of the video name here and presstabKeys can be autocompleted

Return results:

The content within the red box is of no use

Encoder: encoder: Lavf57.25.100
Duration: 00 20.58, start: 0.000000, bitrate: 381 kb/s
Duration duration, start start time, bitrate bit rate

Video stream: Stream #0 Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yu
v420p, 1360×768, 182 kb/s, 29.94 fps, 30 tbr, 15360 tbn, 30720 tbc (default)
h264 encoding format, avc1 encapsulation format, yuv420p each frame format, 1360×768 resolution, 182 kb/s video bit rate, 29.94 fps frame number

Audio Stream: Stream #0 Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, flt
p, 191 kb/s (default)
aac encoding format, mp4a packaging format, 44100 Hz sampling rate, stereo, fltp floating point, 191 kb/s (default) audio bit rate

Remove audio

ffmpeg -i filename-an -vcodec copy filename.mp4

-an removes audio, -vcodec copy copies the video stream of the source data to the file name .mp4 file

Remove video

ffmpeg -i filename-vn -acodec copy filename.mp4

-vn removes audio, -acodec copy copies the audio stream of the source data to the filename.mp4 file.

Save audio as AAC file

ffmpeg -i filename.mp4 -vn -acodec copy filename.aac

Change video resolution

ffmpeg -i filename.mp4 -vf scale=640:360 filename.mp4

scale=What is filled in next is the modified resolution

ffmpeg -i filename.mp4 -vf scale=iw*2:ih*2 filename.mp4

Double the resolution, or *0.5 to half it

Hide useless information

ffmpeg -i filename.mp4 -hide_banner

add watermark

ffmpeg -i filename.mp4 -i watermark.png -filter_complex overlay out.mp4

The default is the upper left corner, overlay=Ww is the upper right corner, overlay=0:Hh is the lower left corner, overlay=Ww:Hh is the lower right corner
The uppercase H is the height of the video, the uppercase W is the width of the video, the lowercase h is the height of the watermark, and the lowercase w is the width of the watermark.

Remove watermark

ffmpeg -i filename.mp4 -vf delogo=xw show out.mp4

x, y determine the position coordinates of the logo
w, h set the width and height of the logo
show indicates whether to display the rectangular box, the default is 0 not to display, 1 to display

Add text watermark

ffmpeg -i filename.mp4 -vf drawtext=text='content' fontsize=font size out.mp4

Capture video

ffmpeg -i filename.mp4 -ss seconds-to time out.mp4

How many seconds does the video start from and how long does it take to capture the video?

Convert video to gif

ffmpeg -i filename.mp4 -ss 10 -t 5 -s 640*320 -r 15 out.mp4

-ss start time
-t duration
-i is followed by the source file
-s animation resolution
-r frame rate

ffmpeg -i filename.mp4 -vframes 30 out.gif

-vframes 30 the first thirty frames of the video
Convert the first thirty frames of the video to gif

Video compression-crf

ffmpeg -i filename.mp4 -c:v libx264 -crf 23 out.mp4

×264 The default value is 23 (18 – 28 recommended)
×265 default is 28 (recommended 24 – 34)
Available values ​​for both are 0 to 51
The smaller the value, the better the encoding quality and the higher the bitrate.
Among them, 0 can be said to be lossless, and 51 is the worst quality.
A crf difference of 6 will double or halve the file size

Video compression-preset

ffmpeg -i filename.mp4 -vocdec libx264 -preset xxx -crt 25 out.mp4

preset mainly affects the speed of encoding and does not greatly affect the size of the output file.
The x264 encoding algorithm has the following parameter values
Ultrafast has the fastest encoding speed and low compression rate, and the generated files are very large.
superfa
veryfast
faster
fast
medium x264 default standard
slow
slower
veryslow
placebo has the slowest encoding speed, high compression rate, and small generated files.

xxx is to fill in the following parameters

Add black borders to videos or images

ffmpeg -i filename.mp4 -vf pad=1080 0 black out.mp4

The parameter values ​​behind the pad are: width, height, x coordinate, y coordinate. The default is black border and can be omitted.
56:(720 – 680)/2=56
1080 x 608 resolution becomes 1080 x 720 resolution

Convert format

ffmpeg -i out.mp4 -vcodec copy -acodec copy out.flv

The above command list is to copy audio and video directly, but convert the mp4 encapsulation format to flv.
Copy is to copy the original without conditions

  • -i: input file
  • -vcodec copy: video encoding processing method
  • -acodec copy: Audio encoding processing method

Audio and video merge

ffmpeg -i out.h264 -i out.aac -vcodec copy -acodec copy out.mp4

Extract YUV data

ffmpeg -i input.mp4 -an -c:v rawvideo -pixel_format yuv420p out.yuv

ffplay -s (wxh resolution, view in stream) out.yuv
  • -an: a represents audio, n represents no, which does not include audio.
  • -c:v rawvideo specifies to convert the video into raw data
  • -pixel_format yuv420p specifies the conversion format to yuv420p(4 0)

YUV to H264

ffmpeg -f rawvideo -pix_fmt yuv420p -s 320x240 -r 30 -i out.yuv -c:v libx264 -f rawvideo out.h264

Extract PCM data

ffmpeg -i out.mp4 -vn -ar 44100 -ac 2 -f s16le out.pcm

When playing, the parameters should be the same as above, because I don’t know what sampling rate and channel you use.

ffplay -ar 44100 -ac 2 -f s16le -i out.pcm
  • -vn: v stands for video, n stands for no video
  • -ar: a represents audio, r represents read, sampling rate 44100 44.1k
  • -ac: a represents audio, c2 represents two-channel and stereo, surround sound
  • -f: Audio extraction access format, s16 s represents signed, each bit is represented by 16 bits

PCM to WAV

ffmpeg -f s16be -ar 8000 -ac 2 -acodec pcm_s16be -i input.raw output.wav

See what devices are available

First, use the following command to check what devices are available on the mac.

ffmpeg -f avfoundation -list_devices true -i ""

Record screen

You need to specify the width and height when playing yuv

ffmpeg -f avfoundation -i 1 -r 30 out.yuv
  • -f specifies using avfoundation to collect data.
  • -i specifies where to collect data from, which is a file index number. On my MAC, 1 represents the desktop (you can query the device index number through the above command) (1 is actually an index, the screen is 1, and the camera is 0).
  • -r specifies the frame rate. According to the official ffmpeg documentation, -r and -framerate have the same effect, but they were found to be different during actual testing. -framerate is used to limit input, and -r is used to limit output.

Note that desktop input has no frame rate requirements, so there is no need to limit the desktop frame rate. In fact, it is useless to limit it.

play

You will be prompted when recording

Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 3360×2100,
q=2-31, 3386880 kb/s, 30 fps, 30 tbn, 30 tbc

-s 3360×2100
-pix_fmt uyvy422

ffplay -s xxxxx (the width and height when you record) -pix_fmt (also the format when you record) ./out.yuv

Screen recording + sound

ffmpeg  -f avfoundation -i 1:0  -r 29.97 -c:v libx264 -crf 0 -c:a libfdk_aac -profile:a aac_he_v2 -b:a 32k  out.flv
ffmpeg -y -f avfoundation -i 1:0 -framerate 60 -c:v libx264 -r 60 -pix_fmt yuv420p -preset 0 -crf 19 -c:a aac -b:a 192k "$HOME/Screen Record $(date "+%Y-%m-%d %H-%M-%S").mp4"
  • -i 1:0 The “1” before the colon represents the screen index number. The “0” after the colon represents the phase number of the sound.
  • -c:v is the same as the parameter -vcodec, indicating the video encoder. c is the abbreviation of codec, v is the abbreviation of video.
  • -crf is a parameter for x264. 0 table-style lossless compression.
  • -c:a is the same as the parameter -acodec, indicating the audio codec.
  • -profile is a parameter of fdk_aac. The aac_he_v2 table uses AAC_HE v2 to compress data.
  • -b:a specifies the audio bitrate. b is the abbreviation of bitrate, a is the abbreviation of audio

Record video

ffmpeg -framerate 30 -f avfoundation -i 0 out.mp4 
  • -framerate limits the video capture frame rate. This must be set according to the prompt requirements. If not set, an error will be reported.
  • -f specifies using avfoundation to collect data.
  • -i specifies the index number of the video device

video+audio

ffmpeg -framerate 30 -f avfoundation -i 0:0 out.mp4 

recording

ffmpeg -f avfoundation -i :0 out.wav

0 is the index representing the device, you can check it above

Recorded and played

ffplay ./out.wav

Record audio raw data

ffmpeg  -f avfoundation -i :0 -ar 44100 -f s16le out.pcm

Video doubled in size

ffmpeg -i out.mp4 -vf scale=iw/2:-1 scale.mp4
  • -vf scale specifies the use of a simple filter scale, and the iw in iw/2:-1 specifies the width of the video as an integer. -1 means the height changes along with the width.

Video cropping

ffmpeg -i VR.mov  -vf crop=in_w-200:in_h-200 -c:v libx264 -c:a copy -video_size 1280x720 vr_new.mp4

crop format: crop=out_w:out_h: x: y

  • out_w: The width of the output. The width of the video can be entered using the in_w form.
  • out_h: Output height. You can use the in_h table to enter the height of the video.
  • x: X coordinate
  • y: Y coordinate

If x and y are set to 0, cropping starts from the upper left corner. If not written, it will be cropped from the center point.

Play at double speed

ffmpeg -i out.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" speed2.0.mp4
  • -filter_complex complex filter, [0:v] indicates the video of the first (file index number is 0) file as input. setpts=0.5*PTS means that the pts timestamp of each frame of video is multiplied by 0.5, which is half the difference. [v] represents the output alias. The same goes for audio.
  • Map can be used to process complex output. For example, you can output specified multiple streams to one output file, or you can specify the output to multiple files. “[v]” Alias ​​the output of a complex filter as a stream of the output file. The usage of the above map is to output the video and audio output by the complex filter to the specified file.

symmetrical video

ffmpeg  -i out.mp4 -filter_complex "[0:v]pad=w=2*iw[a];[0:v]hflip[b];[a][b]overlay=x=w" duicheng.mp4
  • hflip horizontal flip

If you want to change it to vertical flipping, you can use vflip.

picture within picture

ffmpeg -i out.mp4 -i out1.mp4 -filter_complex "[1:v]scale=w=176:h=144:force_original_aspect_ratio=decrease[ckout];[0:v][ckout]overlay=x=W-w-10:y=0[out]" -map "[out]" -movflags faststart new.mp4

Record picture-in-picture

ffmpeg  -f avfoundation -i "1" -framerate 30 -f avfoundation -i "0:0" 
-r 30 -c:v libx264 -preset ultrafast 
-c:a libfdk_aac -profile:a aac_he_v2 -ar 44100 -ac 2 
-filter_complex "[1:v]scale=w=176:h=144:force_original_aspect_ratio=decrease[a];[0:v][a]overlay=x=W-w-10:y=0[out]" 
-map "[out]" -movflags faststart -map 1:a b.mp4

Multi-channel video splicing

ffmpeg  -f avfoundation -i "1" -framerate 30 -f avfoundation   -i "0:0" -r 30 -c:v libx264 -preset ultrafast -c:a libfdk_aac -profile:a aac_he_v2 -ar 44100 -ac 2 -filter_complex "[0:v]scale=320:240[a];[a]pad=640:240[b];[b][1:v]overlay=320:0[out]" -map "[out]" -movflags faststart  -map 1:a  c.mp4

add watermark

ffmpeg -i out.mp4  -vf "movie=logo.png,scale=64:48[watermask];[in][watermask] overlay=30:10 [out]" water.mp4
  • -movie in vf specifies the logo location. scale specifies the size of the logo. overlay specifies the position of the logo.

Remove watermark

First find the location where you want to delete the LOGO through ffplay

ffplay -i test.flv -vf delogo=x=806:y=20:w=70:h=80:show=1

Use delogo filter to delete LOGO

ffmpeg -i test.flv -vf delogo=x=806:y=20:w=70:h=80 output.flv

Crop

ffmpeg -i out.mp4 -ss 00:00:00 -t 10 out1.mp4
  • -ss specifies the start time of cropping, accurate to seconds, hours seconds
  • -t is the length of time after being trimmed, in seconds

merge

First create an inputs.txt file with the following contents:

file ‘1.flv’
file ‘2.flv’
file ‘3.flv’

Then execute the following command:

ffmpeg -f concat -i inputs.txt -c copy output.flv

hls slice

ffmpeg -i out.mp4 -c:v libx264 -c:a libfdk_aac -strict -2 -f hls  out.m3u8
  • -strict -2 specifies that the audio should have AAC.
  • -f hls Convert to m3u8 format.

Video to JPEG

ffmpeg -i test.flv -r 1 -f image2 image-%3d.jpeg
  • -r: frame rate of image transfer, 1 image per second
  • -f : What format to convert the input file to?
  • %3d is dynamic and consists of 3 numbers

Convert video to gif

ffmpeg -i out.mp4 -ss 00:00:00 -t 10 out.gif

Picture to video

This will output all frames at once

ffmpeg  -f image2 -i image-%3d.jpeg images.mp4
  • -i: indicates input, followed by a picture

Play YUV data

ffplay -pix_fmt nv12 -s 192x144 1.yuv 

Play Y plane in YUV

ffplay -pix_fmt nv21 -s 640x480 -vf extractplanes='y' 1.yuv

Related Posts

About the pitfalls of ARM development (AI)

Nanny-level official yolov7’s training of own data sets and project deployment

It has been officially announced that the second brother is going to start a business with the Huawei boss! Maybe friends in Luoyang will have another choice when they return to their hometown in the future!

Image-based 3D reconstruction

Analog Devices Inc. A2B (automotive audio bus)

Machine learning basic HMM model (hidden Markov)

Fundamentals of Deep Learning: Survey on Compressed Video Enhancement

The 20 most common types of cybersecurity attacks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*