jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23631
Joined: Sat Jul 30, 2011 7:41 pm

Re: avconv reads h264 camera stream but doesn't write fps

Wed Aug 14, 2013 2:48 pm

An H264 stream doesn't contain any fps information - since something seems to think you are 25fps, how did it work that out? I wonder if it guessed, and really you should be specifying the fps you want to be applied?
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

flippant
Posts: 31
Joined: Wed Aug 07, 2013 7:28 am

Re: avconv reads h264 camera stream but doesn't write fps

Wed Aug 14, 2013 3:13 pm

pi@rpi /var/www/images $ sudo ffmpeg -r 2 -i surveillance.h264 -vcodec copy -r 2 surveillance.mp4
as per the manual, I specified both input and output framerates, and got this (avconv was the same):
ffmpeg version 0.8.6-6:0.8.6-1+rpi1, Copyright (c) 2000-2013 the Libav developers
built on Mar 31 2013 13:58:10 with gcc 4.6.3
*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
[h264 @ 0xfcc660] max_analyze_duration reached
[h264 @ 0xfcc660] Estimating duration from bitrate, this may be inaccurate

Seems stream 0 codec frame rate differs from container frame rate: 2400000.00 (2400000/1) -> 2.00 (2/1)
Input #0, h264, from 'surveillance.h264':
Duration: N/A, bitrate: N/A
Stream #0.0: Video: h264 (High), yuv420p, 1280x720, 2 fps, 2 tbr, 1200k tbn, 2400k tbc
File 'surveillance.mp4' already exists. Overwrite ? [y/N] y
Output #0, mp4, to 'surveillance.mp4':
Metadata:
encoder : Lavf53.21.1
Stream #0.0: Video: libx264, yuv420p, 1280x720, q=2-31, 1200k tbn, 1200k tbc
Stream mapping:
Stream #0.0 -> #0.0
Press ctrl-c to stop encoding
frame= 58 fps= 0 q=-1.0 Lsize= 495kB time=0.01 bitrate=405828.0kbits/s
video:494kB audio:0kB global headers:0kB muxing overhead 0.249000%

Multiverse
Posts: 4
Joined: Sun Aug 04, 2013 4:02 am

Re: avconv reads h264 camera stream but doesn't write fps

Wed Aug 21, 2013 9:10 am

I had a heck of a time getting my raw H.264 stream from the raspberry pi camera module containerized and playing back at the proper rate. No matter how I formatted it with avconv, the video always played back at three times the rate. Furthermore, it appeared avconv was not outputting all the input frames. (I had a 10s video at 25fps for a total of 250 frames, with bitrate 30Mbps and dimensions 1920x1080.) During conversion, avconv always reported 85 frames instead of the total 250 frames.

So why does avconv produce a file that plays too fast, with fewer frames than the input frames? I think the problem is the 'presentation timestamps' (PTS) on the frames of the raw video produced by raspivid. When I used the showinfo filter of avconv, I could see that the last frame PTS was 3.32 when it should have been 10, because I recorded ten seconds of video. When I rescaled the PTS values using the setpts filter, I chose a scale factor of 3.0 to slow it down. To my satisfaction, avconv then reported 249 frames output. The video plays back at the proper rate, has the same number of frames as the input, and looks fantastic.

I think what avconv does when you format the h.264 stream into an mp4 container is combine (or duplicate) input frames in order to give the best-looking output at the given fps and bitrate. It may combine or create new frames depending on the difference in time between successive input frames and output frames. The number of frames reported is the number of output frames, not the number of input frames.

BTW, if you try this conversion on your Rpi it can process only about 1 frame per second. Avconv doesn't sense any special ability of the CPU to help with the graphics operations. On my Dell E4300 laptop, avconv processes up to 16 frames per second. However, the avconv that comes with Raspbian is a strange version available only on that platform, and it appears to interpret the timestamps 'correctly'. A raw H.264 video re-encoded as an mp4 by avconv in rasbpian will play back at the nominal rate, and will contain the original number of frames.

This realization only took a week of downloading and playing with every last video conversion software offered in the Ubuntu repositories!

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: avconv reads h264 camera stream but doesn't write fps

Wed Aug 21, 2013 9:57 am

Raw H264 doesn't have timestamps. No PTS.

libav is just bugged.

kaos
Posts: 108
Joined: Mon Mar 26, 2012 8:14 pm

Re: avconv reads h264 camera stream but doesn't write fps

Wed Aug 21, 2013 8:33 pm

Did you try MP4Box, part of the GPAC package? I'm very happy with it, both fast and simple to use. You have to let it know the framerate you shot the video at though; as towolf pointed out there are no timestamps in the h264 stream.

--
Regards, Kári.

Multiverse
Posts: 4
Joined: Sun Aug 04, 2013 4:02 am

Re: avconv reads h264 camera stream but doesn't write fps

Thu Aug 22, 2013 2:02 am

I like the fact that MP4Box has only one non-gpac dependency. I will try it.

Also, thanks for pointing out that the raw h.264 stream has no presentation timestamps (PTS). This makes sense. So different versions of avconv assume different delay offsets between input frames. With that knowlege, and from what I saw in the output from the showinfo filter, I now have the formula for reformating raw h.264 into the MP4 container with a somewhat proper playback rate:

Code: Select all

$ avconv -i test_30Mbps_hd1080.h264  -b 30M -filter setpts='(N/25.0)*1000000' -y test_30Mbps_hd1080_avconv_setpts.mp4
Since the setpts filter argument is in nanoseconds, and the original video was recorded at 25 fps, a good estimate of the timestamp is frame number N * 1,000,000 / 25 fps. This resulted in exactly 250 output frames, and actually processed faster than real time on my laptop:

Code: Select all

frame=  250 fps= 32 q=2.0 Lsize=   26092kB time=10.00 bitrate=21374.3kbits/s    
video:26089kB audio:0kB global headers:0kB muxing overhead 0.010765%

Multiverse
Posts: 4
Joined: Sun Aug 04, 2013 4:02 am

Re: avconv reads h264 camera stream but doesn't write fps

Thu Aug 22, 2013 2:46 am

Wow, MP4Box is the way to go! Since it's not a transcoder, it doesn't waste time analyzing and processing image data. It is fast. It's also very simple to use. This was all I needed to put my raw H.264 stream into an MPEG4 container:

Code: Select all

$ MP4Box -add test_30Mbps_hd1080.h264%video -fps 25 test_30Mbps_hd1080_mp4box_25fps.mp4

AVC-H264 import - frame size 1920 x 1080 at 25.000 FPS
AVC Import results: 250 samples - Slices: 5 I 245 P 0 B - 0 SEI - 5 IDR
Saving to test_30Mbps_hd1080_mp4box_25fps.mp4: 0.500 secs Interleaving
It processed 10 seconds of HD 1080p video within just a few seconds, on my Raspberry Pi! It was instantaneous on my laptop. It also produces a smaller file than avconv.

This is exactly what I was looking for. Thanks!

Return to “Camera board”