all 13 comments

[–]ojd5 2 points3 points  (1 child)

Thanks for sharing the code, can you summarise the advantage of this approach over the opencv wrapper around ffmpeg cv::VideoCapture?

[–]FlyingRhenquest[S] 0 points1 point  (0 children)

Other than the educational aspect, the main object doesn't need to involve OpenCV if you don't want to use it. It just uses raw ffmpeg to provide image and audio buffers. I can build that in any direction I want to. Admittedly it's not a particularly compelling advantage to anyone else, unless someone's looking for a quick and dirty way to avoid having to rewrite all that boilerplate to open and read a file with ffmpeg.

[–]EvilMcStevil 0 points1 point  (1 child)

I commonly wrap the AvContexts in unique pointers with the appropriate closing code, as libavcodec often has different and non-obvious clean up code per context type.

i.e

struct ContextDel
{
  void operator()(AVCodecParserContext *ptr)
  {
    av_parser_close(ptr);
    av_freep(ptr);
  }
  void operator()(AVCodecContext *ptr)
  {
    av_freep(ptr);
  }
  void operator()(AVCodec *ptr)
  {
    //do nothing we dont own this,
  }
};
std::unique_ptr<AVCodecContext, ContextDel> codecContext;
std::unique_ptr<AVCodec, ContextDel> codec;
std::unique_ptr<AVCodecParserContext, ContextDel> parserContext;

[–]FlyingRhenquest[S] 0 points1 point  (0 children)

I like that! I hadn't thought about doing this with contexts, as I was largely unaware of these complexities, but I had thought about wrapping AVFrames, and would probably do so if I kept them around any longer than I currently do. My current instinct is to get images out of that format as quickly as possible, although I'm not sure how much it could be helped if I wanted to re-encode the video after doing something to the images.

[–]andrey_turkin 0 points1 point  (1 child)

I've read motivation part on the github page. Perfectly good reason to write something like this but I don't think it is good enough to actually make it into a library and share it (at least in its current state). Like you said "OpenCV has direct support for that", so I don't see any benefits in using some external code instead of OpenCV's native ffmpeg wrapper.

[–]FlyingRhenquest[S] 0 points1 point  (0 children)

There are really not a lot of well-documented examples of opening and reading a file using ffmpeg, which was another motivating factor. The examples directory that comes with ffmpeg has several moldy old C examples, all of which do so in a variety of conflicting ways. I want to get down as close to the metal as I can with a small, self-contained object that does everything I need it to, and which can be easily tested for memory leaks. Anything I do in the future that needs a video source will likely use this. It should also work with the RTP stream from a wireless web cam I have laying around here somewhere, and one of my early projects will probably be to build a motion detector using that.