3

I have just read this in the book OpenCV 2 Computer Vision Application Programming Cookbook by R. Laganiere :

It is important to note that in order to open the specified video file, your computer must have the corresponding codec installed, otherwise cv::VideoCapture will not be able to understand the input file. Normally, if you are able to open your video file with a video player on your machine (such as the Windows Media Player), then OpenCV should also be able to read this file.

Unfortunately, things aren't that easy for me. Yes, I can read avi files on my video player, however it does not work with my OpenCV-Qt application. VideoCapture isOpen() method returns false, despite the fact that the path is correct, and every codec needed seem to be here. I tried several files, so it is not related to one particular format.

Does someone here has experience in opening avi files in Ubuntu using OpenCV ? I think this is a big issue, can't find any trully relevant solution on the internet...

Thanks !!

[EDIT] Here is the function I am working on; some of the variables here are class members, so it may looks incomplete. However, it is this very piece of code that is not working. In particular, the line where I instanciate a new VideoCapture object.

void MainWindow::on_actionOuvrir_fichier_triggered()
{
    //mettre a -1 streamId
    streamId = -1;
    //get path to the avi file
    QString fileName = QFileDialog::getOpenFileName(this,tr("Ouvrir fichier video"),"/home", tr("Videos (*.avi)"));
    std::string utf8_text = fileName.toUtf8().constData();
    //open .avi
    capture = new VideoCapture(utf8_text);
    //check 
    if(!capture->isOpened())
        cout << "probleme ouverture fichier video" << endl;
    //delay between each frame in ms
    rate = capture->get(CV_CAP_PROP_FPS);
    delay = 1000 / rate;
    //start Qtimer recordId
    recordId = startTimer(delay);
    //capture first frame
    if(!capture->read(in))
        cout << "probleme lecture frame fichier video" << endl;
}

[EDIT 2] test on Windows 7

void MainWindow::on_actionOuvrir_fichier_triggered()
{
    //mettre a -1 streamId
    streamId = -1;
    //ouvrir fenetre navigation fichiers pour recuperer path vers .avi
    QString fileName = QFileDialog::getOpenFileName(this,tr("Ouvrir fichier video"),"/home",
                                                    tr("Videos (*.avi)"));
    //std::string utf8_text = fileName.toUtf8().constData();
    std::string current_locale_text = fileName.toLocal8Bit().constData();
    if(QDir().exists(current_locale_text.c_str())) std::cout << "Path is good!" << endl;
    //ouvrir .avi
    capture = new VideoCapture(current_locale_text);
    //check ouverture
    if(!capture->isOpened())
        cout << "probleme ouverture fichier video" << endl;
    //calculer delay between each frame in ms
//    rate = capture->get(CV_CAP_PROP_FPS);
//    delay = 1000 / rate;
    //demarrer timer recordId
    recordId = startTimer(100);
    //capture premiere frame
    if(!capture->read(in))
        cout << "probleme lecture frame fichier video" << endl;
}

With that code I was able to open SOME avi files, but not all (actually far from that). So I guess I definitely have a codec issue... does anyone can tell me how to fix this under Ubuntu ? Don't let me go on bounty for that one ! Thank you very much.

[EDIT 3] As suggested by Etienne, I followed instructions here and tried to convert my video to the I420 format supposedly supported by OpenCV on all platforms, using mencoder with the command line given. So I went from 24 bits RGB (RV24) codec to Planar 4:2:0 YUV (I420), according to VLC. Same behavior though, I'm still unable to instanciate the VideoCapture object.

THERE IS A LOT OF UNSOLVED CASES similar to mine on Stack Overflow...

Community
  • 1
  • 1
CTZStef
  • 1,675
  • 2
  • 18
  • 47
  • Can you help on this one Astor ? thx – CTZStef Jun 25 '12 at 18:48
  • possible duplicate of [Open .avi with OpenCV 2.4 & Ubuntu 11.04](http://stackoverflow.com/questions/11181689/open-avi-with-opencv-2-4-ubuntu-11-04) – karlphillip Jun 25 '12 at 19:39
  • You already asked this question. Be absolutely sure the path you are sending to OpenCV is valid: `if (QDir().exists(utf8_text.c_str())) std::cout << "Path is good!\n";` – karlphillip Jun 25 '12 at 19:40
  • it is a duplicate indeed; I wanted to rephrase it, it was messy and unclear. Sorry about that. Concerning the issue, I tried to load directly a file which is in the same folder as my application by doing capture = new VideoCapture("1.avi"); did not work either so this is not a path related problem, unless I actually have two problems :) Sorry again for double posting, it was not intended that way. – CTZStef Jun 25 '12 at 19:45
  • I tried (QDir().exists(utf8_text.c_str())) std::cout << "Path is good!\n"; and path is good! – CTZStef Jun 25 '12 at 19:58
  • 1
    Did you take a look at OpenCV videos codecs compatibility? : http://opencv.willowgarage.com/wiki/VideoCodecs – Etienne Jun 26 '12 at 13:19
  • I just did ;) I tried to convert my video to the I420 supposedly supported by OpenCV on all platform, using mencoder with the command line given. So I went from 24 bits RGB (RV24) codec to Planar 4:2:0 YUV (I420), but still, eh, does not work. – CTZStef Jun 27 '12 at 16:57
  • I have to emphasize the fact that I do not think the problem is related to Qt, since I was able to open some files on Windows 7, with the same code, and after having compiled OpenCV the same way on both system (i.e. without taking care of Qt support for highGUI). – CTZStef Jun 27 '12 at 21:51

1 Answers1

6

I finally managed to solve my problem.

What I did :

  • try to open some video to finally find one that would work. Fortunately it only took me 3 videos to find one that work.

  • check the codec with VLC. It is MPEG-4 XVID.

  • use mencoder to convert the file I wanted to read to XVID:

    sudo mencoder myFile.avi -ovc lavc vcodec=mpeg4:mbd=2:cbp:trell:vbitrate=300 -ffourcc XVID -o test.avi

I also switched to old C function cvCaptureFromFile(). I will go back to C++ interface to make sure it was indeed a codec issue, but I am pretty sure it was ([EDIT] it was a codec issue).

What did not work :

  • recompile OpenCV with Qt support enabled
Community
  • 1
  • 1
CTZStef
  • 1,675
  • 2
  • 18
  • 47
  • 1
    Note VLC has it's own buitlin codecs it doesn't use system ones. So just because it opens in vlc isn't a good test of the system – Martin Beckett Jun 28 '12 at 02:40
  • But VLC tells you which codec it uses to read your file, so yes, this is useful to know how you have to encode your file. In my case, I was able to open the file with my app but I didn't know how. VLC gave me the information; so now I will encode every other video this way. – CTZStef Jun 28 '12 at 02:42