User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

How do you wrap/encode the .h264 video files into mp4

Wed May 15, 2013 5:47 pm

Got some vid files I want to edit, but video editing software won't play ball with the .h264 files.

Presumably I need to dump the .h264 raw into an mp4 package or something? I expect it can be done with FFMPEG but no idea how.

Can anyone help?
Alex Eames RasPi.TV, RasP.iO

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: How do you wrap/encode the .h264 video files into mp4

Wed May 15, 2013 7:30 pm

Assuming this is not a Raspberry Pi related issue then I would recommend you try Avidemux for editing your video files.
If that doesn't work then re-encoding with ffmpeg or remuxing the video streams may help i.e.
ffmpeg -i infile.mp4 -acodec copy -vcodec copy outfile.mp4

If you intend to edit you video clips on a frame by frame basis then you'll need to use a small GOP sequence like 1.
To encode mpeg4 / h.264 I use the following parameters:

ffmpeg -i infile.mp4 -crf 22.0 -acodec copy -vcodec libx264 -flags +loop+mv4 -cmp 256 -me_range 16 -g 64 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10 -qmax 51 -qdiff 4 outfile.mp4

To set GOP to 1 use -g 1 instead of -g 64, the -crf option is used to define frame quality bit rate instead of fixed bit rate (lower values are better but anything between 22 - 25 should suffice).

Richard S.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: How do you wrap/encode the .h264 video files into mp4

Wed May 15, 2013 7:46 pm

Thanks Richard, what I want to do is take the video I shot with the Pi camera, which is in some sort of raw .h264 format and get it into something I can edit with my normal editing software (Pinnacle 15)

Practically any format would do as long as the quality is good: avi, mp4, even the accursed .flv

I'm very inexpert in this area. :cry:
Alex Eames RasPi.TV, RasP.iO

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: How do you wrap/encode the .h264 video files into mp4

Wed May 15, 2013 7:51 pm

Does it open in VLC (VideoLAN) media player??

For re-encoding with ffmpeg I would recommend WinFF http://winff.org with the following as a preset:
-crf 22.0 -acodec copy -vcodec libx264 -flags +loop+mv4 -cmp 256 -me_range 16 -g 64 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10 -qmax 51 -qdiff 4

Richard S.

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

Re: How do you wrap/encode the .h264 video files into mp4

Wed May 15, 2013 7:56 pm

You really just need to wrap a container round it - mkv or similar. It's a raw H264 stream, it shouldn't need reencoding,
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I own the world’s worst thesaurus. Not only is it awful, it’s awful."

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5398
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: How do you wrap/encode the .h264 video files into mp4

Wed May 15, 2013 8:02 pm

You want something like:

Code: Select all

ffmpeg -r 30 -i test.264 -vcodec copy test.mkv
The "-r 30" is the framerate and you must use "-vcodec copy" or it will reencode and take forever.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: How do you wrap/encode the .h264 video files into mp4

Wed May 15, 2013 8:58 pm

dom wrote:You want something like:

Code: Select all

ffmpeg -r 30 -i test.264 -vcodec copy test.mkv
The "-r 30" is the framerate and you must use "-vcodec copy" or it will reencode and take forever.
And tonight's prize in "Help the retard" goes to Dom Cobley :lol: Thanks Dom.

That works in that I can make an mkv file. Pinnacle won't read it though, nor will Win Media player (spit) but once I convert to mp4 WMP will read it. Pinnacle still won't though. It will if I export it from VideoPad, but the max bitrate is 4096.

Well at least I have a way of getting the goods out of the camera, but I need to find something a bit less "round the houses" :) Was hoping to publish a day 1 video today. I think not.

Are either Dom or James going to the CamJam on Saturday?
Alex Eames RasPi.TV, RasP.iO

maddogdave
Posts: 46
Joined: Sat Dec 17, 2011 6:05 pm
Location: Berkshire England

Re: How do you wrap/encode the .h264 video files into mp4

Wed May 15, 2013 10:15 pm

Just used vlc player on my PC (which plays the files) and selected the stream / convert option. Gave the clip a new filename ending in .mp4 and it will now open in other players.
Also tried 'Handbrake' software which failed to convert the file.
Dave

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: How do you wrap/encode the .h264 video files into mp4

Wed May 15, 2013 10:47 pm

maddogdave wrote:Just used vlc player on my PC (which plays the files) and selected the stream / convert option. Gave the clip a new filename ending in .mp4 and it will now open in other players.
Also tried 'Handbrake' software which failed to convert the file.
Handbrake didn't work for me either, what did work was a hybrid of what Dom gave me above and Mike "recantha" Horne.

The million dollar command, which I executed on my Ubuntu laptop (I think Get_iplayer changed FFMPEG on my Pi) was...

Code: Select all

ffmpeg -r 30 -i video_in.h264 -vcodec copy video_out.mp4
And now Pinnacle will edit the mp4 files with no further piddling about.

Oh, yeah. I almost forgot. Here's the edited video output...
http://youtu.be/hvNe7oO9u5Y

Just a little day1 30s quick 'n' dirty. More tomorrow :)
Alex Eames RasPi.TV, RasP.iO

btidey
Posts: 1634
Joined: Sun Feb 17, 2013 6:51 pm

Re: How do you wrap/encode the .h264 video files into mp4

Fri May 17, 2013 11:03 am

