how to use libavcodec/ffmpeg to find duration of video file

前端 未结 4 2048
半阙折子戏
半阙折子戏 2020-12-28 17:31

I needed a library to perform basic functions such as length, size, etc of a video file (i\'m guessing through the metadata or tags) so I chose ffmpeg. Vali

4条回答
  •  醉酒成梦
    2020-12-28 17:40

    libavcodec is pretty hard to program against, and it's also hard to find documentation, so I feel your pain. This tutorial is a good start. Here is the main API docs.

    The main data structure for querying video files is AVFormatContext. In the tutorial, it's the first thing you open, using av_open_input_file -- the docs for that say it's deprecated and you should use avformat_open_input instead.

    From there, you can read properties out of the AVFormatContext: duration in some fractions of a second (see the docs), file_size in bytes, bit_rate, etc.

    So putting it together should look something like:

    AVFormatContext* pFormatCtx = avformat_alloc_context();
    avformat_open_input(&pFormatCtx, filename, NULL, NULL);
    int64_t duration = pFormatCtx->duration;
    // etc
    avformat_close_input(&pFormatCtx);
    avformat_free_context(pFormatCtx);
    

    If you have a file format with no headers, like MPEG, you may need to add this line after avformat_open_input to read information from the packets (which might be slower):

    avformat_find_stream_info(pFormatCtx, NULL);
    

    Edit:

    • Added allocation and de-allocation of pFormatCtx in the code example.
    • Added avformat_find_stream_info(pFormatCtx, NULL) to work with video types that have no headers such as MPEG

提交回复
热议问题