安裝Python庫 sudo pip install opencv-python 或者sudo pip install opencv-python -i https://pypi.douban.com/simple/
1 #get first frame
2 import os 3 import cv2 4 import base64 5 mp4_loc='./a.mp4'
6 first_frame_loc='./first_frame.jpg'
7 videoCapture = cv2.VideoCapture(mp4_loc) 8 success, frame = videoCapture.read() 9 if(success): 10 cv2.imwrite(first_frame_loc,frame) 11 os.remove(mp4_loc) #delete mp4 file
12 with open(first_frame_loc, 'rb') as f: 13 first_frame_pic_64=base64.b64encode(f.read())
1、videoCapture = cv2.VideoCapture(mp4_loc)
VideoCapture()中參數是mp4_loc,表示打開視頻,參數是0則表示打開筆記本的內置攝像頭,如cap = cv2.VideoCapture(0)
2、success, frame = videoCapture .read()
videoCapture .read()按幀讀取視頻,success, frame是獲cap.read()方法的兩個返回值。其中success是布爾值,如果讀取幀是正確的則返回True,如果文件讀取到結尾,它的返回值就為False。frame就是每一幀的圖像,是個三維矩陣。
參考:https://blog.csdn.net/Zhou_yongzhe/java/article/details/80310537