user1245262
user1245262

Reputation: 7505

Cannot Demultiplex Stream from Video created by Py OpenCV

I'm trying to use OpenCV to create a video out of individual frames. Although the code runs without error, I cannot play the resulting avi file. When I do, the error I get is Could not demultiplex stream.

The stripped down version of the code I'm using to do this is:

import caffe
import cv2
import fnmatch
import os

# Get Image List
image_list = fnmatch.filter('./sample/images','*.png')
image_list.sort()

# Create VideoWriter
# codec = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
# codec = -1
codec = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
video = cv2.VideoWriter( './movieT.avi',
                         codec,
                         20,
                         (289, 289))

for img_name in image_list:
    # Load image
    im_path = os.path.join('./sample/images',img_name)
    img = caffe.io.load_image(im_path)

    # Resize image to expected size
    img = caffe.io.resize(img,(289, 289, 3))

    # Write image
    video.write(img)
video.release()

I Googled around a bit, and the main errors I see mentioned are ensuring the image sizes match the size expected by the cv2.VideoWiter and that I have the right codecs. I resize my images to ensure they are the right size and I tried a few different codecs. I also tried to install standard codecs using some apt-get install commands I also found by Googling around:

me@balin:~$ sudo apt update
me@balin:~$ sudo apt install libdvdnav4 libdvdread4 gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly libdvd-pkg
me@balin:~$ sudo apt install ubuntu-restricted-extras

I am currently using:

Ubuntu 18.04
Python 2.7.17
cv2.__version__ 4.0.1

Upvotes: 0

Views: 2444

Answers (1)

furas
furas

Reputation: 142889

I think your problem in incorrect use of fnmatch.filter() which doesn't get filenames from folder but it is used only to check if filename match pattern.

See example from fnmatch

import fnmatch
import os

for file in os.listdir('.'):
    if fnmatch.fnmatch(file, '*.txt'):
        print(file)

With your code I alwyas get empty list so later it can't add any image to video and I can't display this video.

But you can use glob.glob() for this

image_list = glob.glob('./sample/images/*.png')

This code works for me

import cv2
import glob

image_list = glob.glob('./sample/images/*.png')
image_list.sort()
#print(image_list)

codec = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
video = cv2.VideoWriter('./movieT.avi', codec, 20, (289, 289))

for img_name in image_list:
    img = cv2.imread(img_name)
    img = cv2.resize(img, (289, 289))
    video.write(img)

video.release()

Upvotes: 0

Related Questions