ffmpeg的常见命令合集
`ffmpeg` 是一个强大的多媒体处理工具,可以用来转换视频、音频文件的格式,提取音轨,裁剪和合并媒体文件等。
下面是一些常用的 `ffmpeg` 命令示例:
1. 视频格式转换
将一个视频文件从一种格式转换为另一种格式,比如从 `.avi` 转换到 `.mp4`:
bash
ffmpeg -i input.avi output.mp4
2. 提取音频
从视频中提取音频并保存为 MP3 文件:
bash
ffmpeg -i video.mkv -q:a 0 -map a output.mp3
或者提取无损音频流(例如 AAC):
bash
ffmpeg -i video.mp4 -c:a copy output.aac
3. 视频编码
使用 H.264 编码器对视频进行重新编码:
bash
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium output.mp4
- `-crf` 设置恒定质量,值越低质量越高,范围是 0–51。
- `-preset` 设置编码速度与压缩比之间的平衡,选项有 ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow。
4. 音频编码
将音频文件转换为不同的编码格式或调整比特率:
bash
ffmpeg -i input.flac -b:a 192k output.mp3
5. 截取视频片段
从指定时间开始截取一段固定长度的视频:
bash
ffmpeg -ss 00:01:30 -i input.mp4 -t 00:00:20 -c copy output.mp4
- `-ss` 指定开始时间。
- `-t` 指定持续时间。
6. 合并视频
合并多个相同格式的视频文件(假设没有复杂的转场效果):
bash
ffmpeg -f concat -safe 0 -i <(for f in *.mp4; do echo "file '$PWD/$f'"; done) -c copy output.mp4
或者创建一个文本文件列出所有要合并的文件:
bash
file 'part1.mp4'
file 'part2.mp4'
file 'part3.mp4'
然后运行命令:
bash
ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4
7. 添加字幕
将 SRT 字幕文件嵌入到视频中:
bash
ffmpeg -i input.mp4 -vf subtitles=subtitles.srt output.mp4
8. 更改分辨率
调整视频的分辨率大小:
bash
ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4
9. 屏幕录制
录制屏幕(需要安装 x11grab):
bash
ffmpeg -video_size 1920x1080 -framerate 25 -f x11grab -i :0.0 -c:v libx264 output.mp4
10. 图片序列转视频
将一系列图片(如 PNG 文件)转换成视频:
bash
ffmpeg -framerate 25 -i img%03d.png -c:v libx264 -pix_fmt yuv420p output.mp4
还有一些常见的命令,我们可以总结一下
!! Transform videos to picture .\ffmpeg.exe -i input.mp4 -vf fps=1 output%d.png !! Change size of videos or images .\ffmpeg.exe -i input.mp4 -vf scale=320:240 output.mp4 !! Combine/stack two videos or images .\ffmpeg.exe -i part1.mp4 -i part2.mp4 -filter_complex vstack=inputs=2 output.mp4 .\ffmpeg.exe -i part1.mp4 -i part2.mp4 -filter_complex hstack=inputs=2 output.mp4 !! Combine images to videos .\ffmpeg.exe -r 100 -f image2 -s 500x500 -i pic%00d.png -vcodec libx264 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -crf 25 -pix_fmt yuv420p output.mp4 !! Combine/stack two videos or images and change size .\ffmpeg.exe -i part1.mp4 -i part1.mp4 -filter_complex "[0:v]pad=iw*2:ih*1[a];[a][1:v]overlay=w" output.mp4 !! Crop videos or images !! 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.exe -i input.mp4 -strict -2 -vf crop=600:796:200:0 output.mp4 !! ctspecimen !! .\ffmpeg.exe -i input.mp4 -strict -2 -vf crop=1612:300:0:300 output.mp4 !! ASTM_E606 .\ffmpeg.exe -i a.mov -strict -2 -vf crop=out_w:out_h:x:y output.mp4 !! Combine/stack three videos or images .\ffmpeg.exe -i part1.mp4 -i part2.mp4 -i out3.mp4 -filter_complex "[0:v]pad=iw*3:ih*1[a];[a][1:v]overlay=w[b];[b][2:v]overlay=2.0*w" output.mp4 !! Speed up videos .\ffmpeg.exe -i input.mp4 -filter:v "setpts=PTS/60" output.mp4 !! Cut videos .\ffmpeg.exe -i input.mp4 -ss 00:00:00 -t 00:00:10 -async 1 output.mp4 !! remove voice .\ffmpeg.exe -i input.mp4 -c copy -an output.mp4 !! compress video .\ffmpeg.exe -i input.mp4 -vcodec libx265 -crf 28 output.mp4 !! concat video .\ffmpeg.exe -f concat -safe 0 -i join.txt -c copy output.mp4 !! join.txt !! file part1.mp4 !! file part2.mp4 !! file part3.mp4 !! file part4.mp4 !! Merging video and audio, with audio re-encoding .\ffmpeg.exe -i input.mp4 -i audio.wav -c:v copy -c:a aac output.mp4 !! Speed up videos .\ffmpeg.exe -i input.mp4 -filter:v "setpts=0.5*PTS" output.mp4 !! change videos format .\ffmpeg.exe -i input.mp4 -c:v libx264 -ar 22050 -crf 28 output.flv !! change videos size .\ffmpeg.exe -i input.mp4 -vf scale=-1:720 -c:v libx264 -crf 18 -preset veryslow -c:a copy output.mp