Yao Lirong's Blog

Video Editing (FFmpeg DaVinci)

2022/04/09

主要记录视频的一些相关知识以及 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.

  • FFmpeg 合并文件:

    1
    2
    for 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)

  • FFmpeg 转换格式:

    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
  • FFmpeg 批处理转换格式:

    1
    2
    3
    4
    5
    #!/bin/bash
    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 processing

    1
    2
    ffmpeg -i input.mp4 -vn -acodec copy output-audio.aac # extract aac
    ffmpeg -i input.mp4 -vn -acodec copy output-audio.opus # extract opus
  • FFmpeg查看媒体信息: 使用 ffprobe

  • Extract Subtitles: On hdmv_pgs_subtitle, where we need sup format.

    1
    2
    ffmpeg -i video.mkv -map 0:s:0 -c copy subs.sup
    ffmpeg -i Movie.mkv -map 0:s:0 -c copy subs.srt
  • FFmpeg 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

Others

  • ffmpeg download m3u8 file with custome user-agent: If flag -user_agent is not working, you can use -headers, referenced here

    1
    2
    ffmpeg -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

https://superuser.com/questions/1041816/combine-one-image-one-audio-file-to-make-one-video-using-ffmpeg

FFmpeg - Audio

!!!整理!!!

https://stackoverflow.com/questions/46508055/using-ffmpeg-to-cut-audio-from-to-position

https://stackoverflow.com/questions/71158575/output-file-is-empty-nothing-was-encoded-check-ss-t-frames-parameters-i

1
2
3
ffmpeg -ss 60 -i presentation.aac -t 240 -c copy presentation_song.aac 好用

ffmpeg -ss 60 -t 240 -i presentation.aac -c copy presentation_song.aac 不好用
  • convert file format to .ogg with specififed sample rate: here we specified sample rate to be 44.1K

    1
    ffmpeg -i input.wav -c:a libvorbis -ar 44100 output.ogg
1
2
3
4
5
6
7
ffmpeg -framerate 30 -i z_Blue1_1_60_%d.png -c:v libx264 -r 30 output.mp4

for color in "Blue" "Red" "Green" "Yellow"; do for ((i=1; i<=3; i++)); do printf "%s%d_1_60.png\n" "$color" "$i"; done; done

ffmpeg -framerate 15 -i z_Blue1_1_60.png_%d.png -c:v libx264 -r 15 output.mp4

记得看 aphelion-defense/assets/textures/60/test.sh

DaVinci

编码解码格式

  • 用 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 源)

CATALOG
  1. 1. FFmpeg - Video
    1. 1.1. File Format Conversion
    2. 1.2. Editing, Clipping, Encoding
    3. 1.3. Broken File Fix
    4. 1.4. Others
  2. 2. FFmpeg - Audio
  3. 3. DaVinci
  4. 4. 编码解码格式