FFmpeg
FFmpeg basic usage
Problems to be Solved in this lesson
1. What is the FFMPEG transcoding process?
2. What content does a common video format contain?
3. How to extract the content from a video file?
4. How to convert from one format to another?
5. How to zoom in and out a video?
6. How to rotate, flip, fill, crop, blur, and sharpen videos?
7. How to add a logo to the video and delete the logo?
8. How to add text and dynamic text to a video?
9. How to process images?
10. How to video, add dynamic logos, and mosaic videos?
Part 1
Basic
Terms
Container)
A container is a file format, such as FLV and MKV. Contains the following five streams and file header information.
Stream)
It is a transmission of video data information. Five streams are used: audio, video, subtitle, attachment, and data.
Frame)
Frames represent a static image, which can be divided into I frame, P frame, and B frame.
Codec)
Is to compress or decompress the video, codec = Code (encoding) + decode (Decoding)
Multiplexing/demultiplexing (MUX/Demux)
Put different streams into containers according to certain container rules. This behavior is called multiplexing (MUX)
Parse different streams from a container. This behavior is called Demux)
Introduction
The name of FFMPEG comes from the MPEG video encoding standard. The previous "FF" indicates "Fast Forward". FFmpeg is a set of functions that can be used to record and convert digital audios and videos, and can be converted into open-source computer programs of the stream. You can easily convert multiple video formats.
FFmpeg users include Google, Facebook, YouTube, Youku, iQiYi, and Tudou.
Composition
1. libavformat: used to generate and parse audio and video encapsulation formats, including obtaining the information required for decoding to generate the decoding Context Structure and reading audio and video frames, including demuxers and muxer libraries;
2. libavcodec: used for various types of sound/Image Coding/decoding;
3. libavutil: contains some common tool functions;
4. libswscale: used for proportional scaling and color ing conversion in video scenarios;
5. libpostproc: used for post-effect processing;
6. FFMPEG: it is a command line tool used to convert the video file format. It also supports real-time Encoding of video cards;
7. ffsever: an HTTP multimedia real-time broadcast stream server that supports time translation;
8. ffplay: A simple player that uses the FFMPEG library for parsing and decoding and is displayed through SDL;
FFmpeg Process
Filter)
In multimedia processing, filter indicates a software tool used to modify the content of an input file before being encoded into an output file. Such as video flip, rotation, and scaling.
Syntax: [input_link_label1] [input_link_label2]… Filter_name = parameters [output_link_label1] [output_link_label2]…
Filter chart link label: indicates the name of the input or output of the filter.
1. Video filter-VF
For example, the testsrc video is rotated 90 degrees clockwise.
Ffplay-F lavfi-I testsrc-VF transpose = 1
For example, testsrc video horizontal flip (left and right flip)
Ffplay-F lavfi-I testsrc-VF hflip
2. audio filter-AF
Implements Slow playback. The sound speed is 50% of the original speed.
Ffplay p629100108- af atempo = 0.5
How to rotate 90 degrees clockwise and flip horizontally?
Filter chain)
Basic syntax
Filterchain = a comma-separated filter group
Syntax: "filter1, filter2, filter3 ,... FilterN-2, filterN-1, filtern"
Rotate 90 degrees clockwise and flip horizontally
Ffplay-F lavfi-I testsrc-VF transpose = 1, hflip
How to compare the horizontal Flip video and source video? See how the filter chain is implemented.
Filter chain)
Step 1: double the width of the source video.
FFmpeg-I jidu.mp4-T 10-VF pad = 2 * IW output.mp4
Step 2: horizontal flip of the Source Video
FFmpeg-I jidu.mp4-T 10-VF hflip output2.mp4
Step 3: Cover output.mp4 with a horizontal Flip video
FFmpeg-I output.mp4-I output2.mp4-filter_complex overlay = W compare.mp4
Is it complicated?
You only need one command to use filtergraph.
Filter graph)
Basic syntax
Filtergraph = a set of filterchains separated by semicolons
"Filterchain1; filterchain2 ;... FilterchainN-1; filterchainn"
Filtergraph Classification
1. simple one-to-one
2. Complex (complex) Multi-to-one, multi-to-many
Filter graph)
Simple filter chart processing process:
Complex filter chart processing process:
It can be found that the complex filter diagram has two steps less than the simple filter diagram, and the efficiency is higher than the simple filter diagram. We recommend that you use the complex filter diagram whenever possible.
Answer the questions above to achieve horizontal flip of the video and compare the source video.
Filter graph)
Use ffplay to directly view the results:
Ffplay-F lavfi-I testsrc-VF split [a] [B]; [a] pad = 2 * IW [1]; [B] hflip [2]; [1] [2] overlay = W
F1: The split filter creates copies of two input files and marks them as [a], [B]
F2: [a] as the input of the pad filter, the pad filter generates 2 times the width and outputs it to [1].
F3: [B] as the input of hflip filter, vflip filters flip the video horizontally and output it to [2].
F4: Use the overlay filter to overwrite [2] to the side of [1.
Select a media stream
Some multimedia containers, such as Avi, MKV, and MP4, can contain multiple streams of different types. How can we extract various streams from the container?
Syntax:
-Map file_number: stream_type [: stream_number]
Here are some special stream symbols:
1.-map 0 Selects all streams of the first file
2.-map I: V obtains all video streams from the file No. I (INDEX),-map I: A gets all audio streams,-map I: s gets all subtitle streams, and so on.
3. Special parameters-An,-VN, and-Sn are used to exclude all audio, video, and Subtitle streams.
Note: The file sequence number and stream sequence number are counted from 0.
Select a media stream
Part 2
View help
Help
The FFMPEG tool has a huge Console Help. The following table describes available options. Italics indicate the items to be replaced, and ffplay and ffprobe have similar options.
Help
Available bit stream: FFMPEG-BSFs
Available codecs: FFMPEG-codecs
Available decoder: FFMPEG-decoders
Available encoders: FFMPEG-encoders
Available filter: FFMPEG-Filters
Available video formats: FFMPEG-formats
Available Channel Layout: FFMPEG-layouts
Available license: FFMPEG-l
Available pixel format: FFMPEG-pix_fmts
Available Protocol: FFMPEG-protocals
Part 3
Bit Rate, frame rate, and file size
Overview
Bit Rate and frame rate are the most important basic features of video files. Their special settings determine the video quality. If we know the bit rate and duration, we can easily calculate the size of the output file.
Frame Rate: The frame rate is also called the frame frequency. The frame rate is the number of frames per second in a video file. To see a continuously moving image, you need at least 15 frames.
Bit Rate: Bit Rate (also called bit rate, data rate) is a parameter for determining the overall video/audio quality. The number of bytes processed per second is proportional to the video quality, BPS is used to express the bit rate in a video file.
Frame Rate
1. Use the-R parameter to set the Frame Rate
FFmpeg-I input-r FPS output
2. Use FPS filter to set the Frame Rate
FFmpeg-I clip. mpg-VF FPS = 25 clip. webm
Pre-defined value of frame rate:
For example, if the bitrate is set to 29.97fps, the following three methods have the same results:
FFmpeg-I input. Avi-r 29.97 output. mpg
FFmpeg-I input. Avi-r 30000/1001 output. mpg
FFmpeg-I input. Avi-r netsc output. mpg
Bit Rate and file size
Set bitrate-parameter B
-B
FFmpeg-I film. Avi-B 1.5 m film.mp4
Audio:-B: A Video:-B: V
Set the video bit rate to 1500 kbps.
FFmpeg-I input. Avi-B: V 1500 K output.mp4
Control output file size
-FS (abbreviation of file size)
FFmpeg-I input. Avi-Fs 1024 K output.mp4
Calculate the output file size
(Video bit rate + audio bit rate) * duration/8 = file size k
Part 4
Adjust video resolution
Adjust video resolution
1. Use the-S parameter to set the video resolution. The parameter value is WXH. The unit of W width is pixel, and the unit of H height is pixel.
FFmpeg-I input_file-s 320x240 output_file
2. predefined video size
The following two commands have the same effect
FFmpeg-I input. avi- s 640x480 output. Avi
FFmpeg-I input. Avi-s VGA output. Avi
The following table lists all predefined dimensions.
Scale filter to adjust resolution
The advantage of scale filter is that some additional parameters can be used.
Syntax:
Scale = width: height [: interl = {1 |-1}]
The following table lists common additional parameters.
Example
The following two commands have the same effect
FFmpeg-I input. mpg-s 320x240 output.mp4
FFmpeg-I input. mpg-VF scale = 320: 240 output.mp4
Proportional scaling of input videos
Change to half size of Source Video
FFmpeg-I input. mpg-VF scale = IW/2: ih/2 output.mp4
Change to 90% of the original video size:
FFmpeg-I input. mpg-VF scale = IW * 0.9: ih * 0.9 output.mp4
Example
When the resolution of an unknown video is used, make sure that the adjusted resolution is consistent with that of the source video.
Fixed width 400, height proportional:
FFmpeg-I input. Avi-VF scale = 400: 400/
FFmpeg-I input. Avi-VF scale = 400:-1
On the contrary, the height is fixed at 300, and the width is proportional:
FFmpeg-I input. Avi-VF scale =-1:300
FFmpeg-I input. Avi-VF scale = 300 * A: 300
Part 5
Crop/fill video
Crop video crop Filter
Selecting the desired rectangular area from the input file to the output file is often used to remove the Black edge of the video.
Syntax: crop: Ow [: Oh [: X [: Y: [: keep_aspect]
Example
Crop the left 1/3, middle 1/3, and right 1/3 of the input video:
FFmpeg-I input-VF crop = IW/3: iH: 0: 0 output
FFmpeg-I input-VF crop = IW/3: iH: IW/3:0 output
FFmpeg-I input-VF crop = IW/3: iH: IW/3*2: 0 output
Center of the cropped Frame
When we want to crop the area in the middle of the frame, the cropping filter can skip the input x and y values. Their default values are
Xdefault = (input width-output width)/2
Ydefault = (input height-output height)/2
FFmpeg-I input_file-V crop = W: H output_file
Crop the middle half of the area:
FFmpeg-I input. Avi-VF crop = IW/2: ih/2 output. Avi
Example
Compare the cropped video and Source Video
Ffplay-I jidu.mp4-VF split [a] [B]; [a] drawbox = x = (iw-300)/2 :( ih-300)/2: W = 300: H = 300: C = yellow [a]; [a] pad = 2 * IW [c]; [B] crop = 300: 300 :( iw-300)/2 :( ih-300) /2 [B]; [C] [B] overlay = W * 2.4: 40
Auto-detect crop area
Cropdetect filter automatically detects Black edge areas
Ffplay jidu.mp4-VF cropdetect
Then, use the detected value to crop the video.
Ffplay jidu.mp4-VF crop = 672: 272: 0: 54
Pad)
Adds a fast additional area to the video frame, which is often used to display different vertical/horizontal ratios during playback.
Syntax: pad = width [: Height: [: X [: Y: [: Color]
Example
Create a 30-pixel pink width to enclose an image of svga size:
FFmpeg-I photo.jpg-VF pad = 860: 660: 30: 30: pink framed_photo.jpg
?
Similarly, you can create a video named testsrc and enclose the video with 30 pixels in pink.
Ffplay-F lavfi-I testsrc-VF pad = IW + 60: ih + 60: 30: 30: pink
To
Some devices can only play the ratio. The ratio must be filled with on both sides of the horizontal direction,
The height is maintained, and the width equals to the height multiplied by 16/9. The X (horizontal displacement of the input file) value is calculated by the expression (output_width-input_width)/2.
The general commands from to are:
FFmpeg-I input-VF pad = ih * 16/9: ih :( Ow-IW)/2: 0: color output
Example
Ffplay-F lavfi-I testsrc-VF pad = ih * 16/9: ih :( Ow-IW)/2: 0: pink
PM to PM
To display the ratio of X to Y at, fill in the vertical sides of the input file, and keep the width unchanged. The height is 3/4 of the width and Y value (the vertical offset of the input file) is calculated by an expression (output_height-input_height)/2.
General commands from PM to PM:
FFmpeg-I input-VF pad = IW: IW * 3/4: 0 :( oh-IH)/2: color output
Example
Ffplay-F lavfi-I testsrc = size = 320x180-VF pad = IW: IW * 3/4: 0 :( oh-IH)/2: pink
Part 6
Flip and rotate
Flip
Horizontal flip Syntax:-VF hflip
Ffplay-F lavfi-I testsrc-VF hflip
Vertical flip Syntax:-VF vflip
Ffplay-F lavfi-I testsrc-VF vflip
Rotate
Syntax: transpose = {0, 1, 2, 3}
0: Rotate 90 ° counterclockwise and flip vertically
1: Rotate 90 ° clockwise
2: Rotate 90 ° counterclockwise
3: Rotate 90 ° clockwise and then flip horizontally
Part 7
Blur and sharpen
Fuzzy
Syntax: boxblur = luma_r: luma_p [: chroma_r: chram_p [: alpha_r: alpha_p]
Ffplay-F lavfi-I testsrc-VF boxblur = 1: 10: 4: 10
Note: The radius of luma_r and alpha_r ranges from 0 ~ Min (W, h)/2, the range of chroma_r radius is 0 ~ Min (CW/CH)/2
Sharpen
Syntax:-VF unsharp = l_msize_x: l_msize_y: l_amount: c_msize_x: c_msize_y: c_amount
All parameters are optional. The default value is. 0: 5: 5: 0.0.
L_msize_x: horizontal brightness matrix, value range: 3-13, default value: 5
L_msize_y: vertical brightness matrix, value range: 3-13, default value: 5
Rochelle amount: brightness intensity. value range:-2.0-5.0. The negative number indicates the blur effect. The default value is 1.0.
C_msize_x: horizontal color matrix; value range: 3-13; default value: 5
C_msize_y: vertical color matrix, value range: 3-13, default value: 5
C_amount: Color intensity. value range:-2.0-5.0. The negative number indicates the blur effect. The default value is 0.0.
Example
Use the default value. The brightness matrix is 5x5 and the brightness value is 1.0.
FFmpeg-I input-VF unsharp output.mp4
Gaussian blur effect (strong fuzzy ):
Ffplay-F lavfi-I testsrc-VF unsharp = 13: 13:-2
Part 8
Overwrite (in-picture)
Overwrite
Syntax: overlay [= x [: Y]
All parameters are optional. The default value is 0.
Example
The logo is in the upper left corner.
FFmpeg-I pair.mp4-I logo.png-filter_complex overlay pair1.mp4
Example
Upper right corner:
FFmpeg-I pair.mp4-I logo.png-filter_complex overlay = w-w pair2.mp4
Lower left:
FFmpeg-I pair.mp4-I logo.png-filter_complex overlay = 0: H-H pair2.mp4
Bottom right corner:
FFmpeg-I pair.mp4-I logo.png-filter_complex overlay = W-W: H-H pair2.mp4
Delete logo
Syntax:-VF delogo = x: Y: W: H [: T [: Show]
X: Y coordinate from the upper left corner
W: H logo width and height
T: The thickness of the rectangular edge. The default value is 4.
Show: If 1 is set to a green rectangle, the default value is 0.
Ffplay-I jidu.mp4-VF delogo = 50: 51: 60: 60: 100: 0
Part 9
Add text
Add text
Syntax:
Drawtext = fontfile = font_f: text = text1 [: P3 = V3 [: P4 = V4 […]
Common parameter values
X: the abscissa from the upper left corner.
Y: vertical coordinate from the upper left corner
Fontcolor: font color
Fontsize: font size
Text: Text Content
Textfile: Text File
T: Timestamp, in seconds
N: the starting position of the number of frames is 0.
Draw/enable: display the control file. If the value is 0, it is not displayed. If the value is 1, you can use the function.
Simple usage
1. Add the welcome text in the upper left corner.
Ffplay-F lavfi-I color = c = white-VF drawtext = fontfile = Arial. TTF: text = welcom
2. Add good day in the center
Ffplay-F lavfi-I color = c = white-VF drawtext = "fontfile = Arial. TTF: text = 'goodday': x = (W-tw)/2: Y = (H-th)/2"
3. Set the font color and size.
Ffplay-F lavfi-I color = c = white-VF drawtext = "fontfile = Arial. TTF: text = 'Happy holidays': x = (W-tw)/2: Y = (H-th)/2: fontcolor = Green: fontsize = 30"
Dynamic text
Use the T (time second) variable to implement dynamic text
1. horizontal scrolling at the top
Ffplay-I jidu.mp4-VF drawtext = "fontfile = Arial. TTF: text = 'dynamic RTL text': x = W-T * 50: fontcolor = darkorange: fontsize = 30"
2. horizontal scrolling at the bottom
Ffplay-I jidu.mp4-VF drawtext = "fontfile = Arial. TTF: textfile1_textfile.txt: x = W-T * 50: Y = H-TH: fontcolor = darkorange: fontsize = 30"
3. vertical scroll up from bottom
Ffplay jidu.mp4-VF drawtext = "textfile = textfile: fontfile = Arial. TTF: x = (W-tw)/2: Y = h-t * 100: fontcolor = white: fontsize = 30"
Do you want to display the current time in the upper-right corner?
Dynamic text
Display the current time localtime in the upper right corner
Ffplay jidu.mp4-VF drawtext = "fontfile = Arial. TTF: x = W-TW: fontcolor = white: fontsize = 30: text = '% {localtime \: % H \: % m \\\: % s}'
Display the current time every 3 seconds
Ffplay jidu.mp4-VF drawtext = "fontfile = Arial. TTF: x = W-TW: fontcolor = white: fontsize = 30: text = '% {localtime \: % H \: % m \\\: % s} ': Enable = LT (mod (T \, 3) \, 1 )"
Part 10
Image Processing
Image Support
FFmpeg supports the vast majority of image processing, except ljpeg (lossless JPEG), which can be decoded. All except EXR, PIC, and PTx can be encoded.
Use the-SS (seek from start) parameter to intercept an image.
FFmpeg-SS 01:23:45-I jidu.mp4 image.jpg
Generate GIF images from videos
FFmpeg-I jidu.mp4-T 10-pix_fmt rgb24 jidu.gif
Convert a video to an image (one image per frame)
FFmpeg-I clip. Avi frame00004d.jpg
Convert an image to a video
FFmpeg-F image2-I img3164d.jpg-R 25 video.mp4
Cropping and filling
Like videos, images can be cropped and filled.
Crop
FFmpeg-F lavfi-I rgbtestsrc-VF crop = 150: 150 crop_rg.png
Fill
FFmpeg-F lavfi-I smptebars-VF pad = 360: 280: 20: 20: Orange pad_smpte.jpg
Flip, rotate, Overwrite
Images can be flipped, rotated, and overwritten like videos.
Flip
FFmpeg-I orange.jpg-VF hflip orange_hfilp.jpg
FFmpeg-I orange.jpg-VF vflip orange_vfilp.jpg
Rotate
FFmpeg-I image.png-VF transpose = 1 image_rotated.png
Overwrite
FFmpeg-F lavfi-I rgbtestsrc-S 400x300 RGB. PNG
FFmpeg-F lavfi-I smptebars smpte.png
FFmpeg-I rgb. PNG-I smpte.png-filter_complex overlay = (w-w)/2 :( h-h)/2 rgb_smpte.png
Part 1
Other advanced skills
Screen recording
Display Device Name
FFmpeg-list_devices 1-F dshow-I dummy
Call the camera
Ffplay-F dshow-I video = "integrated camera"
Save as file
FFmpeg-y-F dshow-s 320x240-R 25-I video = "integrated camera"-B: V 800 K-vcodec MPEG4 new.mp4
Add subtitle subtitles
Syntax-VF Subtitles = File
FFmpeg-I jidu.mp4-VF Subtitles = RGB. srt output.mp4
Video trembling and color balancing
Video trembling
Ffplay-I jidu.mp4-VF crop = in_w/2: in_h/2 :( in_w-out_w)/2 + (in_w-out_w)/2) * sin (N/10) :( in_h-out_h) /2 + (in_h-out_h)/2) * sin (N/7)
Color balance
Ffplay-I jidu.mp4-VF curves = vintage
Changing colors
Ffplay-I jidu.mp4-VF hue = "H = 2 * pI * T: S = sin (2 * pI * t) + 1"
Color Conversion black/white
Ffplay-I jidu.mp4-VF lutyuv = "u = 128: V = 128"
Set the audio/video playback speed
3X video playback
Ffplay-I jidu.mp4-VF setpts = pts/3
? Video playback speed
Ffplay-I jidu.mp4-VF setpts = pts/(3/4)
Playback of audio at 2x speed
Ffplay-I speech.mp3-AF atempo = 2
Problem: video and audio are simultaneously played at a speed of 3/4
Cut a graph every second
FFmpeg-I input. FLV-F image2-VF FPS = 1 outputted d.png
Screenshot every 20 seconds
FFmpeg-I input. FLV-F image2-VF FPS = 1/20 out0000d.png
Note: The FFMPEG version N-57961-gec8e68c version can cut a graph every 20 s at most.
Merge multiple files into one file (2x3 )? Capture a graph every one thousand frames (in seconds = 1000/fps25)
FFmpeg? -I jidu.mp4-frames 3-VF "select = Not (mod (n \, 1000), scale = 320: 240, tile = 2x3" out.png
Mosaic video
Mosaic video
Create a mosaic video with multiple input files:
FFmpeg-I jidu.mp4-I jidu. FLV-I "day by day sbs.mp4"-I "dangerous.mp4"-filter_complex "nullsrc = size = 640x480 [base]; [0: V] setpts = PTS-STARTPTS, scale = 320x240 [upperleft]; [1: V] setpts = PTS-STARTPTS, scale = 320x240 [upperright]; [2: V] setpts = PTS-STARTPTS, scale = 320x240 [lowerleft]; [3: V] setpts = PTS-STARTPTS, scale = 320x240 [lowerright]; [base] [upperleft] overlay = shortest = 1 [tmp1]; [tmp1] [upperright] overlay = shortest = 1: x = 320 [tmp2]; [tmp2] [lowerleft] overlay = shortest = 1: Y = 240 [tmp3]; [tmp3] [lowerright] overlay = shortest = 1: x = 320: y = 240 "-C: V libx264 output.mkv
Dynamic logo Movement
After 1 and 2 seconds, the logo will be moved from left to right:
Ffplay-I jidu.mp4-VF moview.logo.png [logo]; [in] [logo] overlay = x = 'if (GTE (T \, 2) \, (t-2) * 80) -W \, Nan) ': Y = 0
2. After 2 seconds, the logo is moved from left to right and then stops in the upper left corner.
Ffplay-I jidu.mp4-VF moview.logo.png [logo]; [in] [logo] overlay = x = 'if (GTE (t-2) * 80)-w \, W) \, 0 \, (t-2) * 80)-W) ': Y = 0
3. The logo appears every 10 seconds.
FFmpeg-y-T 60-I jidu.mp4-I logo.png-I logo2.png-filter_complex "overlay = x = If (LT (mod (T \, 20) \, 10 )\, 10 \, Nan): Y = 10, overlay = x = If (GT (mod (T \, 20) \, 10) \, W-w-10 \, Nan ): y = 10 "overlay.mp4
Materials
FFmpeg Official Website: http://www.ffmpeg.org
FFmpeg DOC: http://www.ffmpeg.org/documentation.html
FFmpeg wiki: https://trac.ffmpeg.org/wiki
FAQ
Thanks!
FFmpeg basics: http://wenku.baidu.com/view/296eefcaf90f76c661371af1.html
Original post address: http://derekzhan.iteye.com/blog/1989274
Basic FFMPEG usage (transfer)