主要记录视频的一些相关知识以及 FFmpeg 和 DaVinci 的常见用法
FFmpeg - Video
File Format Conversion
For most simple tasks, you can just do
1 | ffmpeg -i input.avi output.mp4 |
and ffmpeg
will figure out how to perform that conversion.
-
1
2for f in *.flv; do echo "file '$f'" >> mylist.txt; done
ffmpeg -f concat -i mylist.txt -c copy output.flv更详细的官方文档在这里。具体地来看,我们这里用的是 concat demuxer, 这项协议支持不同的容器格式,甚至是本身不支持 concat 操作的容器格式的合并
1
ffmpeg -i "concat:input1|input2" -codec copy output.mkv
对于 ts 之类本身支持 file-level concat 的文件格式,可以直接使用如上的 concat protocol (参考 stackoverflow 答案1, 答案2)
-
1
2
3
4
5# stream copy all streams
ffmpeg -i input -map 0 -c copy output.mp4
# re-encode the video to H.264 and stream copy the audio
ffmpeg -i input.ts -c:v libx264 -c:a copy output.mp4 -
1
2
3
4
5
for i in *.avi;
do
ffmpeg -i "$i" -c:v libx265 -c:a copy X265_"$i"
done
Editing, Clipping, Encoding
Extract Audio from Video: where
-vn
disables video processing1
2ffmpeg -i input.mp4 -vn -acodec copy output-audio.aac # extract aac
ffmpeg -i input.mp4 -vn -acodec copy output-audio.opus # extract opusFFmpeg查看媒体信息: 使用
ffprobe
Extract Subtitles: On hdmv_pgs_subtitle, where we need sup format.
1
2ffmpeg -i video.mkv -map 0:s:0 -c copy subs.sup
ffmpeg -i Movie.mkv -map 0:s:0 -c copy subs.srtFFmpeg H.265 Reencode: if you use all the settings as default, just do:
1
ffmpeg -i input -c:v libx265 -c:a copy output.mp4
Clip Video: you should primarily read this wiki page, which introduces you to seeking in ffmpeg. In short, you should use
-ss
before-i
in most cases.1
2
3
4
5
6
7
8
9
10
11# -ss used before -i: parse input using keyframes, which is very fast
# gives exactly the same output
ffmpeg -ss 00:40:30 -i 20170301.mp4 -t 310 -c copy 1.mp4
ffmpeg -ss 00:40:30 -to 00:45:40 -i 20170301.mp4 -c copy 2.mp4
# -ss used after -i: decodes but discards input until the timestamps reach position.
# which is done very slowly, frame by frame
ffmpeg -i 20170301.mp4 -ss 00:40:30 -to 00:45:40 -c copy 3.mp4
# Doesn't work, will output something at most 45:40 long
ffmpeg -ss 00:40:30 -i 20170301.mp4 -to 00:45:40 -c copy 4.mp4
Broken File Fix
Fill Missing Time Stamps with Empty Audio:
1
ffmpeg -i input -af aresample=async=1 output.wav
Fill Missing Frames (Change Variable Frame to Constant Frame):
1
ffmpeg -i input -vf "fps=30" output.mp4
-
1
ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -i video.mov -c:v copy -c:a aac -shortest output.mov
Others
ffmpeg download m3u8 file with custome user-agent: If flag
-user_agent
is not working, you can use-headers
, referenced here1
2ffmpeg -user_agent "SNH48 ENGINE" -i "https://xxx.m3u8" -codec copy file.mp4
ffmpeg -headers 'User-Agent: Mozilla' -i "https://xxx.m3u8" -codec copy file.mp4
FFmpeg - Audio
!!!整理!!!
https://stackoverflow.com/questions/46508055/using-ffmpeg-to-cut-audio-from-to-position
1 | ffmpeg -ss 60 -i presentation.aac -t 240 -c copy presentation_song.aac 好用 |
convert file format to
.ogg
with specififed sample rate: here we specified sample rate to be 44.1K1
ffmpeg -i input.wav -c:a libvorbis -ar 44100 output.ogg
1 | ffmpeg -framerate 30 -i z_Blue1_1_60_%d.png -c:v libx264 -r 30 output.mp4 |
DaVinci
- Import and Bake Subtitles in DaVinci
- Zoom In and Zoom Out in DaVinci: achieve with key frames
- How to Fade in and Out Video
- Vertical timeline (Tiktok style)
- How to Control Audio Volume Levels
编码解码格式
用 IDM 下载 YouTube 上视频会有两种格式 mkv 和 mp4 两种格式,结论:mkv 格式更优
都下载下来后使用 PotPlayer 播放时查看发现
- mkv 格式需要使用 FFmpeg libdav1d decoder
- mp4 格式需要使用 FFmpeg h264 decoder
使用 ffprobe 发现
- mkv 格式使用 av1 格式编码
- mp4 格式使用 h264 格式编码
查询资料得知 av1 是谷歌新发布的编解码规范,相比 h265 压缩优势都很大,就不用说 h264 了。IDM 官方也推荐使用 av1 编码的 mkv 格式。(官方FAQ: Can I download MP4 instead of MKV or what should I do to play MKV videos correctly? | I cannot play downloaded MKV video. What should I do?)
One caveat: Google seems to be using vp9 as the encoder of live stream, but this is still better than h264 in mp4.
对于 SNH48 公演录播源,发现官网源使用 h264 ts 编码,YouTube 源大概是单独推流,谷歌编码为 vp9,且 YouTube 源有 1080P,官网只有720P。故优先选择 YouTube 源 (但是后来发现 YouTube 源好像音频是 opus 格式,Davinci 识别不了,最后用的还是别人的 bilibili 源)