Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFmpeg get video length when duration is equal to "AV_NOPTS_VALUE"

Tags:

ffmpeg

There's a couple of video I have where the duration value in the AVStream is set so AV_NOPTS_VALUE. But players like VLC are able to get the length of that video. Even the file property in Ubuntu can read it.

So when this happens, what should I do to get the file length? Either in number of frames or in seconds, doesn't really matter.

Thanks

P.S.: only with the API, not interested in calling FFmpeg in the command line.

like image 938
widgg Avatar asked Nov 19 '25 21:11

widgg


1 Answers

So I continued my research and found a solution:

    // Seek last key-frame.
    avcodec_flush_buffers(stream._codecContext);
    av_seek_frame(_context, stream._idx, stream.frameToPts(1<<29), AVSEEK_FLAG_BACKWARD);

    // Read up to last frame, extending max PTS for every valid PTS value found for the video stream.
    av_init_packet(&_avPacket);

    while (av_read_frame(_context, &_avPacket) >= 0) {
      if (_avPacket.stream_index == stream._idx && _avPacket.pts != int64_t(AV_NOPTS_VALUE) && _avPacket.pts > maxPts)
        maxPts = _avPacket.pts;
      av_free_packet(&_avPacket);
    }

I changed it a bit a fit my needs, but this is roughly what I used.

Ref: ffmpegReader.cpp, look for function getStreamFrames.

like image 196
widgg Avatar answered Nov 23 '25 04:11

widgg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!