Two extra variants I used were

Code: Select all

ffmpeg -f lavfi -i aevalsrc=0 -r 30 -i test.h264 -shortest -c:v copy -c:a aac -strict experimental testo.mp4
and

Code: Select all

ffmpeg -i test.mp3 -r 30 -i test.h264 -shortest -c:v copy -c:a aac -strict experimental testo.mp4
The first adds a silent audio channel as some video editors don't work without this. The second adds a real audio track where the test.mp3 should normally be longer than the video as the output is the shortest

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: How do you wrap/encode the .h264 video files into mp4

Fri May 17, 2013 1:08 pm

Oh they're useful Bill. Thanks :) I'll add them to my blog article and credit you. :D
Alex Eames RasPi.TV, RasP.iO

tvjon
Posts: 744
Joined: Mon Jan 07, 2013 9:11 am

Re: How do you wrap/encode the .h264 video files into mp4

Fri May 17, 2013 1:34 pm

alexeames wrote:....
The million dollar command, which I executed on my Ubuntu laptop (I think Get_iplayer changed FFMPEG on my Pi) was...

Code: Select all

ffmpeg -r 30 -i video_in.h264 -vcodec copy video_out.mp4
.........
Using that one-liner on the RPi produces a reasonable speed of conversion too, although so far only tested on short clips from the camera :)

It looks like avconv uses similar syntax to ffmpeg, as substituting that (on RPi) also produced a working file.

Both files play fine using mplayer on a macintosh. I'll try editing them later.


@btidey,

thanks for those examples. I tried your second example using an existing m4a file, & your syntax was fine substituting avconv, but using the original ffmpeg, it wouldn't run, complaining of unrecognised option 'c:v'.

However, the resulting MP4 file interestingly went into very slow motion via omxplayer, yet played fine in mplayer on the mac, but no audio unfortunately.

I'll do some more testing later.

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

Re: How do you wrap/encode the .h264 video files into mp4

Wed Aug 14, 2013 12:34 pm

I have been trying to wrap a h264 at 2 and 4 fps into an mp4 by using the commands here, but nothing seems to work.

Here's what I input

Code: Select all

[email protected] /var/www/images $ sudo raspivid -t 30000 -o /var/www/images/surveillance.h264 -w 1280 -h 720 -fps 2
and then

Code: Select all

[email protected] /var/www/images $ sudo avconv -r 2 -i surveillance.h264 -vcodec copy surveillance.mp4
which returns

Code: Select all

avconv 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
[h264 @ 0x5d2760] max_analyze_duration reached
[h264 @ 0x5d2760] Estimating duration from bitrate, this may be inaccurate
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 (copy)
Press ctrl-c to stop encoding
frame=   58 fps=  0 q=-1.0 Lsize=     547kB time=0.01 bitrate=448104.0kbits/s    
video:546kB audio:0kB global headers:0kB muxing overhead 0.225455%
And apparently gives me a very short video file, the framerate of which is equal to all the frames in the h264 video.

What am I doing wrong?

Trying

Code: Select all

[email protected] /var/www/images $ sudo raspivid -t 30000 -o /var/www/images/surveillance.h264 -w 1280 -h 720 -fps 2
and

Code: Select all

[email protected] /var/www/images $ sudo ffmpeg -r 2 -i surveillance.h264 -vcodec copy -r 2 surveillance.mp4
gives me

Code: Select all

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

[b]Seems stream 0 codec frame rate differs from container frame rate: 2400000.00 (2400000/1) -> 2.00 (2/1)[/b]
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
[b]frame=   58 fps[/b]=  0 q=-1.0 Lsize=     495kB time=0.01 bitrate=405828.0kbits/s    
video:494kB audio:0kB global headers:0kB muxing overhead 0.249000%
Last edited by flippant on Wed Aug 14, 2013 2:17 pm, edited 1 time in total.

ripping
Posts: 32
Joined: Wed Mar 14, 2012 10:55 am
Location: New Zealand

Re: How do you wrap/encode the .h264 video files into mp4

Thu Aug 15, 2013 5:24 am

MP4Box wraps an h264 file in an mp4 container.

Code: Select all

$ sudo apt-get install gpac
once installed:

Code: Select all

$ MP4Box -add inputvideo.h264 outputvideo.mp4

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

Re: How do you wrap/encode the .h264 video files into mp4

Thu Aug 15, 2013 8:42 am

ripping wrote:MP4Box wraps an h264 file in an mp4 container.

Code: Select all

$ sudo apt-get install gpac
once installed:

Code: Select all

$ MP4Box -add inputvideo.h264 outputvideo.mp4
You should, however, also specify the framerate like this:

Code: Select all

MP4Box -fps 30 -add inputvideo.h264 outputvideo.mp4
If you specified a framerate for raspivid, you should replace 30 in the MP4Box command with the same value you used for raspivid. If you don't specify a framerate for MP4Box, it will use 25 as default, which is close enough to raspivid's default 30 fps that you might not notice the discrepancy at first glance. Personally, I didn't notice anything until I started using lower fps for raspivid, though I did wonder why my 10 seconds long video clips alway wound up 11 seconds long in playback :-) . I have used MP4Box successfully with 15 fps clips, but I can't promise what it will do at 1 or 2 fps.

--
Regards, Kári.

Return to “Camera board”