OpenCV VideoWriter: Issues with playing video


I'm having issues correctly rendering/playing a video created by OpenCV's videowriter.


Python does not spit out any errors and sucessfully runs through/creates the video. When I try to play the video with VLC, it looks like VLC is trying to play an empty video (it looks like it has time length 00:00). Furthermore, the video size is a mere 5kbs. I've used this function with other NumPy image arrays, and they have worked.

My NumPy Array Preparation Code:
for i in range(len(frames)-1):
    fig = PIV.runPIV(frames[i].astype(np.int32),frames[i+1].astype(np.int32),dt = self.context.attrs.dt)
    agg = fig.canvas.switch_backends(FigureCanvasAgg)
    agg.draw()  #This produces a successful image. Simple Quiver plot.

    image = np.fromstring(agg.tostring_rgb(),dtype = np.uint8)
    image.shape = agg.get_width_height() + (3,)
Images Array:
[[[191 191 191]
  [191 191 191]
  [191 191 191]

  [191 191 191]
  [191 191 191]
  [191 191 191]]]


This array has a shape of (640,480,3) which was what other arrays previously that worked similarly had. The only difference between this and other past successes is that before I loaded images and did processing on them before rendering, but this stack is from matplotlib.pyplot with a switched backend to FigureCanvasAgg; however, when I save the image prior to its NumPy conversion, the image looks perfectly fine.

My OpenCV VideoWriter Code:
def writeMovie(self, fileName, fps=20):
    isColor = len(self[0].shape) == 3
    writer = cv2.VideoWriter(fileName, 
    for image in self:
        if image.dtype == np.bool:
            image = image.astype(np.uint8)*255

I will gladly provide more information to the best of my abilities to clarify and help answer this question. Any advice/suggestions will be greatly appreciated.


When the video seems to be empty, that's because the images you want to put into the video don't fit the video parameters. I had an equivalent problem in C++ and my problem was the input size and the codec.

I'm a neophyte in python, but I think you might have the same problem. Maybe you should try to print frameSize and fourcc to see if theses parameters are correct ( shouldn't it be self[0].shape[0:1] ? (Again, I don't know much about python, so I might be wrong on this point))

Not sure what the problem is still, but the way I got around it was saving each image as a file and then concatenating all the files with ffmpeg.

Need Your Help

How to create a button in javascript that will open iframe when user clicks on that button?

php javascript html iframe

I'm working on comments for posts on my website. How to create a button in javascript that will open iframe window with comments when user clicks on that button? Something like Facebook have with t...

JSON listview OnItemClickListener url (webview)

android json webview onitemclicklistener

I got all the data ... But I am not able to use webview url(UrlWeb) , i mean when i click on list item then it will open in web view. when i click any list it should pass corresponding url to the W...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.