当前位置:网站首页>Ffmpeg common instructions
Ffmpeg common instructions
2022-07-28 04:01:00 【SingYi】
The task of these two days gives me the feeling that
And then just use .Net call FFmpeg, I found an open source program on the Internet FFmpegCore The document was not found , Forget it or native cmd Call it . Then there is such a thing
Get video screenshots :
ffmpeg -i INPUT -r 1 -q:v 2 -f image2 image-%d.jpeg
The following is reproduced from :https://www.jianshu.com/p/f07f0be088d0
FFmpeg Options
One 、 Main options
1、-f fmt (input/output) Specify the input or output file format . General can be omitted and automatic assignment based on extension can be used , But some options need to be explicitly set . 2、-i filename (input) Specify input file . 3、-y (global) The output file is automatically overwritten by default , Instead of asking for confirmation . 4、-n (global) Do not overwrite the output file , If the output file already exists, exit now . 5、-t duration(input/output) Limit input / Output time . If it's in -i front , Is to limit how long data can be read from the input ; If it is used to limit the output file , It indicates how long the data is written and then stops .duration It can be a number in seconds or hh:mm:ss[.xxx] The time value of the format . Be careful -to and -t They are mutually exclusive. ,-t There's a higher priority . 6、-to position (output) Write only position Stop after time ,position It can be a number in seconds or hh:mm:ss[.xxx] The time value of the format . Be careful -to and -t They are mutually exclusive. ,-t There's a higher priority . 7、-ss position (input/output) When in -i front , Indicates locating the input file to position Designated location . Note that some formats may not support precise positioning , therefore ffmpeg Locate the closest possible to position( Before ) Locatable point of .position It can be a number in seconds or hh:mm:ss[.xxx] The time value of the format . 8、-itsoffset offset (input) Set the time offset of the input file .offset Must be specified in terms of duration , There can be - The time value of number ( A number in seconds or hh:mm:ss[.xxx] The time value of the format ). The offset is appended to the time code of the input file , It means that the specified stream will be in time code + The offset is used as the final output time code . 9、-timestamp date (output) Set the time stamp to be recorded in the container . 10、-codec[:stream_specifier] codec (input/output,per-stream) Select Edit for a specific file / Decoding mode , The output file is the encoder , For an input or a stream, it is the decoder . Option parameter codec Is the name of the codec , Or is it copy( For output files only ) It means that the stream data is copied directly without coding .
Two 、 Video options
1、-vframes number (output) Set the number of frames of the output file , yes -frames:v Another name for . 2、-r[:stream_specifier] fps (input/output,per-stream) Set frame rate ( A kind of Hz value , Abbreviations or fractional values ). 3、-s[:stream_specifier] size (input/output,per-stream) Set the size of the frame . 4、-vn (output) Disable video output . 5、-vcodec codec (output) Set up video encoder , This is a -codec:v An alias for . 6、aspect[:stream_specifier] aspect (output,per-stream) Specifies the aspect ratio of the video ( Length width display scale ).aspect Is a floating-point string or num:den Format string ( Its value is num/den), for example "4:3","16:9","1.3333" as well as "1.7777" Are common parameter values .
Audio options
1、-aframes number (output) Set up number Audio frame output , yes -frames:a Another name for . 2、-ar[:stream_specifier] freq (input/output,per-stream) Set the audio sampling rate . The default is that the output is the same as the input . Set the input , Only the channel is a real device or raw Only the channels separated and mapped by the data are effective . For the output, the adoption rate of audio quantization can be forcibly set . 3、-aq q (output) Set the audio quality ( The encoding is specified as VBR), It is -q:a Another name for . 4、-ac[:stream_specifier] channels (input/output,per-stream) Set the number of audio channels . The default output will have the same audio channel as the input . Set the input , Only the channel is a real device or raw Only the channels separated and mapped by the data are effective . 5、-an (output) Disable audio output . 6、-acode codec (input/output) Set audio decoding / Coding / decoder , yes -codec:a Another name for .
Subtitle options
1、-scodec codec(input/output) Set caption decoder , yes -codec:s Another name for . 2、-sn (output) No subtitles . 3、-fix_sub_duration Fixed caption duration . 4、-canvas_size size Set the size of the caption rendering area ( Location ).
FFmpeg Order sample
1、 Convert format
ffmpeg -i input_test.mp4 -vn -acodec copy output_test.flv ffmpeg -i input_test.aac -vn -acodec copy output_test.mp3
2、 Extract the audio in the picture
ffmpeg -i input_test.mp4 -vn -y -acodec copy output_test.aac ffmpeg -i input_test.mp4 -vn -y -acodec copy output_test.mp3 ffmpeg -i input_test.mp4 -acodec copy -vn output_test.mp3
3、 Extract the video in the picture
ffmpeg -i input_test.mp4 -vcodec copy -an output_test.avi ffmpeg -i input_test.mp4 -vcodec copy -an output_test.mp4
4、 Audio + Video synthesis
ffmpeg -i input_test_1.mp4 -i input_test_2.mp3 -vcodec copy -acodec copy output_test.mp4 ffmpeg -i input_test_1.mp4 -itsoffset 10 -i input_test_2.mp3 -vcodec copy -acodec copy output_test.mp4 ffmpeg -ss 20 -t 5 -i input_test_1.mp4 -i input_test_2.aac -vcodec copy -acodec copy output_test.mp4
The music keeps playing , The video only plays 5 second
ffmpeg -ss 20 -t 5 -i input_test_1.mp3 -i input_test_2.mp4 -vcodec copy -acodec copy output_test.mp4
The video continues to play , Music only plays 5 second
5、 Audio + Audio synthesis
ffmpeg -i input_test_1.mp3 -i input_test_2.mp3 -filter_complex amix=inputs=2:duration=shortest output_test.mp3 ffmpeg -i input_test_1.mp3 -i input_test_2.mp3 -filter_complex amix=inputs=2:duration=longest output_test.mp3 ffmpeg –i input_test_1.mp3 –i input_test_2.mp3 –vcodec copy –acodec copy output_test.mp3
Format :ffmpeg -i INPUT1 -i INPUT2 -i INPUT3 -filter_complex amix=inputs=3:duration=first:dropout_transition=3 OUTPUT
6、 Video is separated into pictures
ffmpeg -i input_test.mp4 -r 1 -f image2 output_image-%03d.jpeg
7、 Picture synthesis video
ffmpeg -f image2 -i output_image-%03d.jpeg output_test.mp4
8、 Change the volume
ffmpeg -i input_test.mp3 -af 'volume=0.5' output_test.mp3
9、 Sound effects fade in and fade out
ffmpeg -i input_test.mp3 -filter_complex afade=t=in:ss=0:d=4 output_test.mp3 Fade in effect : hold input_test.mp3 Before the document 5s Make a fade in and fade out effect , Output to output_test.mp3 In file ffmpeg -i input_test.mp3 -filter_complex afade=t=out:st=20:d=6 output_test.mp3 Fade out effect : take input_test.mp3 File from the 20s Start , do 6s The fade out effect of , Output to output_test.mp3 In file
10、 Capture audio
ffmpeg -ss 10 -i input_test.mp3 -to 20 -vcodec copy -acodec copy output_test.mp3 ffmpeg -ss 10 -i input_test.mp3 -t 5 -vcodec copy -acodec copy output_test.mp3 ffmpeg -i input_test.mp3 -c copy -t 10 -output_ts_offset 120 output_test.mp3
11、 Container duration acquisition
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -i input_test.mp> 3
12、 Network resources download
ffmpeg -i https://xxx.xxx.xxxxxx -c copy -f mp3 output_test.mp3
11、 Play audio and video
ffplay input_test.mp3
12、 Image generation gif Moving graph
ffmpeg -i input_image_%03d.png -r 5 output_test.gif
13、 extract PCM data
ffmpeg -i input_test.mp4 -vn -ar 44100 -ac 2 -f s16le output_test.pcm
The following is from :https://www.ruanyifeng.com/blog/2020/01/ffmpeg.html
One 、 Concept
Introduce FFmpeg Before use , Need to understand some basic concepts of video processing .
1.1 Containers
The video file itself is actually a container (container), It includes video and audio , There may also be subtitles and other content .
Common container formats are as follows . Generally speaking , The suffix of the video file reflects its container format .
MP4 MKV WebM AVI
The following command looks at FFmpeg Supported containers .
$ ffmpeg -formats
1.2 Coding format
Both video and audio need to be encoded , To save as a file . Different coding formats (CODEC), There are different compression rates , Will result in differences in file size and clarity .
The commonly used video coding formats are as follows .
H.262 H.264 H.265
The above coding formats are copyrighted , But you can use it for free . Besides , There are also several non copyright video coding formats .
VP8 VP9 AV1
Common audio coding formats are as follows .
MP3 AAC
All of the above are lossy coding formats , Some details will be lost after coding , In exchange for a smaller file size after compression . Lossless coding format, the compressed file volume is large , I won't introduce it here .
The following commands can be viewed FFmpeg Supported encoding formats , Both video coding and audio coding are included .
$ ffmpeg -codecs
1.3 Encoder
Encoder (encoders) Is a library file that implements a certain encoding format . Only the encoder of some format is installed , To achieve this format video / Audio coding and decoding .
Here are some FFmpeg Built in video encoder .
libx264: The most popular open source H.264 Encoder NVENC: be based on NVIDIA GPU Of H.264 Encoder libx265: Open source HEVC Encoder libvpx: Google's VP8 and VP9 Encoder libaom:AV1 Encoder
The audio encoder is as follows .
libfdk-aac aac
The following commands can be viewed FFmpeg Installed encoder .
$ ffmpeg -encoders
Two 、FFmpeg The use format of
FFmpeg There are a lot of command line arguments , It can be divided into five parts .
$ ffmpeg {1} {2} -i {3} {4} {5}
In the above command , The parameters of the five parts are as follows .
Global parameter Input file parameters Input file Output file parameters The output file
When there are too many parameters , For ease of viewing ,ffmpeg Commands can be written as multiple lines .
$ ffmpeg \ [ Global parameter ] \ [ Input file parameters ] \ -i [ Input file ] \ [ Output file parameters ] \ [ The output file ]
Here is an example .
$ ffmpeg \ -y # Global parameter -c:a libfdk_aac -c:v libx264 # Input file parameters -i input.mp4 # Input file -c:v libvpx-vp9 -c:a libvorbis # Output file parameters output.webm # The output file
The order above will mp4 Document conversion webm file , Both are in container format . Input mp4 The audio encoding format of the file is aac, The video coding format is H.264; Output webm The video encoding format of the file is VP9, The audio format is Vorbis. If the encoding format is not specified ,FFmpeg I will judge the code of the input file by myself . therefore , The above command can be simply written as follows .
$ ffmpeg -i input.avi output.mp4
3、 ... and 、 Common command line parameters
FFmpeg Common command line parameters are as follows .
-c: Specify encoder -c copy: Direct copy , Without recoding ( That's faster ) -c:v: Specify video encoder -c:a: Specify audio encoder -i: Specify input file -an: Remove the audio stream -vn: Remove the video stream -preset: Specifies the video quality of the output , Will affect the file generation speed , There are several values available ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow. -y: Without confirmation , Directly overwrite the file with the same name when exporting .
Four 、 Common use
Let's introduce FFmpeg Several common usages .
4.1 View file information
View the meta information of the video file , Such as coding format and bit rate , You can just use -i Parameters .
$ ffmpeg -i input.mp4
The above command will output a lot of redundant information , add -hide_banner Parameters , You can display only meta information .
$ ffmpeg -i input.mp4 -hide_banner
4.2 Transform the encoding format
Transform the encoding format (transcoding) refer to , Convert video files from one encoding to another . Like turning into H.264 code , Encoder is generally used libx264, Therefore, you only need to specify the video encoder of the output file .
$ ffmpeg -i [input.file] -c:v libx264 output.mp4
Here is the conversion H.265 How to write the code .
$ ffmpeg -i [input.file] -c:v libx265 output.mp4
4.3 Convert container format
Convert container format (transmuxing) refer to , Transfer video files from one container to another . Here is mp4 turn webm Writing .
$ ffmpeg -i input.mp4 -c copy output.webm
In the example above , Just turn the container , The internal coding format remains unchanged , So use -c copy Specify direct copy , No transcoding , That's faster .
4.4 Adjust the bit rate
Adjust the bit rate (transrating) refer to , Change the bit rate of the encoding , It is generally used to reduce the volume of video files . The following example specifies that the minimum bit rate is 964K, The maximum is 3856K, The buffer size is 2000K.
$ ffmpeg \ -i input.mp4 \ -minrate 964K -maxrate 3856K -bufsize 2000K \ output.mp4
4.5 Change resolution (transsizing)
Here's how to change the video resolution (transsizing) Example , from 1080p To 480p .
$ ffmpeg \ -i input.mp4 \ -vf scale=480:-1 \ output.mp4
4.6 Extract audio
Sometimes , You need to extract audio from the video (demuxing), You can write like this .
$ ffmpeg \ -i input.mp4 \ -vn -c:a copy \ output.aac
In the example above ,-vn It means to remove the video ,-c:a copy Indicates that the audio coding will not be changed , Direct copy .
4.7 Add track
Add track (muxing) refer to , Add external audio to video , Such as adding background music or narration .
$ ffmpeg \ -i input.aac -i input.mp4 \ output.mp4
In the example above , There are audio and video input files ,FFmpeg They will be combined into a file .
4.8 Screenshot
The following example starts from the specified time , Continuous to 1 Take a screenshot of the second video .
$ ffmpeg \ -y \ -i input.mp4 \ -ss 00:01:24 -t 00:00:01 \ output_%3d.jpg
If you only need to cut a picture , You can specify that only one frame be intercepted .
$ ffmpeg \ -ss 01:23:45 \ -i input \ -vframes 1 -q:v 2 \ output.jpg
In the example above ,-vframes 1 Specifies that only one frame is intercepted ,-q:v 2 Indicates the quality of the output picture , It's usually 1 To 5 Between (1 For the highest quality ).
4.9 tailoring
tailoring (cutting) refer to , Capture a clip from the original video , Output as a new video . You can specify the start time (start) And duration (duration), You can also specify the end time (end).
ffmpeg -ss [start] -i [input] -t [duration] -c copy [output] ffmpeg -ss [start] -i [input] -to [end] -c copy [output]
Here is a practical example .
ffmpeg -ss 00:01:50 -i [input] -t 10.5 -c copy [output] ffmpeg -ss 2.5 -i [input] -to 10 -c copy [output]
In the example above ,-c copy Indicates that the encoding format of audio and video will not be changed , Direct copy , It will be much faster .
4.10 Add cover for audio
Some video websites only allow uploading video files . If you want to upload an audio file , You must add a cover for the audio , Turn it into a video , And then upload . The following command can convert the audio file , Convert to video file with cover .
$ ffmpeg \ -loop 1 \ -i cover.jpg -i input.mp3 \ -c:v libx264 -c:a aac -b:a 192k -shortest \ output.mp4
In the above command , There are two input files , One is the cover picture cover.jpg, The other is audio files input.mp3.-loop 1 The parameter represents the infinite loop of the picture ,-shortest Parameter indicates the end of the audio file , The output video is over .
边栏推荐
- 程序人生 | 测试工程师还只会点点点?7个捷径教给你快速学习新技术...
- 【无标题】
- Regression - linear regression
- Lightpicture - exquisite drawing bed system
- JDBC usage
- Recursion and non recursion are used to calculate the nth Fibonacci number respectively
- 巧用栈回溯,帮你快速定位问题
- Read Plato farm's eplato and the reason for its high premium
- 企业数字化建设“三不五要”原则
- 数据挖掘-01
猜你喜欢

Dynamic programming - 509. Fibonacci number

【图像分类】2021-MLP-Mixer NIPS

Embedded development: tips and techniques -- the best practice of defensive programming with C

Appnium--APP自动化测试工具

Cookies and session

Crowdfunding platform system based on JSP & Servlet

Notes on writing questions in sword finger offer

ftp服务器、nfs服务器的搭建和使用

security异常处理机制

21天,胖哥亲自带你玩转OAuth2
随机推荐
Crowdfunding platform system based on JSP & Servlet
一名合格的软件测试工程师,应该具备哪些技术能力?
Build an "industrial brain" and improve the park's operation, management and service capabilities with "digitalization"!
月薪28K学员 自动化测试经验分享
R notes mice
离职前一定要做好这7件事情,少一件都很麻烦。
We must do these seven things well before leaving, and it's troublesome to do one less thing.
ServletContext、request、response
LeetCode 0140. 单词拆分 II
Several ways of connecting upper computer and MES
21 days, fat brother personally takes you to play oauth2
jdbc使用
C language: find the number of 1 in binary stored in memory as an integer
WordPress simple mkblog blog theme template v2.1
JDBC usage
Linux - MySQL advanced (day19)
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (the last 8 questions)
Analysis of static broadcast transmission process
面试必备杀技:SQL查询专项训练!
Detailed explanation of pointer written test questions (C language)