使用 FFMPEG:如何进行场景变化检测?带时间码?

Using FFMPEG: How to do a Scene Change Detection? with timecode?

根据这篇文章,似乎可以使用 FFMPEG 检测视频中的场景变化: http://www.luckydinosaur.com/u/ffmpeg-scene-change-detector

现在我有一个显示书籍文本的视频,当说出文本(单词或句子)时,它会突出显示。 类似于这本有声读物:https://youtu.be/lA7L6ZNVKjc

我需要知道突出显示文本(因此场景发生变化)时的时间戳,这样我就可以在我的 YouTube 视频上添加时间戳标签,这样听众就可以更轻松地浏览有声读物。

什么是 magic 命令行可以做到这一点?

非常感谢!

结合 scene filter (for detecting scene changes) and the showinfo 过滤器应该可以达到你想要的效果:

ffmpeg -i input.flv  \
       -filter:v "select='gt(scene,0.4)',showinfo" \
       -f null \
       - 2> ffout

此命令提取所有与前一帧相差超过 (gt) 0.4 的帧(范围从 01)。对于这些帧,信息是这样打印出来的(showinfo

[Parsed_showinfo_1 @ 0x2d85e60] n:   0 pts:2537204 pts_time:2.5372  pos:  2998114 fmt:rgb24 sar:1/1 s:1920x1200 i:P iskey:1 type:I checksum:5616582E plane_checksum:[5616582E]

现在你只需要提取时间戳。我认为您对 pts_time 感兴趣。你可以这样做:

grep showinfo ffout | grep pts_time:[0-9.]* -o | grep [0-9.]* -o > timestamps

这将为您提供所有时间戳的列表:

2.5372
4.37799
6.65301
8.09344

要使此方法起作用,您必须具有实现场景检测的 FFmpeg 版本。此外,您必须 select 一个合适的阈值(第一个命令中的 0.4 )。您可以尝试通过提取不同阈值的帧(然后手动检查帧)来找到最佳阈值,如下所示

ffmpeg -i input.flv \
       -filter:v "select='gt(scene,0.1)',showinfo" \
       -vsync 0 frames/%05d.jpg

澄清一下:grep [0-9.]* 不排除另一个答案中声称的整数。它匹配任何由数字和句点组成的字符序列,但它也匹配非数字,如“4.4.4”。但是,ffmpeg 不应该输出这种格式错误的时间戳。

我没有 post 的代表对上述答案发表评论,但我想指出由@ckoehn 和@keypulsations 编辑的 grep post 只会获取时间戳这是浮点数。要获取浮点和整数时间戳,请使用以下正则表达式

grep showinfo ffout | grep pts_time:[0-9.]* -o | grep -E '[0-9]+(?:\.[0-9]*)?' -o > timestamps

我正在尝试@ckoehn 的答案并且它有效,直到它停止工作,最后一个 grep 中的星号引起了麻烦。为避免这种情况,我建议在 grep 语句中使用双引号,例如:

grep showinfo ffout | grep pts_time:[0-9.]* -o | grep "[0-9.]*" -o > timestamps

您可以简单地使用命令:

ffmpeg -i inputvideo.mp4 -filter_complex "select='gt(scene,0.3)',metadata=print:file=time.txt" -vsync vfr img%03d.png

这将只在 time.txt 文件中保存相关信息,如下所示。

frame:0    pts:108859  pts_time:1.20954
lavfi.scene_score=0.436456
frame:1    pts:285285  pts_time:3.16983
lavfi.scene_score=0.444537
frame:2    pts:487987  pts_time:5.42208
lavfi.scene_score=0.494256
frame:3    pts:904654  pts_time:10.0517
lavfi.scene_score=0.462327
frame:4    pts:2533781 pts_time:28.1531
lavfi.scene_score=0.460413
frame:5    pts:2668916 pts_time:29.6546
lavfi.scene_score=0.432326

frame是检测到的shot change从头开始的序号。此外,为您的用例选择适当的阈值(此处为 0.3)以获得正确的输出