3

Executing avprobe test.h264 outputs

Input #0, h264, from 'test.h264':
  Duration: N/A, bitrate: N/A
    Stream #0.0: Video: h264 (High), yuv420p, 720x480, 25 fps, 25 tbn, 50 tbc

Executing file test.h264 outputs

test.h264: JVT NAL sequence, H.264 video @ L 30

Note that the file isn't damaged or corrupted, I can play it with no problem on VLC.

Is there a way to get the duration and bitrate from a raw H264 file? I read somewhere that it might be possible if I decode the file first, but I'm not sure how this can be done.

Edit #1

I'm the one creating the H264 file with a Python library called picamera.

Edit #2

Console output when running avconv -i test.h264 -f null -

avconv version 11.7-6:11.7-1~deb8u1+rpi1, Copyright (c) 2000-2016 the Libav developers
  built on Jun 17 2016 02:13:49 with gcc 4.9.2 (Raspbian 4.9.2-10)
[h264 @ 0x1bcc200] Estimating duration from bitrate, this may be inaccurate
Input #0, h264, from 'test.h264':
  Duration: N/A, bitrate: N/A
    Stream #0.0: Video: h264 (High), yuv420p, 720x480, 25 fps, 25 tbn
 Output #0, null, to 'pipe:':
  Metadata:
    encoder         : Lavf56.1.0
    Stream #0.0: Video: rawvideo, yuv420p, 720x480, q=2-31, 200 kb/s, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc56.1.0 rawvideo
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> rawvideo (native))
Press ctrl-c to stop encoding
frame=  208 fps= 68 q=0.0 Lsize=       0kB time=10000000000.00 bitrate=   0.0kbits/s
video:13kB audio:0kB other streams:0kB global headers:0kB muxing overhead: unknown
VC.One
  • 14,790
  • 4
  • 25
  • 57
Maxime Dupré
  • 5,319
  • 7
  • 38
  • 72

3 Answers3

8

You could mux it to a container and then check

ffmpeg -i test.h264 -c copy test.mp4

ffprobe test.mp4

You can also count frames in the H264 and divide by frame rate

ffprobe test.h264 -count_frames -show_entries stream=nb_read_frames,avg_frame_rate,r_frame_rate

Duration = nb_read_frames / avg_frame_rate

Gyan
  • 85,394
  • 9
  • 169
  • 201
  • "Muxing" the file to a container seems to work, but when running the command it ouputs `Non-monotonous DTS in output stream 0:0; previous: 203, current: 0; changing to 204. This may result in incorrect timestamps in the output file.` for each frame it's trying to encode for the MP4 file. Also is it reliable? Thanks! – Maxime Dupré Jun 30 '16 at 18:48
  • 1
    No timestamps in a raw bitstream so it imputes `0` to each incoming frame, which is of course, wrong. But not a problem. If your bitstream doesn't have a framerate stored like @Ronald mentioned, then you should add `-framerate N` before `'-i` where N is the fps value else ffmpeg will assume 25 fps. – Gyan Jun 30 '16 at 18:56
  • `avconv -framerate 30 -i test.h264 -c copy test.mp4` works perfectly! Thanks! – Maxime Dupré Jun 30 '16 at 19:36
3

The reason duration is N/A is that annexb (raw) H.264 files usually have no timestamp or framerate information stored in the file. Framerate can optionally be stored in the VUI of the PPS, but this file apparently doesn't have that, so all it knows is framecount (by parsing the whole file), but not anything timestamp-related.

Ronald S. Bultje
  • 10,828
  • 26
  • 47
  • So I currently can't get the duration of the video file, but is there a way I can? – Maxime Dupré Jun 30 '16 at 14:53
  • as @Mulvya suggested, the most straightforward solution is to mux the video frames into a container format, e.g. mp4, mkv or something like that. These container formats contain timestamps and duration information. With the files you already created, you can manually re-add this information by remuxing them (using ffmpeg) into another container and informing ffmpeg about the source's framerate (ffmpeg -r 30 -i file.h264 -c:v copy file.mp4, I believe). – Ronald S. Bultje Jun 30 '16 at 15:01
2

One method is to decode the file with ffmpeg to get the duration:

ffmpeg -framerate 24 -i input.h264 -f null -

Then refer to time= in the second-to-last line in the console output for the duration. For example, a 5 second input:

frame=  125 fps=0.0 q=-0.0 Lsize=N/A time=00:00:05.00 bitrate=N/A speed= 189x
llogan
  • 121,796
  • 28
  • 232
  • 243
  • I get `time=10000000000.00`. My video is 6 seconds long, so I doubt that this information is meaningful. – Maxime Dupré Jun 30 '16 at 18:26
  • @maximedupre Worked for me. Show your actual command and the complete console output. Are you actually using `ffmpeg`? – llogan Jun 30 '16 at 19:38
  • I'm using `avconv` (fork of `ffmpeg` that does pretty much the same thing). I updated my question the complete console output. – Maxime Dupré Jun 30 '16 at 19:43
  • @maximedupre My answer was specifically for `ffmpeg`, which I assumed was acceptable since "ffmpeg" is in the question title and the ffmpeg tag was used. Please try `ffmpeg`. You can [download a static build](http://johnvansickle.com/ffmpeg/) and simply execute that. – llogan Jun 30 '16 at 19:47