參考地址:
https://geek-docs.com/python/python-ask-answer/683_python_get_video_dimension_in_pythonopencv.html
https://blog.csdn.net/m0_61787307/article/details/129654826
1. 安裝(試了三臺伺服器,有一臺伺服器安裝過程會很慢,卡在一個地方一直不動,目前不清楚是什麼原因,但是多等會確實安裝成功了)
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
2. 使用
import cv2 # 開啟影片檔案 video = cv2.VideoCapture('影片地址') # 檢查影片是否成功開啟 if not video.isOpened(): print('無法開啟影片檔案') # 獲取影片尺寸 width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 輸出影片尺寸 print(f'影片尺寸:{width}x{height}') # 釋放影片 video.release()
3. 遇到問題
3.1 [ERROR:0@0.014] global cap.cpp:166 open VIDEOIO(CV_IMAGES): raised OpenCV exception: OpenCV(4.9.0) /tmp/pip-install-9bc0cux4/opencv-python_fa0a3297e6c14b959cf0737136675a99/opencv/modules/videoio/src/cap_images.cpp:274: error: (-215:Assertion failed) number < max_number in function 'icvExtractPattern'
原因:python版本和opencv-python安裝包版本不符
解決:點這裡 檢視你使用python的對應包版本,重新pip下載即可