我开始学习opencv。使用opencv打开一个视频文件非常容易。但是我不明白如何使用gsteamer打开文件。我阅读了文档。按照文档中的说明,必须这样做:
filesrc location = videofile! encodebin name =解码器解码器。 !队列!音频转换! audioresample! osssink解码器。 !视频转换!xvimagesink
或
filesrc location = something.mp4! matroskademux!解码器!视频转换! appsink
但是我不太了解如何正确设置任何文件的路径,仍然使用gstreamer。如何使用opencv和gstreamer打开任何视频文件?这个我的代码
import cv2
#filepath = input("enter the path to the video")
cap = cv2.VideoCapture("filepath",cv2.CAP_GSTREAMER)
fps = cap.get(cv2.CAP_PROP_FPS) # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
duration = frame_count/fps
print('fps = ' + str(fps))
print('duration (S) = ' + str(duration))
minutes = int(duration/60)
seconds = duration%60
print('duration (M:S) = ' + str(minutes) + ':' + str(seconds))
cap.release()
P.S:Gstreamer正在运行。我检查了命令print(cv2.getBuildInformation())。我的操作系统Windows 8.1。
对于MP4文件:
import cv2
WINDOW_NAME = 'Video'
filepath = "./my_video_file.mp4"
cap = cv2.VideoCapture(
'filesrc location={} ! decodebin ! videoconvert ! appsink'.format(filepath),
cv2.CAP_GSTREAMER
)
cv2.namedWindow(
WINDOW_NAME,
cv2.WINDOW_GUI_NORMAL | cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO
)
cv2.resizeWindow(
WINDOW_NAME,
int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
)
while True:
ret, frame = cap.read()
if not ret:
return
cv2.imshow(WINDOW_NAME, frame)
cv2.waitKey(1)
要更改视频大小/质量,可以使用此类管道:
filesrc location={} ! decodebin ! videoconvert ! videoscale ! video/x-raw,width=640,pixel-aspect-ratio=1/1 ! appsink
用cv2.resizeWindow(WINDOW_NAME, 640, 640)
。
请参见Bug 725418 "videoscale ignores add-borders property - Always maintains aspect ratio"