使用来自QueryFrame如何工作?的代码示例我注意到,如果程序运行到视频末尾,则需要花费大量时间来退出。我想在最后一帧快速退出,并且我验证了如果我不尝试播放到视频末尾,速度会快很多,但有一些细节对我来说没有意义。这是我的代码:
import cv2
# create a window
winname = "myWindow"
win = cv2.namedWindow(winname, cv2.CV_WINDOW_AUTOSIZE)
# load video file
invideo = cv2.VideoCapture("video.mts")
frames = invideo.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
print "frame count:", frames
# interval between frame in ms.
fps = invideo.get(cv2.cv.CV_CAP_PROP_FPS)
interval = int(1000.0 / fps)
# play video
while invideo.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) < frames:
print "Showing frame number:", invideo.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
(ret, im) = invideo.read()
if not ret:
break
cv2.imshow(winname, im)
if cv2.waitKey(interval) == 27: # ASCII 27 is the ESC key
break
del invideo
cv2.destroyWindow(winname)
唯一的问题是,返回的帧数是 744,而最后播放的帧数是 371(从 0 开始计数,所以是 372 帧)。我认为这是因为视频是隔行扫描的,我想我需要考虑到这一点,并将
interval
除以 2,将 frames
除以 2。但问题是,我如何知道我需要这样做?似乎没有属性可以检查这个:
我在另一个问题之间写了一些东西,我想我也应该在这里发布它,如果你在Linux上运行,这可能有用,到目前为止它对我有用:
import subprocess
import re
inputVideo = "theFileInQuestion.mp4" # sample of interlaced video
# construct the ffmpeg command as a list of arguments
ffmpegCommand = [
"ffmpeg", # command for ffmpeg, you need to install this with sudo apt instlal ffmpeg
"-filter:v", "idet", # filter this specific information
"-frames:v", "100", # test on first 100 frames
"-an", # don't test audio
"-f", "rawvideo", "-y", "/dev/null", # assign results to nothing
"-i", inputVideo # on this video
]
try:
outputBytes = subprocess.check_output(ffmpegCommand, stderr=subprocess.STDOUT) # get the output
outputStr = outputBytes.decode('utf-8') # decode the output
tffLines = re.findall(r'TFF:\s+(\d+)', outputStr) # search for a pattern that is the count of top field first
bffLines = re.findall(r'BFF:\s+(\d+)', outputStr) # search for a pattern that is the count of bottom field first
totalInterlacedFrames = sum(map(int, tffLines)) + sum(map(int, bffLines)) # get total interlaced frames in the 100 checked
if totalInterlacedFrames != 0: # check if interlaced
print("This video is interlaced!") # YES
else: # if not
print("This video is not interlaced!") # NO
except subprocess.CalledProcessError as e:
print(f"Error during interlacing analysis: {e}")
我主要使用了here描述的linux功能。我也在努力寻找任何 opencv 道具来自动识别隔行扫描。