1 FFmpeg是什么
ffmpeg是一套用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它包含了非常先进的音频/视频编解码库libavcodec,提供了录制、转换以及流化音视频的完整解决方案。
许多FFmpeg的开发人员都来自MPlayer项目,而且当前FFmpeg也是放在MPlayer项目组的服务器上。项目名称来自MPEG视频编码标准,前面的"FF"代表"Fast Forward",即Fast forword mpeg,音视频转换器。
github地址
https://github.com/FFmpeg/FFmpeg
2 FFmpeg能做什么
1 色彩转换、缩放视频尺寸、视频抽帧、去噪、模糊、压缩、剪切、转码、截图、旋转、添加/去除水印、播放速度、封面、转换3D的VR视频、 图片转视频
2 音频转码、剪切、拼接、各类音效、音量、抽象音频、音乐频谱波形图
3 安装FFmpeg
//mac
brew search ffmpeg
brew install ffmpeg
//linux
yum install ffmpeg
sudo apt install ffmpeg
验证安装
tom@bogon ~ % ffmpeg -version
ffmpeg version 6.0 Copyright (c) 2000-2023 the FFmpeg developers
built with Apple clang version 14.0.0 (clang-1400.0.29.202)
configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/6.0 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-neon
libavutil 58. 2.100 / 58. 2.100
libavcodec 60. 3.100 / 60. 3.100
libavformat 60. 3.100 / 60. 3.100
libavdevice 60. 1.100 / 60. 1.100
libavfilter 9. 3.100 / 9. 3.100
libswscale 7. 1.100 / 7. 1.100
libswresample 4. 10.100 / 4. 10.100
libpostproc 57. 1.100 / 57. 1.100
版本信息里有ffmpeg的版本6.0,平台apple,配置configuration以及核心模块
4 核心模块
名称 | 说明 |
---|---|
libavutil | 工具库,比如算数运算、字符操作等 |
libavcodec | 音视频编解码核心库 |
libavformat | I/O操作和封装格式(muxer/demuxer)处理 |
libavdevice | 各种设备的输入输出 |
libavfilter | 滤镜特效处理 |
libswscale | 视频像素处理,包括缩放图像尺寸、色彩映射转换、像素颜色空间转换等 |
libswresample | 音频重采样,格式转换和混音 |
libpostproc | 后期效果处理,如图像的去块效应 |
一般转码过程
5 常见参数说明
Per-file main options:
-f fmt force format
-c codec codec name
-codec codec codec name
-pre preset preset name
-map_metadata outfile[,metadata]:infile[,metadata] set metadata information of outfile from infile
-t duration record or transcode "duration" seconds of audio/video
-to time_stop record or transcode stop time
-fs limit_size set the limit file size in bytes
-ss time_off set the start time offset
-sseof time_off set the start time offset relative to EOF
-seek_timestamp enable/disable seeking by timestamp with -ss
-timestamp time set the recording timestamp ('now' to set the current time)
-metadata string=string add metadata
-program title=string:st=number... add program with specified streams
-target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad audio pad
-frames number set the number of frames to output
-filter filter_graph set stream filtergraph
-filter_script filename read stream filtergraph description from a file
-reinit_filter reinit filtergraph on input parameter changes
-discard discard
-disposition disposition
Video options:
-vframes number set the number of video frames to output
-r rate set frame rate (Hz value, fraction or abbreviation)
-fpsmax rate set max frame rate (Hz value, fraction or abbreviation)
-s size set frame size (WxH or abbreviation)
-aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-display_rotation angle set pure counter-clockwise rotation in degrees for stream(s)
-display_hflip set display horizontal flip for stream(s) (overrides any display rotation if it is not set)
-display_vflip set display vertical flip for stream(s) (overrides any display rotation if it is not set)
-vn disable video
-vcodec codec force video codec ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff set initial TimeCode value.
-pass n select the pass number (1 to 3)
-vf filter_graph set video filters
-b bitrate video bitrate (please use -b:v)
-dn disable data
Audio options:
-aframes number set the number of audio frames to output
-aq quality set audio quality (codec-specific)
-ar rate set audio sampling rate (in Hz)
-ac channels set number of audio channels
-an disable audio
-acodec codec force audio codec ('copy' to copy stream)
-ab bitrate audio bitrate (please use -b:a)
-af filter_graph set audio filters
6 简单Demo
6.1 图片生成视频
ffmpeg -r 25 -f image2 -i ./xuanmu/activity_%d.png -vcodec libx265 -vtag hvc1 -crf 25 -pix_fmt yuv420p out.mp4 -y
6.2 视频旋转90度
ffmpeg -i out.mp4 -vf transpose=1 test.mp4
6.3 视频翻转
ffmpeg -i input.mp4 -vf reverse -an output.mp4
6.4 视频截图
ffmpeg -ss 0 -i input.mp4 -f image2 -vframes 1 -an screenshot.jpg
6.5 视频水印
ffmpeg -i qd.mp4 -i 46x46.png -filter_complex overlay=10:20 watermark.mp4
6.6 转换3D的VR视频
ffmpeg -i qd.mp4 -filter_complex stereo3d=sbsl:arbg stereo3d.mp4 -y
6.7 音频混音
ffmpeg -i 1.mp3 -i 2.mp3 -filter_complex amix=inputs=2:duration=first -vn mix.mp3
6.8 空灵音效(没太听出来效果)
ffmpeg -i input.mp3 -af aecho=0.8:0.8:1000:0.5 echo.mp3
6.9 搞笑音效
ffmpeg -i 1.mp3 -filter_complex atempo=2 atempo.mp3
6.10 音乐频谱波形图(安河桥)
ffmpeg -i 1.mp3 -filter_complex showwavespic=s=1280x720 waveform.png
6.11 音频拼接
ffmpeg -i concat:"1.mp3|2.mp3" -acodec copy -vn concat.mp3
6.12 视频自定义宽高
ffmpeg -i qd.mp4 -s 720x480 -threads 4 -c:a copy target.mp4 -y
参考文档
1 https://trac.ffmpeg.org/wiki/TitleIndex
2 https://zhuanlan.zhihu.com/p/37516093
3 ffmpeg official document
4 https://blog.csdn.net/u011686167/article/details/120879467
5 https://blog.csdn.net/u011686167/article/details/120879467