Python CV2提取带字幕的视频帧
Python CV2 extract video frames with subtitles
我有一个带字幕的视频文件result.mp4
。我想提取帧,但我也希望这些帧上有字幕,字幕在文件中作为单独的轨道。
目前我使用这段代码提取:
vidcap = cv2.VideoCapture("result.mp4")
success,image = vidcap.read()
count = 0
while success:
cv2.imwrite("frame%d.jpg" % count, image)
success,image = vidcap.read()
print('Read a new frame: ', success)
count += 1
效果很好,但正如我所说,帧上不包含字幕。仅当我将字幕硬编码到看起来不同且花费大量时间的视频中时,我才能提取带字幕的帧。
有没有办法让 opencv 提取带有单独轨道上的子帧的帧?
仅供参考:
我使用此 FFMPEG 命令将 .mp4
与 .srt
:
合并
ffmpeg -i ourvid.mp4 -i ourvid.srt -c:s mov_text -c:v copy -c:a copy result.mp4 -f srt
大约在一秒内完成
如果我对它进行硬编码:
ffmpeg -i ourvid.mp4 -vf "subtitles=ourvid.srt" result.mp4
平均至少需要源视频长度的一半(完成时间)
基本上,我认为 cv2 无法做到这一点,但是 ffmpeg 可以做到这一点,使用这样的命令:
ffmpeg -i ourvid.mp4 -vf select='between(n\,x\,y)',subtitles=ourvid.srt -q:v 2 frames%d.jpg
其中 x
和 y
是提取帧的范围的开始和结束
所以我只是使用,例如
os.system("ffmpeg -i ourvid.mp4 -vf select='between(n\,3901\,3901)',subtitles=ourvid.srt -q:v 2 frames%d.jpg")
从我的 python 脚本 运行 到
不是很好的方法,但这是我迄今为止找到的唯一方法
我有一个带字幕的视频文件result.mp4
。我想提取帧,但我也希望这些帧上有字幕,字幕在文件中作为单独的轨道。
目前我使用这段代码提取:
vidcap = cv2.VideoCapture("result.mp4")
success,image = vidcap.read()
count = 0
while success:
cv2.imwrite("frame%d.jpg" % count, image)
success,image = vidcap.read()
print('Read a new frame: ', success)
count += 1
效果很好,但正如我所说,帧上不包含字幕。仅当我将字幕硬编码到看起来不同且花费大量时间的视频中时,我才能提取带字幕的帧。
有没有办法让 opencv 提取带有单独轨道上的子帧的帧?
仅供参考:
我使用此 FFMPEG 命令将 .mp4
与 .srt
:
ffmpeg -i ourvid.mp4 -i ourvid.srt -c:s mov_text -c:v copy -c:a copy result.mp4 -f srt
大约在一秒内完成
如果我对它进行硬编码:
ffmpeg -i ourvid.mp4 -vf "subtitles=ourvid.srt" result.mp4
平均至少需要源视频长度的一半(完成时间)
基本上,我认为 cv2 无法做到这一点,但是 ffmpeg 可以做到这一点,使用这样的命令:
ffmpeg -i ourvid.mp4 -vf select='between(n\,x\,y)',subtitles=ourvid.srt -q:v 2 frames%d.jpg
其中 x
和 y
是提取帧的范围的开始和结束
所以我只是使用,例如
os.system("ffmpeg -i ourvid.mp4 -vf select='between(n\,3901\,3901)',subtitles=ourvid.srt -q:v 2 frames%d.jpg")
从我的 python 脚本 运行 到
不是很好的方法,但这是我迄今为止找到的唯一方法