Ask Your Question

VideoCapture cant read url

asked 2016-06-28 09:47:50 -0500

katrina4c gravatar image


My current OpenCV version is 2.4.13. Python version is 2.7.11. My operating system is OS X Yosemite 10.10.5. I've been trying to use VideoCapture() to read url but keep getting this:

WARNING: Couldn't read movie file

VideoCapture() works fine with my laptop's webcam and local files. For example, I can open and play video like this:


But I am trying to access my ip camera with OpenCV. At first I thought I had the wrong url or it was some authentication problem. I disabled authentication, tried several camera urls(both rtsp and http) and could open them in VLC player. VLC player can also open other resources like this one: rtsp://

I even managed to open the url for capturing camera view image in my browser.

But cv2.VideoCapture cant open any of those urls, both video and image, whether its for my own camera or from the internet. Im sure the problem is not with the ip camera.

I am relatively new to programming and I installed OpenCV following this guide:

Please help!! Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-06-28 10:13:19 -0500

logidelic gravatar image

updated 2016-06-28 10:38:21 -0500

Not sure if this is possible using the OpenCV VideoCapture. I too needed to stream RTSP feeds, so I made use of libvlc for these. Here's some info:

Here is the class that I created to allow for a similar interface to what you get with VideoCapture:


#ifndef VLCCAP_H
#define VLCCAP_H

#include <opencv2/opencv.hpp>
#include <vlc/vlc.h>
#include <mutex>
#include <atomic>

using namespace std;
using namespace cv;

class VlcCap {

        void open(const char* url);
        void release();
        bool isOpened();

        bool read(Mat& outFrame);

        unsigned format(char* chroma, unsigned* width, unsigned* height, unsigned* pitches, unsigned* lines);
        void*    lock(void** p_pixels);
        void     unlock(void* id, void* const* p_pixels);

        static unsigned format_helper(void** data, char* chroma, unsigned* width, unsigned* height, unsigned* pitches, unsigned* lines);
        static void*    lock_helper(void* data, void** p_pixels);
        static void     unlock_helper(void* data, void* id, void* const* p_pixels);

        mutex           m_mutex;
        string          m_url;
        Mat             m_frame;
        bool            m_isOpen;
        atomic<bool>    m_hasFrame;

        libvlc_instance_t*      m_vlcInstance;
        libvlc_media_player_t*  m_mp;

#endif //VLCCAP_H


#include "VlcCap.h"
#include <thread>
#include <cstring>

  : m_isOpen(false)
  , m_hasFrame(false)
  , m_vlcInstance(NULL)
  , m_mp(NULL)

VlcCap::~VlcCap() {
  try {
  } catch(...) {
    // TODO log

void VlcCap::open(const char* url) {
  m_url = url;

  const char* args[] = {"-I", "dummy", "--ignore-config"};
  m_vlcInstance = libvlc_new(3, args);  

  libvlc_media_t* media = libvlc_media_new_location(m_vlcInstance, m_url.c_str());  
  m_mp = libvlc_media_player_new_from_media(media);  

  libvlc_video_set_callbacks(m_mp, lock_helper, unlock_helper, NULL, this);  
  libvlc_video_set_format_callbacks(m_mp, format_helper, NULL);

  int resp = libvlc_media_player_play(m_mp);
  if(resp == 0) {
    m_isOpen = true;
  } else {

void VlcCap::release() {
  if(m_vlcInstance) {
    m_vlcInstance = NULL;
    m_mp = NULL;
  m_hasFrame = false;
  m_isOpen   = false;

bool VlcCap::isOpened() {
      return false;

  libvlc_state_t state = libvlc_media_player_get_state(m_mp);
  return (state != libvlc_Paused && state != libvlc_Stopped && state != libvlc_Ended && state != libvlc_Error);

bool VlcCap::read(Mat& outFrame) {
  while(!m_hasFrame) {
      return false; // connection closed

    lock_guard<mutex>  guard(m_mutex);
    outFrame = m_frame.clone();
    m_hasFrame = false;
  return true;

unsigned VlcCap::format(char* chroma, unsigned* width, unsigned* height, unsigned* pitches, unsigned* lines) {
  // TODO: Allow overriding of native size?
  lock_guard<mutex>  guard(m_mutex);
  //cout << "VlcCap::format - " << chroma << " - " << *width<<"/"<<*height << endl;
  memcpy(chroma, "RV24", 4);
  pitches[0] = (*width) * 24/8;
  lines[0]   = *height;
  m_frame.create(*height, *width, CV_8UC3);

  return 1;

void* VlcCap::lock(void** p_pixels) {
  //cout << "VlcCap::lock" << endl;
  *p_pixels = (unsigned char*);
  return NULL;  

void VlcCap::unlock(void* id, void* const* p_pixels) {
  m_hasFrame = true;

unsigned VlcCap::format_helper(void** data, char* chroma, unsigned* width, unsigned* height, unsigned* pitches, unsigned* lines) {
  return ((VlcCap*)(*data))->format(chroma, width, height, pitches, lines);

void* VlcCap::lock_helper(void* data, void** p_pixels) {
  return ((VlcCap*)data)->lock(p_pixels);

void VlcCap::unlock_helper(void* data, void* id, void* const* p_pixels) {
  ((VlcCap*)data)->unlock(id, p_pixels);
edit flag offensive delete link more


Thanks for the quick response. I am an intern and the other UI developer specifically asked me to make it work using OpenCV. But I'll check out the link. Thank you!

katrina4c gravatar imagekatrina4c ( 2016-06-28 10:19:16 -0500 )edit

I wasn't able to get RTSP URLs working with VideoCapture(). Also, I figured that the VLC implementation was more robust. It has been working well for me.

logidelic gravatar imagelogidelic ( 2016-06-28 11:03:10 -0500 )edit

Question Tools

1 follower


Asked: 2016-06-28 09:47:50 -0500

Seen: 3,529 times

Last updated: Jun 28 '16