VideoCapture failing for ~25% of videos from IP Camera

asked 2019-07-20 15:02:34 -0500

dwalker gravatar image

updated 2019-07-21 02:56:21 -0500

I've had a Python (3.7) app using OpenCV (3.4) for a year or so, working fine. I recently reinstalled Debian Buster, and with it upgraded to OpenCV 4.1. Somewhere in that process I've found that videos which previously worked ok now can't be read by OpenCV.

A sample video with the problem is here:

Basically, this plays fine in most media apps, but my install of OpenCV won't open it - isOpened() returns false. However, the odd thing is that it's not a constant problem - most of the videos from the same IP camera work fine, but around a quarter can't be read. I assume there's some very minor oddity with the files, but I can't see what it is, and it's hard to get the manufacturer to do much about it because they play fine in most media apps.

When I run cv2.getBuildInformation() I get the following:

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES
      avcodec:                   YES (58.35.100)
      avformat:                  YES (58.20.100)
      avutil:                    YES (56.22.100)
      swscale:                   YES (5.3.100)
      avresample:                NO
    GStreamer:                   YES (1.14.4)
    v4l/v4l2:                    YES (linux/videodev2.h)

After turning on debugging mode (with export OPENCV_LOG_LEVEL=debug and export OPENCV_VIDEOIO_DEBUG=1) I get the following when I try to do vc = cv2.VideoCapture('/home/Dave/ErrorReadingHeader.mp4'):

[ INFO:0] VIDEOIO: Enabled backends(5, sorted by priority): FFMPEG(1000); GSTREAMER(990); V4L2(980); CV_IMAGES(970); CV_MJPEG(960)
[ WARN:0] VIDEOIO(FFMPEG): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ...
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x2338590] error reading header
[ WARN:0] VIDEOIO(FFMPEG): can't create capture
[ WARN:0] VIDEOIO(GSTREAMER): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ...
[ WARN:0] VIDEOIO(GSTREAMER): can't create capture
[ WARN:0] VIDEOIO(V4L2): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ...
VIDIOC_REQBUFS: Inappropriate ioctl for device
[ WARN:0] VIDEOIO(V4L2): can't create capture
[ WARN:0] VIDEOIO(CV_IMAGES): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ...
[ WARN:0] VIDEOIO(CV_IMAGES): created, isOpened=0
[ WARN:0] VIDEOIO(CV_MJPEG): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ...
[ WARN:0] VIDEOIO(CV_MJPEG): can't create capture

Any thoughts on how to resolve this issue? Keen to understand if this particular video can be opened in other people's installations of OpenCV 4.1, and if so what your VideoIO configuration is.

Edit: for info, the following are the steps I used to install OpenCV 4.1 on Raspberry Pi 4 / Debian Buster:

Temporarily increase swap file size and gpu memory:
    sudo sed -i 's/CONF_SWAPSIZE=100/CONF_SWAPSIZE=2048/g' /etc/dphys-swapfile
    sudo /etc/init.d/dphys-swapfile stop
    sudo /etc/init.d/dphys-swapfile start
    sudo sed -i '$a gpu_mem=128' /boot/config.txt
Install dependencies:
    sudo apt-get -y install build-essential cmake pkg-config git
    sudo apt-get -y install libjpeg-dev libtiff-dev libjasper-dev libpng12-dev
    sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev
    sudo apt-get -y install libgtk2.0-dev libgtk-3-dev libatlas-base-dev gfortran
    sudo apt-get install libcanberra-gtk ...
edit retag flag offensive close merge delete


@dwalker. You cannot upgrading from Stretch to Buster. If you do'll get reboot. And cannot be resolved. So, kindly, download new image and burn to 128gb microsdxc or more. It worked for me both pi 3 and pi 4b.

supra56 gravatar imagesupra56 ( 2019-07-20 21:13:08 -0500 )edit

Hi thanks for taking a look and for the reply. And apologies but I wasn’t clear in my description - although I said upgrade, I did actually do a fresh install from a fresh Buster image, and then installed OpenCV. If yours can play the linked video, I’d be keen to understand what you did differently. I’ll add my OpenCV installation steps to my original post above...

dwalker gravatar imagedwalker ( 2019-07-21 02:52:13 -0500 )edit

Did you swapfile first, then after install library?

supra56 gravatar imagesupra56 ( 2019-07-21 04:29:04 -0500 )edit

Hi, yes the first time I ran it I did everything in that order, so swapfiles first. However, late last night I ended up re-installing OpenCV (i.e. ran through all of those steps again) with the addition of gstreamer, and that time I skipped the swapfile settings. I'm on a new Raspberry Pi 4 with 4GB RAM so not sure the swap is really necessary anymore, and the make and install completed successfully.

Since this post I've discovered that the problem likely lies with a change in FFMPEG 4.x, which (paraphrasing my probably poor understanding) is now less tolerant of the format than previously. Ideally I'd like to downgrade to FFMPEG 3.x, but after reading around it seems fraught with problems and so I've got a workaround that I'll stick with for the moment...

dwalker gravatar imagedwalker ( 2019-07-21 11:33:58 -0500 )edit

I'm downloading right now. I'm about 43% in 1 hrs for rpi3. I have rpi4 but I have to buy hdmi monitor. I don't used swapfile. I let you know.

supra56 gravatar imagesupra56 ( 2019-07-21 12:19:02 -0500 )edit

No! You can't downgrade FFMPEG 3.x. There is newer version for FFMPEG4 and GSTREAMER too.

supra56 gravatar imagesupra56 ( 2019-07-21 12:24:26 -0500 )edit

Mine is working. I want to test your code.

supra56 gravatar imagesupra56 ( 2019-07-22 21:45:44 -0500 )edit