Ask Your Question

hackcv's profile - activity

2020-07-17 03:09:09 -0600 received badge  Notable Question (source)
2019-10-18 21:05:31 -0600 received badge  Popular Question (source)
2016-07-19 12:49:46 -0600 received badge  Famous Question (source)
2015-08-18 16:47:07 -0600 received badge  Notable Question (source)
2015-02-03 13:26:52 -0600 received badge  Popular Question (source)
2014-07-27 17:59:20 -0600 received badge  Student (source)
2014-05-02 22:51:05 -0600 asked a question OpenCV move to begin frame position fail

I am trying to use OpenCV VideoCapture to read some frames for training. After training, I would like to return to the beginning of the video and do processing. The problem is that OpenCV VideoCapture set(CV_CAP_PROP_POS_FRAMES, 0) cannot return to the beginning of the video.

VideoCapture cap("video1.mp4");
if( !cap.isOpened()){
    cout << "Cannot open the video file" << endl;
    return -1;
}

// read some frames here

int count = cap.get(CV_CAP_PROP_FRAME_COUNT); //get total frame count
cap.set(CV_CAP_PROP_POS_FRAMES, 0); //Set index to 0 (start frame)

int index = 1;
while(1)
{
   Mat frame;
   bool success = cap.read(frame);
   if (!success){
     cout << "Cannot read  frame " << endl;
     break;
   }
   cout << "the current frame: " << index << endl;
   index++;
}

In the program, the final index value would not be same as frame count. Say a sample running would be:

index = 3774 and count = 3786
index = 3764 and count = 3776

I also try to set frame index using CV_CAP_PROP_POS_MSEC (according to a post). But it didn't work.

My current solution is to reconstruct a VideoCapture and read from begin to end. Can anyone explain why set can even not return to the beginning of the video? I think it go nothing to do with the decompression algorithm.

2014-04-26 01:43:11 -0600 commented question Ubuntu OpenCV VideoWriter using ffmpeg with “Could not open codec 'libx264'” Error

I downloaded OpenCV2.4.8 from the opencv.org website, and it redirected me to the sourcefourge.com. Would it be any problem?

2014-04-24 21:42:11 -0600 received badge  Editor (source)
2014-04-24 21:38:17 -0600 asked a question Ubuntu OpenCV VideoWriter using ffmpeg with “Could not open codec 'libx264'” Error

I am new to OpenCV, and I want write Mat images into video using VideoWriter on Ubuntu 12.04. But when constructing VideoWriter, errors came out.

It seems that OpenCV invoke ffmpeg API using default parameters and ffmpeg invoke x264 using its default parameters. Then these setting is broken for libx264. Thus the "Could not open codec 'libx264'" error.

Anyone has ideas to solve this problem?

More specifically:

  1. anyone knows where and how OpenCV invoke ffmpeg API?
  2. how to change ffmpeg default settings using code, hopefull, can be easily embeded into OpenCV?
  3. will changes of default in ffmpeg be carried to libx264?

Errors:

1. Uising CV_FOURCC('H', '2', '6', '4')
[libx264 @ 0x255de40] broken ffmpeg default settings detected
[libx264 @ 0x255de40] use an encoding preset (e.g. -vpre medium)
[libx264 @ 0x255de40] preset usage: -vpre <speed> -vpre <profile>
[libx264 @ 0x255de40] speed presets are listed in x264 --help
[libx264 @ 0x255de40] profile is optional; x264 defaults to high
Could not open codec 'libx264': Unspecified error

2. Using FOURCC = -1 to invoke user customized codec
OpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv 
backend doesn't support this codec acutally.) in CvVideoWriter_GStreamer::open, file
/home/XXX/Downloads/opencv-2.4.8/modules/highgui/src/cap_gstreamer.cpp, line 505
terminate called after throwing an instance of 'cv::Exception'
what():  /home/XXX/Downloads/opencv-2.4.8/modules/highgui/src/cap_gstreamer.cpp:505: 
error: (-210) Gstreamer Opencv backend doesn't support this codec acutally. in function
CvVideoWriter_GStreamer::open

Codes:

int main(int argc, char *argv[])
{
    VideoWriter outputVideo;
    bool fourcc_on = true; //switch on / off different error
    if (fourcc_on)
        outputVideo.open("outVideo.avi", CV_FOURCC('H', '2', '6', '4'), 25, Size(100, 100), true);
    else
        outputVideo.open("outVideo.avi", -1, 25, Size(100, 100), true);

    if (!outputVideo.isOpened())
    {
        cout  << "Could not open the output video for write" << endl;
        return -1;
    }
    return 0;
}

OpenCV Configuration:

enter image description here

FFMPEG

ffmpeg is enable to support OpenCV and libx264 is enabled when compiling ffmpeg. By using ffmpeg command line, libx264 is running normally.

ffmpeg -i test.avi -vcodec libx264 test.mp4 enter image description here