【视频处理】

1. ffmpeg 可以完成视频抽帧。使用 ffmpeg 转换视频格式命令:

1 ffmpeg -i 原视频路径  输出视频路径

1. 调用本地摄像头

 1 ### 读取摄像头中图像
 2 import cv2
 3 
 4 videoCapture = cv2.VideoCapture(0)
 5 sucess, frame = videoCapture.read()
 6 
 7 while sucess:
 8 
 9     cv2.imshow('img', frame)
10     k = cv2.waitKey(20)   # 等待时间,单位毫秒 m s。 参数为 0 等待键盘输入后继续
11     # 按 q 键退出
12     if (k & 0xff == ord('q')):
13         break
14     ### 读取下一帧
15     sucess, frame = videoCapture.read()
16 videoCapture.release()
17 cv2.destroyAllWindows()

2. 读取指定位置的视频文件

 1 import cv2
 2 
 3 video_path = r'视频的绝对路径'     # r 表示路径中不进行转义
 4 videoCapture = cv2.VideoCapture(video_path)
 5 success, frame = videoCapture.read()
 6 
 7 fps = videoCapture.get(cv2.CAP_PROP_FPS)
 8 size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
 9 
10 print(fps, size, success, frame)
11 
12 while sucess:
13 
14     cv2.imshow('img', frame)
15     cv2.waitKey(500)   # 等待时间,单位毫秒 m s。 参数为 0 等待键盘输入后继续
16 
17     ### 读取下一帧
18     sucess, frame = videoCapture.read()

3. 将视频由 mp4 格式转为 avi 格式

 1 ### 实现将原视频的 mp4 格式转为 新的 avi 格式
 2 import cv2
 3 
 4 videoCapture = cv2.VideoCapture('原视频路径.mp4')
 5 
 6 fps = videoCapture.get(cv2.CAP_PROP_FPS)
 7 size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
 8         int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
 9 
10 
11 videoWriter = cv2.VideoWriter('新视频路径.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, size)
12 
13 success, frame = videoCapture.read()
14 
15 while success:
16     videoWriter.write(frame)
17     success, frame = videoCapture.read()
18 print ("process end!")
原文地址:https://www.cnblogs.com/lyj0123/p/15770234.html