Ffmpeg基础命令行

如果只是想处理视频或图片,不需要在代码逻辑中进行处理,那么使用ffmpeg的命令行工具是首选。

去官网下载编译好的程序,https://ffmpeg.org/download.html。无外部依赖项,下载下来可以直接用。

支持各种命令和功能,我这里仅列举一些自己常用的:

ffmpeg -i input.mp4 output.avi 转换格式,用的最多

ffmpeg -i input.mp4 image%03d.jpg 视频逐帧提取图像

ffmpeg -ss 00:01:00.0 -i video.mp4 -t 00:02:00.0 cut.mp4 剪切视频(时间)
说明:先使用-ss定位开始点增加效率,-t表示duration。时间格式是HH:MM:SS.MS。如果有-c copy是直接拷贝,没有就是重新encode。尝试过copy但是总发现末尾有点问题,所以干脆encode好了

ffmpeg -i input.avi -vf scale=320:240 output.avi 调整视频大小

ffmpeg -i in.mp4 -filter:v “crop=out_w:out_h:x:y” out.mp4 剪切视频(大小)
out_w is the width of the output rectangle
out_h is the height of the output rectangle
x and y specify the top left corner of the output rectangle

ffmpeg -i input.mp4 -c:v mpeg1video -c:a mp2 output.mpg 将视频转为mpeg1格式

反正你能想到的视频、音频和图像操作都支持,可以上网自行搜索。