radek-k / FFMediaToolkit

FFMediaToolkit is a cross-platform video decoder/encoder library for .NET that uses FFmpeg native libraries. It supports video frames extraction, reading stream metadata and creating videos from bitmaps in any format supported by FFmpeg.
MIT License
353 stars 56 forks source link

[HEVC] Duration overflow issue. #8

Closed redbaty closed 4 years ago

redbaty commented 4 years ago

First of all, I'd like to thank you for your efforts, this lib is pretty life saving (probably the first cross-platform library in .net which is not a complete mess).

Anyway, here's the problem, I've got an HEVC (H265) file which is exactly 22 minutes and 53 seconds long, but for some reason, the StreamInfo constructor fails to parse the correct duration causing an overflow (claiming that the duration is too long). I'm not so sure if this is related to this project or the project this is based on FFmpeg.AutoGen.

Here is some information that can be helpful:

redbaty commented 4 years ago

I've found a workout, tho this issue might be weirder than I thought, should I provide a sample file?

radek-k commented 4 years ago

I've tested this library with a 30 minutes long 60 fps h265 file which has 108 000 frames(generated by the following command) and it's working correctly. ffmpeg -t 0:30:00 -f lavfi -i color=c=black:s=640x480 -c:v libx265 -x265-params crf=25 -r 60 -pix_fmt yuv420p c:/video-h265.mp4

The bug may be in FFmpeg.AutoGen because stream->duration is set by it. Can you provide a sample file?

redbaty commented 4 years ago

As it is on my fork, I can only extract the duration from the AVContext.

radek-k commented 4 years ago

The bug probably is in FFmpeg.AutoGen or even in ffmpeg.

Could you open a pull request with the workaround - duration from AVContext (and also with the chapters support - your 2e86dd8 commit) to my develop branch?

devenv_ZRyeC743KW

redbaty commented 4 years ago

Sure, I'll open some pull requests, the only thing I'm worried about is breaking another file in which the old stream->duration method works but the new avcontext->duration doesn't.

radek-k commented 4 years ago

Which file it will break?

redbaty commented 4 years ago

I mean, if we move from stream->duration to AVContext->duration there's a chance in some specific file it'll break, right?

radek-k commented 4 years ago

Stream duration is often equal to the file duration (converted to seconds), but in some cases it can be shorter. The library may get the stream duration from AVContext only if the AVStream->duration is unavailable. Example:

if(stream->duration >= 0)
  return stream->duration;
else
  return avcontext->duration;
redbaty commented 4 years ago

Great suggestion! This way we keep compatibility and fixes files in which the stream duration is invalid.

radek-k commented 4 years ago

Fixed by #11