A long time ago I hit this and as far as I was then able to tell, there is not 
a lightweight way to get the precise number of frames in a video file. I seem 
to recall (don't ask me how) there was a way to effectively have it "play" the 
file, to parse the container, without actually doing the work of decoding the 
frames. That's less hard work than literally decoding it all. It's still not a 
very cheap way to count frames, but it was the best solution I could find. 
Someone here may be able to advise.
P
    On Wednesday, 5 August 2020, 18:01:27 BST, Tom Burrows via ffmpeg-user 
<[email protected]> wrote:  
 
 I'm a developer of the Python MoviePy package and I'm trying to rework how we 
import file information to be more robust.

I need to get the duration of any video file such that when multiplied with the 
media's fps value (in this case, either ’tbr’ or ‘fps'), it equals the number 
of frames in the video.

I've found 3 solutions:

(1) ffmpeg -i broken_video.mp4
(2) ffmpeg -i broken_video.mp4 -f null -
(3) ffmpeg -i broken_video.mp4 -codec copy -f null -
(Full outputs at the end)

FFmpeg reports that the video has 29.90 fps, 30 tbr, and contains 305 frames.

They all list Duration: 00:00:10.26 near the top. I believe that this is 
derived from the metadata and has proved to be unreliable, so I'd rather avoid 
using this. In this case, 10.26x30=307.8

(2) has time=00:00:10.21 near the bottom. 10.21x30=306.3. Also, this one is a 
lot slower since it is re-encoding the entire file.

(3) has time=00:00:10.19 10.19x30=305.7. This is the closest to 305, although 
it still rounds incorrectly.

I've also considered simply taking the fps and the number of frames and 
generating the duration from this (in this case, 30x305= 10.16667), but I’m 
assuming that sometimes the fps is wrong? (occasionally the tbr is something 
crazy like 10k and that would surely create an incorrect duration. Maybe this 
behaviour wouldn’t happen with ‘fps’?).

Is there a better solution to this problem?

—————

$ ffmpeg -i broken_video.mp4
ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.17)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.2_2 --enable-shared 
--enable-pthreads --enable-version3 --enable-avresample --cc=clang 
--host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl 
--enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus 
--enable-librubberband --enable-libsnappy --enable-libtesseract 
--enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx 
--enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid 
--enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r 
--enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb 
--enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr 
--enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 31.100 / 56. 31.100
  libavcodec    58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter    7. 57.100 /  7. 57.100
  libavresample  4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample  3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'broken_video.mp4':
  Metadata:
    minor_version  : 512
    major_brand    : isom
    compatible_brands: isomiso2avc1mp41
    information    : {"com.bytedance.info": "{}"}
    comment        : vid:v0200f970000brb4glubn5v3g13q5db0
    encoder        : Lavf58.20.100
  Duration: 00:00:10.26, start: 0.000000, bitrate: 743 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, 
smpte170m/bt470bg/smpte170m), 576x1024 [SAR 1:1 DAR 9:16], 608 kb/s, 29.90 fps, 
30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, 
fltp, 127 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
At least one output file must be specified

————

$ ffmpeg -i broken_video.mp4 -f null -
ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.17)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.2_2 --enable-shared 
--enable-pthreads --enable-version3 --enable-avresample --cc=clang 
--host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl 
--enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus 
--enable-librubberband --enable-libsnappy --enable-libtesseract 
--enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx 
--enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid 
--enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r 
--enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb 
--enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr 
--enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 31.100 / 56. 31.100
  libavcodec    58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter    7. 57.100 /  7. 57.100
  libavresample  4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample  3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'broken_video.mp4':
  Metadata:
    minor_version  : 512
    major_brand    : isom
    compatible_brands: isomiso2avc1mp41
    information    : {"com.bytedance.info": "{}"}
    comment        : vid:v0200f970000brb4glubn5v3g13q5db0
    encoder        : Lavf58.20.100
  Duration: 00:00:10.26, start: 0.000000, bitrate: 743 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, 
smpte170m/bt470bg/smpte170m), 576x1024 [SAR 1:1 DAR 9:16], 608 kb/s, 29.90 fps, 
30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, 
fltp, 127 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> wrapped_avframe (native))
  Stream #0:1 -> #0:1 (aac (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, null, to 'pipe:':
  Metadata:
    minor_version  : 512
    major_brand    : isom
    compatible_brands: isomiso2avc1mp41
    information    : {"com.bytedance.info": "{}"}
    comment        : vid:v0200f970000brb4glubn5v3g13q5db0
    encoder        : Lavf58.29.100
    Stream #0:0(und): Video: wrapped_avframe, yuv420p, 576x1024 [SAR 1:1 DAR 
9:16], q=2-31, 200 kb/s, 30 fps, 30 tbn, 30 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder        : Lavc58.54.100 wrapped_avframe
    Stream #0:1(und): Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s 
(default)
    Metadata:
      handler_name    : SoundHandler
      encoder        : Lavc58.54.100 pcm_s16le
frame=  305 fps=0.0 q=-0.0 Lsize=N/A time=00:00:10.21 bitrate=N/A speed=27.1x   
 
video:160kB audio:1760kB subtitle:0kB other streams:0kB global headers:0kB 
muxing overhead: unknown

—————

$ ffmpeg -i broken_video.mp4 -codec copy -f null -
ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.17)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.2_2 --enable-shared 
--enable-pthreads --enable-version3 --enable-avresample --cc=clang 
--host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl 
--enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus 
--enable-librubberband --enable-libsnappy --enable-libtesseract 
--enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx 
--enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid 
--enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r 
--enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb 
--enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr 
--enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 31.100 / 56. 31.100
  libavcodec    58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter    7. 57.100 /  7. 57.100
  libavresample  4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample  3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'broken_video.mp4':
  Metadata:
    minor_version  : 512
    major_brand    : isom
    compatible_brands: isomiso2avc1mp41
    information    : {"com.bytedance.info": "{}"}
    comment        : vid:v0200f970000brb4glubn5v3g13q5db0
    encoder        : Lavf58.20.100
  Duration: 00:00:10.26, start: 0.000000, bitrate: 743 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, 
smpte170m/bt470bg/smpte170m), 576x1024 [SAR 1:1 DAR 9:16], 608 kb/s, 29.90 fps, 
30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, 
fltp, 127 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Output #0, null, to 'pipe:':
  Metadata:
    minor_version  : 512
    major_brand    : isom
    compatible_brands: isomiso2avc1mp41
    information    : {"com.bytedance.info": "{}"}
    comment        : vid:v0200f970000brb4glubn5v3g13q5db0
    encoder        : Lavf58.29.100
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, 
smpte170m/bt470bg/smpte170m), 576x1024 [SAR 1:1 DAR 9:16], q=2-31, 608 kb/s, 
29.90 fps, 30 tbr, 15360 tbn, 15360 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, 
fltp, 127 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=  305 fps=0.0 q=-1.0 Lsize=N/A time=00:00:10.19 bitrate=N/A 
speed=2.14e+03x    
video:758kB audio:160kB subtitle:0kB other streams:0kB global headers:0kB 
muxing overhead: unknown
_______________________________________________
ffmpeg-user mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".  
_______________________________________________
ffmpeg-user mailing list
[email protected]
https://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
[email protected] with subject "unsubscribe".

Reply via email to