Python 使用 OpenCV 在给定时间从视频中提取图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27481993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extracting image from video at a given time using OpenCV
提问by venpo045
My task is to make a utility that can take a video and time in seconds.
我的任务是制作一个可以在几秒钟内拍摄视频和时间的实用程序。
The utility should write out jpeg images from the video with the given input.
该实用程序应该使用给定的输入从视频中写出 jpeg 图像。
E.g. let the video name be abc.mpegand time be supplied to the tool as 20seconds. The utility should write out image from video @ 20th second.
例如,让视频名称abc.mpeg和时间以20秒为单位提供给工具。该实用程序应该从视频@ 20 秒写出图像。
# Import the necessary packages
import argparse
import cv2
vidcap = cv2.VideoCapture('Wildlife.mp4')
success,image = vidcap.read()
count = 0;
while success:
success,image = vidcap.read()
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
if cv2.waitKey(10) == 27: # exit if Escape is hit
break
count += 1
The above code gives all frames of the entire video, my concern is how can I pass time and get the frame at the specified time?
上面的代码给出了整个视频的所有帧,我关心的是如何打发时间并在指定时间获得帧?
采纳答案by berak
why don't you just do, what @micka proposed ?
你为什么不做,@micka 提出了什么?
import cv2
vidcap = cv2.VideoCapture('d:/video/keep/Le Sang Des Betes.mp4')
vidcap.set(cv2.CAP_PROP_POS_MSEC,20000) # just cue to 20 sec. position
success,image = vidcap.read()
if success:
cv2.imwrite("frame20sec.jpg", image) # save frame as JPEG file
cv2.imshow("20sec",image)
cv2.waitKey()
回答by venpo045
# Import the necessary packages
import cv2
vidcap = cv2.VideoCapture('Wildlife.mp4')
success,image = vidcap.read()
print success
#cv2.imwrite("frame.jpg", image)
count = 0
framerate = vidcap.get(5)
print "framerate:", framerate
framecount = vidcap.get(7)
print "framecount:", framecount
vidcap.set(5,1)
newframerate = vidcap.get(5)
print "newframerate:", newframerate
while success:
success,image = vidcap.read()
#cv2.imwrite("frame%d.jpg" % count, image)
getvalue = vidcap.get(0)
print getvalue
if getvalue == 20000:
cv2.imwrite("frame%d.jpg" % getvalue, image)
#if cv2.waitKey(10) == 27:
#break
count += 1
The output is as follows
输出如下
framerate: 29.97002997
framecount: 901.0
newframerate: 29.97002997
Why frame rate is not changing.I want to change frame rate to 1 so that whatever time value user gives i should be able to get image frame.
为什么帧速率没有改变。我想将帧速率更改为 1,以便用户提供的任何时间值都应该能够获得图像帧。
回答by John
import cv2
cap = cv2.VideoCapture('bunny.mp4')
cap.set(cv2.CAP_PROP_POS_MSEC,1000) # Go to the 1 sec. position
ret,frame = cap.read() # Retrieves the frame at the specified second
cv2.imwrite("image.jpg", frame) # Saves the frame as an image
cv2.imshow("Frame Name",frame) # Displays the frame on screen
cv2.waitKey() # Waits For Input
Here, cap.set(cv2.CAP_PROP_POS_MSEC,1000)is responsible for skipping directly to the 1st second in the video (1000th millisecond). Feel free to substitute the value of your choice.
这里,cap.set(cv2.CAP_PROP_POS_MSEC,1000)负责直接跳到视频的第 1 秒(第 1000 毫秒)。随意替换您选择的值。
I have tested the code on OpenCV 3.1.0.
我已经在 OpenCV 3.1.0 上测试了代码。

