Simple frame by frame video decoder library

后端 未结 3 1750
醉酒成梦
醉酒成梦 2021-01-20 03:06

I\'m looking for a simple c/c++ lib that would allow to extract the first frame of a video as a uchar array. And have a simple fonction to access the next one.

I kno

3条回答
  •  爱一瞬间的悲伤
    2021-01-20 03:40

    Here's an example with OpenCV:

    #include 
    #include 
    
    int
    main(int argc, char **argv)
    {       
        cv::VideoCapture capture(argv[1]);
        if (capture.grab())
        {   
            cv::Mat_ frame;
            capture.retrieve(frame);
            //
            // Convert to your byte array here
            //
        }    
        return 0;
    }
    

    It's untested, but I cannibalized it from some existing working code, so it shouldn't take you long to get it working.

    The cv::Mat_ is essentially a byte array. If you really need something that's explicitly of type unsigned char *, then you can malloc space of the appropriate size and iterate over the matrix using

    You can convert a cv::Mat to a byte array using pixel positions(cv::Mat_::at()) or iterators (cv::Mat_::begin() and friends).

    There are many reasons why libraries rarely expose image data as a simple pointer, such as:

    • It implies the entire image must occupy a contiguous space in memory. This is a big deal when dealing with large images
    • It requires committing a certain ordering of the data (pixel vs non-planar -- are the RGB planes stored interspersed or separately?) and reduces flexibility
    • Dereferencing pointers is a cause for bugs (buffer overruns, etc).

    So if you want your pointer, you have to do a bit of work for it.

提交回复
热议问题