[英]Python CV2 extract video frames with subtitles
I have a video file result.mp4
with subtitles.我有一个带字幕的视频文件result.mp4
。 I want to extract frames, but I want subtitles to be on those frames too, subtitles are in file as a separate track.我想提取帧,但我希望字幕也出现在这些帧上,字幕作为单独的轨道在文件中。
Currently I use this code to extract:目前我使用这段代码来提取:
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
which works fine, but as I said, does not include subtitles on the frames.效果很好,但正如我所说,框架上不包含字幕。 I am able to extract frames with subtitles only if I hardcode subtitles into video which looks different and takes a lot of time.只有当我将字幕硬编码到看起来不同且需要大量时间的视频中时,我才能提取带有字幕的帧。
Is there a way for opencv to extract frames with subs that are on separate track? opencv 有没有办法提取带有单独轨道上的子的帧?
FYI: I use this FFMPEG command to merge .mp4
with .srt
:仅供参考:我使用此 FFMPEG 命令将.mp4
与.srt
合并:
ffmpeg -i ourvid.mp4 -i ourvid.srt -c:s mov_text -c:v copy -c:a copy result.mp4 -f srt
which completes in around a second大约在一秒钟内完成
if I hardcode it however:但是,如果我对其进行硬编码:
ffmpeg -i ourvid.mp4 -vf "subtitles=ourvid.srt" result.mp4
it takes on average at least half of the length of the source video (in completion time)平均至少需要源视频长度的一半(完成时间)
Essentially, I think cv2 is not able to do so, but ffmpeg can do that, with a command like this:本质上,我认为 cv2 不能这样做,但 ffmpeg 可以这样做,使用如下命令:
ffmpeg -i ourvid.mp4 -vf select='between(n\,x\,y)',subtitles=ourvid.srt -q:v 2 frames%d.jpg
where x
and y
are start and end of range to extract frames so I just use, for example其中x
和y
是提取帧的范围的开始和结束,所以我只使用,例如
os.system("ffmpeg -i ourvid.mp4 -vf select='between(n\,3901\,3901)',subtitles=ourvid.srt -q:v 2 frames%d.jpg")
to run it from my python script从我的 python 脚本运行它
not the nice way, but this is the only one I found so far不是好方法,但这是迄今为止我发现的唯一一个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.