图片来源: https://unsplash.com/@ormphotography
FFmpeg来源, 引自wikipedia
FFmpeg is a free and open-source project consisting of a vast software suite of libraries and programs for handling video, audio, and other multimedia files and streams. At its core is the FFmpeg program itself, designed for command-line-based processing of video and audio files, and widely used for format transcoding, basic editing (trimming and concatenation), video scaling, video post-production effects, and standards compliance (SMPTE, ITU). FFmpeg includes libavcodec, an audio/video codec library used by many commercial and free software products, libavformat (Lavf), mux and demux library, and the core ffmpeg command line program for transcoding multimedia files. FFmpeg is published under the GNU Lesser General Public License2.1+ or GNU General Public License 2+ (depending on which options are enabled).
大体意思是ffmpeg是一个很牛逼的音视频处理软件库, 并且是开源免费的.
比较有趣的是ffmpeg的作者是一个非常牛X的人物, 他开挂的人生充满了色彩——ffmpeg的作者、π小数点算的最多的一个人、上个月又开发了一款微型JS引擎QuickJS, 有兴趣的可以深入了解一下他, 这是他的blog。
常用命令
关于基础的只是我不多讲了,需要学习的可以去这里,这个大神整理了中文文档
废话不多说直接列出常用命令(持续更新, 欢迎交流):
-
查看音视频信息
ffmpeg -i input.mp4
-
去掉ffmpeg每次启动的提示
// 命令都加上hide_banner,就会取消掉的提示信息 ffmpeg -hide_banner ***
-
从视频中抽取出纯音频和纯视频
// 抽取音频 ffmpeg -i test.mp4 -vn -y -acodec copy test.aac // 抽取视频 ffmpeg -i test.mp4 -an -y -acodec copy slience.mp4
-
音频视频合成
ffmpeg -i video.mp4 -i audio.mp3 -vcodec copy -acodec copy output.avi
-
截取部分视频/音频
// 截取0-30秒的视频 ffmpeg -i input.mp4 -ss 00:00:00 -t 00:00:30 -codec copy output.mp4
-
多个视频合成一个
//将多个视频名写入文件 for f in ./*.mp4; do echo “file ‘$f'” >> mylist.txt; done // concat直接拼接 fmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
-
多个音频合成一个
ffmpeg -i input1.mp3 -i input2.mp3 -i input3.mp3 -filter_complex “amix=inputs=3:duration=first:dropout_transition=2" output.mp3 // 想要其中某一个音频延迟10秒加载音频 ffmpeg -i input1.mp3 -i input2.mp3 -filter_complex “[1]adelay=10|10[a];[0][a]amix=inputs=2:duration=first:dropout_transition=2” -y output.mp3
-
消音视频的后几秒
// 只取视频的前十秒的声音,其他地方静音 ffmpeg -i input.mp4 -ss 00:00:00 -t 00:00:10 -i input.mp4 -map 0:v:0 -map 1:a:0 -y output.mp4
-
变速
// 视频变速 ffmpeg -i input.mp4 -filter:v setpts=0.5*PTS output.mp4 // 音频变速 ffmpeg -i input.mp3 -filter:a atempo=2.0 output.mp3
-
视频画中画
// 覆盖在视频的右下角 ffmpeg -y -i input1.mp4 -i input2.mp4 -filter_complex "[0:v][1:v]overlay=W-w:H-h" output.mp4