python cv2获取视频第一帧,并转码

安装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())

1videoCapture = cv2.VideoCapture(mp4_loc)

VideoCapture()参数是mp4_loc,表示打开视频,参数是0则表示打开笔记本的内置摄像头,如cap = cv2.VideoCapture(0)

2success, frame = videoCapture .read()

 videoCapture .read()按帧读取视频,success, frame是获cap.read()方法的两个返回值。其中success是布尔值,如果读取帧是正确的则返回True,如果文件读取到结尾,它的返回值就为Falseframe就是每一帧的图像,是个三维矩阵。

参考:https://blog.csdn.net/Zhou_yongzhe/java/article/details/80310537

 

posted @ 2020-07-23 14:37  声声慢43  阅读(3825)  评论(0编辑  收藏  举报