opencv and python 3 -- cannot write video frames to disk

0

Python 3.5.2, anaconda 4.2.0 on Windows 10. OpenCV installed from conda, version 3.1.0.

I'm trying to process a video file by opening it, transforming each frame, and putting the result into a new video file. The output file is created, but the size is about 800 bytes and its empty. The input file has ~4,000 frames and it's about 150 MB.

Here's my code, which follows the guide on the OpenCV documentation pretty closely.

import cv2
import progressbar
# preprocess video
# args.input is a valid file name     
    outname = 'foo.mp4'
    cap = cv2.VideoCapture(args.input)    

    codec = int(cap.get(cv2.CAP_PROP_FOURCC)) 
    framerate = app_config.camera.framerate #240
    size = (app_config.camera.width, app_config.camera.height) #1080 x 720

    vw = cv2.VideoWriter(filename=outname, fourcc=codec, fps=framerate, frameSize=size, isColor=False)
    curframe = 0
    with progressbar.ProgressBar(min_value=0, max_value=int(cap.get(cv2.CAP_PROP_FRAME_COUNT))) as pb:
        while cap.isOpened():
            ret, frame = cap.read()
            if ret:
                #update the progress bar
                curframe += 1
                pb.update(curframe)
                # convert to greyscale
                grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                # invert colors
                inverted = cv2.bitwise_not(grey)
                vw.write(inverted)   

                #cv2.imshow('right', right)
                #if cv2.waitKey(1) & 0xFF == ord('q'):
                #    break
            else:
                break

        cap.release()
        vw.release()
        cv2.destroyAllWindows()

I receive the following error:

OpenCV: FFMPEG: tag 0x7634706d/'mp4v' is not supported with codec id 13 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000020/' ???'

I receive similar errors (as well as a warning that I have an incorrect environment variable for h.264 library path) if i try to set codec = cv2.VideoWriter_fourcc(*'H264').

opencv
video
python-3.5
asked on Stack Overflow Feb 17, 2017 by gvoysey

1 Answer

0

Ensure that the dimensions of inverted match the dimensions of the size parameter in the videoWriter definition.

Also use 'M','P','4','V' codec with the .mp4 container.

answered on Stack Overflow Mar 13, 2017 by Shawn Mathew

User contributions licensed under CC BY-SA 3.0