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

前端 未结 4 2015
半阙折子戏
半阙折子戏 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:55

    I had to add a call to

    avformat_find_stream_info(pFormatCtx,NULL)
    

    after avformat_open_input to get mgiuca's answer to work. (can't comment on it)

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

    The duration is in uSeconds, divide by AV_TIME_BASE to get seconds.

提交回复
热议问题