Skip to content

Instantly share code, notes, and snippets.

@rayworks
Last active October 28, 2021 15:21
Show Gist options
  • Save rayworks/15ba92c2e93bc6b4d6270d52942313e3 to your computer and use it in GitHub Desktop.
Save rayworks/15ba92c2e93bc6b4d6270d52942313e3 to your computer and use it in GitHub Desktop.
ffmpeg in practice
1. Compile configure
configuration: --enable-libass --enable-gpl --enable-libfreetype --enable-libvpx --enable-libx264 --enable-libmp3lame
--enable-libspeex --enable-libx265 --enable-nonfree --enable-videotoolbox --enable-audiotoolbox --enable-fontconfig
--enable-libfdk-aac
2. Add subtitles(字幕)
./ffmpeg -i input.mp4 -vf subtitles=your.srt output.mp4
3. Specify subtitles when playing the video
./ffplay -vf subtitles=your.srt input.mp4
4. Convert subtitles
./ffmpeg -i s2.lrc sun2.srt
5. Cut the video (https://www.jianshu.com/p/eff314074177)
ffmpeg -ss 00:00:00 -t 00:00:30 -i test.mp4 -vcodec copy -acodec copy output.mp4
* -ss 指定从什么时间开始
* -t 指定需要截取多长时间
* -i 指定输入文件
//截取从头开始的30s
ffmpeg -ss 00:00:00 -t 00:00:30 -i keyoutput.mp4 -vcodec copy -acodec copy split.mp4
//截取从30s开始的30s
ffmpeg -ss 00:00:30 -t 00:00:30 -i keyoutput.mp4 -vcodec copy -acodec copy split1.mp4
6. Merge the video
ffmpeg -f concat -i list.txt -c copy concat.mp4
list.txt:
file split.mp4
file split1.mp4
7. Multithread config
可以用-threads n 来实施多线程的运算,充分利用多核cpu
eg:
ffmpeg -threads 2 -crf 20 -y -i ML-02.avi -strict experimental ML-02.mp4
8. bitrate (Also see https://www.cnblogs.com/frost-yen/p/5848781.html)
eg: => 4Mbps
ffmpeg -threads 2 -i concat.mp4 -b:v 4000k -bufsize 4000k output.mp4
9. Change the resolution (更新视频文件分辨率)
ffmpeg -i video_320x180.mp4 -vf scale=160:90 video_160x90.mp4
10. 文件格式转换 [MOV -> MP4]
The command to just stream it to a new container (mp4) needed by some applications like Adobe Premiere Pro without encoding (fast) is:
ffmpeg -i input.mov -qscale 0 output.mp4
Alternative as mentioned in the comments, which re-encodes with best quaility (-qscale 0):
ffmpeg -i input.mov -q:v 0 output.mp4
11. 添加图片到视频
ffmpeg -i input.mp4 -i test.png -filter_complex "[0:v][1:v] overlay=350:350:enable='between(t,0,15)'"
-pix_fmt yuv420p -c:a copy output_pic.mp4
Note :
overlay=25:25 means we want to position the image 25px to the right and 25px down, originating from the top left corner (0:0).
enable='between(t,0,20)' means we want the image to show between second 0 and 20.
[0:v][1:v] means that we want the first video file we import with -i, in our case input.mp4 or how ffmpeg sees it,
video input file number 0, to be under video input file 1, in our case image.png. :v just means we want the video
track from these file sources. [0:a] would mean we want the first imported audio track. Which would also come from
input.mp4 but would point to the audio track instead of the video track in the mp4 file.
Source: https://video.stackexchange.com/questions/12105/add-an-image-overlay-in-front-of-video-using-ffmpeg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment