Skip to content

Instantly share code, notes, and snippets.

@LuoZijun
Last active April 4, 2022 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuoZijun/f38af8370a4dac9818351f6cc0446db2 to your computer and use it in GitHub Desktop.
Save LuoZijun/f38af8370a4dac9818351f6cc0446db2 to your computer and use it in GitHub Desktop.
FFMPEG 添加时间戳水印

FFMPEG 添加时间戳水印

安装 FFMPEG

# 如果已安装的 FFMPEG 已经支持了 `freetype` ,那么就不需要再安装了。
brew reinstall ffmpeg --with-sdl2 --with-freetype

添加时间戳水印

wget "https://github.com/LuoZijun/rustpy_demo/raw/master/data/Secret%20World%20of%20US%20Election%20-%20Julian%20Assange%20talks%20to%20John%20Pilger%20(FULL%20INTERVIEW)-_sbT3_9dJY4.mp4" -o in.mp4
ffmpeg -i in.mp4 \
    -vf "drawtext=fontfile=/Library/Fonts/Times New Roman.ttf: text='%{localtime\:%Y\/%m\/%d %H\/%M\/%S}': r=25: x=16: y=48: fontsize=30: fontcolor=white@0.8: box=1: boxcolor=black@1" \
    -preset medium \
    -an \
    -y out.mp4

参考

@gMan1990
Copy link

ffmpeg内部有可以获得本片时长的变量吗?
比如加水印 当前时间点/总时长 00:01:00/01:23:45

@LuoZijun
Copy link
Author

内部 API 肯定是可以获取时长变量的。
所以你大概可以通过组合命令行的形式来。先获取时长数据,并格式化成你想要的格式,如下面的方式,得到秒数:

ffmpeg -i in.mp4 2>&1 \
    | grep "Duration" \
    | cut -d ' ' -f 4 \
    | sed s/,// \
    | sed 's@\..*@@g' \
    | awk '{ split($1, A, ":"); split(A[3], B, "."); print 3600*A[1] + 60*A[2] + B[1] }'

然后把上面的输出通过管道和上面打水印的命令连接起来应该就可以了。

当然如果你通过调用 FFMPEG 的 API 来编程的话,那就更灵活了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment