问题:为何视频能读出来,而写入代码也没报错,但文件大小为0k,视频好似没有写入成功!
原因:cv2.VideoWriter()
a second parameter controls Video Encoding format, which is written in most tutorials
Videowriter = Cv2. Videowriter (' Out.mp4 ', Cv2.cv.CV_FOURCC (' M ', ' J ', ' P ', ' G '), FPS, size)
Or
Videowriter = Cv2. Videowriter (' Out.avi ', Cv2.cv.CV_FOURCC (' I ', ' 4 ', ' 2 ', ' 0 '), fps, size)
The actual runtime generated video size of 0k, the reason is that the operating environment does not correspond to the video encoder, so the video can not be generated, or the second parameter setting is not appropriate, the system is not appropriate.
Solutions
cv2.VideoWriter()
The second parameter is set to -1, and the program runs interactively with a dialog box that lets you select one from the system's existing encoding.
Import cv2videocapture = Cv2. Videocapture (' Clocka.avi ') fps = Videocapture.get (cv2.cv.CV_CAP_PROP_FPS) size = (int (videocapture.get (Cv2.cv.CV_CAP _prop_frame_width)), int (Videocapture.get (Cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))) v = cv2. Videowriter (' Bb.avi ',-1, FPS, size) print fps, size, ' v-> ', vsuccess, frame = Videocapture.read () while success: Cv2.imshow (' Mywindow ', frame) Cv2.waitkey (1000/int (fps)) V.write (frame) success, frame = Videocapture.read ()
The results are as follows
File information
OpenCV Video Writing _python