Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing an mp4 video using python opencv

I want to capture video from a webcam and save it to an mp4 file using opencv. I found example code on stackoverflow (below) that works great. The only hitch is that I'm trying to save it as mp4, not avi. Part of what I don't get is that the 'XVID' argument passed to the FOURCC writer is supposed to be, I think, an mp4 codec (from this link). If I change the filename to 'output.mp4' it tells me that the tag is invalid, so I have to believe that the XVID codec is actually making an avi file. Is this a stupid question? How do I write to an mp4?

I have found links showing how to convert an avi to an mp4 after the fact but that seems inefficient. Seems like I should be able to do it during the initial write.

import numpy as np import cv2  cap = cv2.VideoCapture(0)  # Define the codec and create VideoWriter object fourcc = cv2.cv.CV_FOURCC(*'XVID') out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))  while(cap.isOpened()):     ret, frame = cap.read()     if ret==True:         frame = cv2.flip(frame,0)          # write the flipped frame         out.write(frame)          cv2.imshow('frame',frame)         if cv2.waitKey(1) & 0xFF == ord('q'):             break     else:         break  # Release everything if job is finished cap.release() out.release() cv2.destroyAllWindows() 
like image 427
Gadzooks34 Avatar asked May 28 '15 14:05

Gadzooks34


People also ask

Does OpenCV support mp4?

In this Python OpenCV lesson we are going to learn How to Play Mp4 Videos in Python OpenCV, before this we have learned that how you can read avi videos in opencv, you can use an mp4 video or you can use camera, the same code will be implemented with camera or mp4 video.

How to write to a video using OpenCV in Python?

In this article, we will discuss how to write to a video using OpenCV in Python. Import the required libraries into the working space. Read the video on which you have to write. Create a output file using cv2.VideoWriter_fourcc () method output = cv2.VideoWriter (“path”,cv2.VideoWriter_fourcc (*’MPEG’),30, (1080,1920))

Why won't OpenCV play MP4 videos?

The problem such as OpenCV: FFMPEG: tag 0x5634504d/'MP4V' is not supported with codec id 13 and format 'mp4 / MP4 (MPEG-4 Part 14)' OpenCV: FFMPEG: fallback to use tag 0x00000020/' ???' maybe that your output video size is not the same as original video. You can look over the frame size of video first.

What is VideoWRITER FourCC in Python?

VideoWriter_fourcc ('M', 'J', 'P', 'G') in Python. VideoWriter::fourcc ('M', 'J', 'P', 'G') in C++. The video codec specifies how the video stream is compressed. It converts uncompressed video to a compressed format or vice versa.

How to write MP4 files with OpenCV or FFmpeg?

Anyone who's looking for most convenient and robust way of writing MP4 files with OpenCV or FFmpeg, can see my state-of-the-art VidGear Video-Processing Python library's WriteGear API that works with both OpenCV backend and FFmpeg backend and even supports GPU encoders. Here's an example to encode with H264 encoder in WriteGear with FFmpeg backend:


2 Answers

This worked for me.

self._name = name + '.mp4' self._cap = VideoCapture(0) self._fourcc = VideoWriter_fourcc(*'MP4V') self._out = VideoWriter(self._name, self._fourcc, 20.0, (640,480)) 
like image 131
10SecTom Avatar answered Sep 25 '22 21:09

10SecTom


What worked for me was to make sure the input 'frame' size is equal to output video's size (in this case, (680, 480) ).

http://answers.opencv.org/question/27902/how-to-record-video-using-opencv-and-python/

Here is my working code (Mac OSX Sierra 10.12.6):

cap = cv2.VideoCapture(0) cap.set(3,640) cap.set(4,480)  fourcc = cv2.VideoWriter_fourcc(*'MP4V') out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640,480))  while(True):     ret, frame = cap.read()     out.write(frame)     cv2.imshow('frame', frame)     c = cv2.waitKey(1)     if c & 0xFF == ord('q'):         break  cap.release() out.release() cv2.destroyAllWindows() 

Note: I installed openh264 as suggested by @10SecTom but I'm not sure if that was relevant to the problem.

Just in case:

brew install openh264 
like image 35
ling Avatar answered Sep 21 '22 21:09

ling