好好学习
- 1、从一个文件中读视频
- 2,从摄像头读视频同时写视频
- 3,读文件中的视频的同时写视频
1、从一个文件中读视频
import cv2
# Create a video capture object, in this case we are reading the video from a file
vid_capture = cv2.VideoCapture('Resources/Cars.mp4') #文件名/视频名
#说明 如果你的视频文件放在代码同级,则直接写视频名就行;要是不在同一级,在下一级需要加上文件夹的名字
if (vid_capture.isOpened() == False):
print("Error opening the video file")#判断视频有没有被正确打开
# Read fps and frame count fps和帧数
else:
# Get frame rate information
# You can replace 5 with CAP_PROP_FPS as well, they are enumerations(也可以用CAP_PROP_FPS替换5,它们是枚举)
fps = vid_capture.get(5)
print('Frames per second : ', fps,'FPS')
# Get frame count
# You can replace 7 with CAP_PROP_FRAME_COUNT as well, they are enumerations
frame_count = vid_capture.get(7)
print('Frame count : ', frame_count)
while(vid_capture.isOpened()): #如果视频正在播放
# vid_capture.read() methods returns a tuple, first element is a bool
# and the second is frame
ret, frame = vid_capture.read() #返回值为一个元组,ret接收元祖的第一个值,为布尔类型;frame是视频的帧数,接收元组的其他值
if ret == True:
cv2.imshow('Frame',frame)#当第一个元素为 True 时,表示视频流包含要读取的帧。 在窗口中显示当前帧
# 20 is in milliseconds, try to increase the value, say 50 and observe0
#以毫秒为单位,尝试增加该值,例如50,然后观察
key = cv2.waitKey(20)#在视频帧之间暂停 20 毫秒。调用该waitKey()函数可让您监视键盘以获取用户输入。在这种情况下如果用户按下“ q”键,您将退出循环
if key == ord('q'):
break
else:
break
# Release the video capture object
vid_capture.release()
cv2.destroyAllWindows()
#一旦视频流被完全处理或用户过早退出循环,您释放视频捕获对象 ( vid_capture) 并关闭窗口
该isOpened()方法返回一个布尔值,指示视频流是否有效。否则,您将收到错误消息。错误消息可以暗示很多事情。其中之一是整个视频已损坏,或某些帧已损坏。假设视频文件成功打开,
我们可以使用该get()方法检索与视频流关联的重要元数据。请注意,此方法不适用于网络摄像机。该get()方法从此处记录的选项枚举列表中获取单个参数。在下面的示例中,我们提供了数值 5 和 7,它们对应于帧速率 (CAP_PROP_FPS) 和帧数 ( CAP_PROP_FRAME_COUNT)。可以提供数值或名称。
2,从摄像头读视频同时写视频
摄像头可以是本机的,也可以是通过usb连的摄像头
替换上一个程序中的
vid_capture = cv2.VideoCapture(‘story_video/sample_video.mp4’)
为
vid_capture = cv2.VideoCapture(0)
import cv2
# Create a video capture object, in this case we are reading the video from a file
# vid_capture = cv2.VideoCapture('story_video/sample_video.mp4')
vid_capture = cv2.VideoCapture(0)
if (vid_capture.isOpened() == False):
print("Error opening the video file")
# Read fps and frame count
else:
# Get frame rate information
# You can replace 5 with CAP_PROP_FPS as well, they are enumerations
fps = vid_capture.get(5)
print('Frames per second : ', fps, 'FPS')
# Get frame count
# You can replace 7 with CAP_PROP_FRAME_COUNT as well, they are enumerations
frame_count = vid_capture.get(7)
print('Frame count : ', frame_count)
# Obtain frame size information using get() method
frame_width = int(vid_capture.get(3))
frame_height = int(vid_capture.get(4))
frame_size = (frame_width, frame_height)
fps = 20
# Initialize video writer object
output = cv2.VideoWriter('output_video.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20,
frame_size)
while (vid_capture.isOpened()):
# vid_capture.read() methods returns a tuple, first element is a bool
# and the second is frame
ret, frame = vid_capture.read()
if ret == True:
cv2.imshow('Frame', frame)
# Write the frame to the output files, #您正在以每秒 20 帧的速度将 AVI 视频文件写入磁盘
output.write(frame)
# 20 is in milliseconds, try to increase the value, say 50 and observ
key = cv2.waitKey(20)
if key == ord('q'):
break
else:
print("Stream disconnected")
break
# Release the video capture object
vid_capture.release()
#释放写视频的操作
output.release()
#关闭窗口
cv2.destroyAllWindows()
解释这个函数::
VideoWriter(filename, apiPreference, fourcc, fps, frameSize[, isColor])
The VideoWriter() class takes the following arguments:
filename: pathname for the output video file
apiPreference: API backends identifier
fourcc: 4-character code of codec, used to compress the frames (fourcc)
fps: Frame rate of the created video stream
frame_size: Size of the video frames
isColor: If not zero, the encoder will expect and encode color frames. Else it will work with grayscale frames (the flag is currently supported on Windows only).
VideoWriter()类采用以下参数:
filename:输出视频文件的路径名
API偏好:API后端标识符
fourcc:压缩视频格式的选择。编解码器的4个字符代码,用于压缩帧(fourcc),fourcc代表了所使用的编码方式。
fps:创建的视频流的帧速率
帧大小:视频帧的大小
isColor:如果不是零,编码器将期望并编码颜色帧。否则它将用于灰度帧(该标志当前仅在Windows上受支持)。
VideoWriter(filename, apiPreference, fourcc, fps, frameSize[, isColor])
output = cv2.VideoWriter('output_video.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20,
frame_size)
VideoWriter_fourcc('M', 'J', 'P', 'G')
编码格式:
CV_FOURCC('P','I','M','1') = MPEG-1 codec
CV_FOURCC('M','J','P','G') = motion-jpeg codec
CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
CV_FOURCC('U', '2', '6', '3') = H263 codec
CV_FOURCC('I', '2', '6', '3') = H263I codec
CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
#cv2.VideoWriter_fourcc(‘I’, ‘4’, ‘2’, ‘0’),该参数是YUV编码类型,文件名后缀为.avi
#cv2.VideoWriter_fourcc(‘P’, ‘I’, ‘M’, ‘I’),该参数是MPEG-1编码类型,文件名后缀为.avi
#cv2.VideoWriter_fourcc(‘X’, ‘V’, ‘I’, ‘D’),该参数是MPEG-4编码类型,文件名后缀为.avi
#cv2.VideoWriter_fourcc(‘T’, ‘H’, ‘E’, ‘O’),该参数是Ogg Vorbis,文件名后缀为.ogv
#cv2.VideoWriter_fourcc(‘F’, ‘L’, ‘V’, ‘1’),该参数是Flash视频,文件名后缀为.flv
3,读文件中的视频的同时写视频
import cv2
# Create a video capture object, in this case we are reading the video from a file
vid_capture = cv2.VideoCapture('sample_video.mp4')
if (vid_capture.isOpened() == False):
print("Error opening the video file")
# Read fps and frame count
else:
# Get frame rate information
# You can replace 5 with CAP_PROP_FPS as well, they are enumerations
fps = vid_capture.get(5)
print('Frames per second : ', fps, 'FPS')
# Get frame count
# You can replace 7 with CAP_PROP_FRAME_COUNT as well, they are enumerations
frame_count = vid_capture.get(7)
print('Frame count : ', frame_count)
# Obtain frame size information using get() method
frame_width = int(vid_capture.get(3))
frame_height = int(vid_capture.get(4))
frame_size = (frame_width, frame_height)
fps = 20
# Initialize video writer object
output = cv2.VideoWriter('output_video.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20,
frame_size)
while (vid_capture.isOpened()):
# vid_capture.read() methods returns a tuple, first element is a bool
# and the second is frame
ret, frame = vid_capture.read()
if ret == True:
cv2.imshow('Frame', frame)
# Write the frame to the output files
output.write(frame)
# 20 is in milliseconds, try to increase the value, say 50 and observe
key = cv2.waitKey(20)
if key == ord('q'):
break
else:
print("Stream disconnected")
break
# Release the video capture object
vid_capture.release()
output.release()
cv2.destroyAllWindows()