Order Now AdSolution Sign Up | Login » Bits on the Run Sign Up | Login »

Forums

/

FFMPEG + MP4Box + h.264 question

33 replies [Last post]
Reply

Hello, Im trying to encode ...Hello, Im trying to encode videos to high quality and bandwidth friendly mp4 video (h.264) , the point is also to make them pseudo-streamable with the lighttpd h.264 module.

I'm using ffmpeg command:

ffmpeg -i start.avi -s 640:352 -acodec libfaac -ab 32k -ac 1 -vcodec libx264  -aspect 16:9 -vpre hq -crf 22 -threads 0 end.mp4

The video is a mythbusters episode

After that, I tried doing the whole moov atom moving with MP4Box

MP4Box

MP4Box -inter 500 final.mp4

Then I added the player to a website and tried to play it, the video took around 1 30 seconds to even start to play, however after it started to play the video played fine with no stuttering, however, these 30 seconds are misleading. Can anyone help?

Thanks!

Reply

From the MP4box documentation here: http://gpac.sourceforge.net/doc_mp4box.php

As of version 0.2.4, MP4Box always stores the file with 0.5 second interleaving and meta-data at the begining, making it suitable for HTTP streaming.

-inter Duration : interleaves media data in chunks of desired duration (in seconds). This is usefull to optimize the file for HTTP/FTP streaming or reducing disk access. All meta data are placed first in the file, allowing a player to start playback while downloading the content. By default MP4Box always stores files with half a second interleaving and performs drift checking between tracks while interleaving. Specifying a 0 interleaving time will result in the file being stored without interleaving, with all meta-data placed at beginning of the file.

So, use:

MP4Box end.mp4

Reply

Did, just that...

http://66.90.73.189/sampler/test.html

and still, taking a good while to start playing...

Reply

Also:

[root@centos sampler]# MP4Box -info ffmpegoutput.mp4
* Movie Info *
        Timescale 1000 - Duration 01:30:13.992
        Fragmented File no - 4 track(s)
        File Brand mp42 - version 1
        Created: GMT Fri Jan  2 00:00:00 1970

File has root IOD
Scene PL 0x01 - Graphics PL 0x01 - OD PL 0x01
Visual PL: Not part of MPEG-4 Visual profiles (0xfe)
Audio PL: AAC Profile @ Level 2 (0x29)

iTunes Info:
        Encoder Software: Lavf52.32.0

Track # 1 Info - TrackID 201 - TimeScale 24000 - Duration 01:30:13.991
Media Info: Language "Undetermined" - Type "vide:avc1" - 129803 samples
MPEG-4 Config: Visual Stream - ObjectTypeIndication 0x21
AVC/H264 Video - Visual Size 640 x 352 - Profile High @ Level 3
NAL Unit length bits: 32
Pixel Aspect Ratio 22:17 - Indicated track size 828 x 352
Synchronized on stream 1

Track # 2 Info - TrackID 101 - TimeScale 48000 - Duration 01:30:13.845
Media Info: Language "Undetermined" - Type "soun:mp4a" - 253774 samples
MPEG-4 Config: Audio Stream - ObjectTypeIndication 0x40
MPEG-4 Audio AAC LC - 1 Channel(s) - SampleRate 48000
Synchronized on stream 1

Track # 3 Info - TrackID 2 - TimeScale 1000 - Duration 00:00:01.000
Track is present in Root OD
Media Info: Language "Undetermined" - Type "odsm:mp4s" - 1 samples
MPEG-4 Config: ObjectDescriptor Stream - ObjectTypeIndication 0x01
Synchronized on stream 1

Track # 4 Info - TrackID 1 - TimeScale 1000 - Duration 00:00:01.000
Track is present in Root OD
Media Info: Language "Undetermined" - Type "sdsm:mp4s" - 1 samples
MPEG-4 Config: SceneDescription Stream - ObjectTypeIndication 0x02
BIFS Scene description - Command stream
Self-synchronized

Reply

The MOOV atoms make up a large percentage of the overall size of an H.264 encoded video in a MP4 container.

For your video file with a total time of 1:30:13, 30seconds to load the matadata is not unreasonable.

You could try interleaving at a slightly longer interval (the default is 0.5seconds), but you won't see much improvement in the initial startup time.

Also, since you're using LightTPD to stream the MP4, it has some delay while it pre-processes the metadata. More information is available at the code-shop website (code-shop makes the H.264 streaming modules).

Reply

So, when I see the circle turning, it is actually downloading the meta data first?

Reply

I could always MUX the mp4 to flv to keep the quality

ffmpeg -i name_file.mp4 -vcodec copy -acodec copy name_file.flv

and then use flvtool2 to add the metadata, would this work or is it trivial?

Reply

Nobody has come up with a method of pseudostreaming H.264 in a FLV container.

You can MUX it and it will work as a progressive download, but not pseudostreaming.

Some of the available tools will create and inject the metadata into the FLV, but it won't work correctly.

Reply

I just noticed that, i was able to move forward into the video but no image, just sound... *sighs*.. Btw, thanks for your help, im learning as I go rick... the quality of the video is great with the presets that im currently using, for h.264, however, do you know a good command line to convert to vimeo similar quality for flv (they use flv)

Reply

Two-pass encoding is always much better.

This is what I use for HD. Change the size and bump the bitrate up a bit for your video.

:: HD 2-pass transcode to MP4[H.264/AAC] with W & H divisible by 16
:: first pass
ffmpeg -y -i video.flv -an                               -pass 1 -threads 2 -vcodec libx264 -s 640x400 -b 512k -bf 3 -subq 6 -cmp 256 -refs 5 -qmin 10 -qmax 51 -qdiff 4 -coder 1 -loop 1 -me_method hex -me_range 16 -trellis 1 -flags +mv4 -flags2 +bpyramid+wpred+mixed_refs+dct8x8 -partitions parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -g 240 -r 20 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 video_HD2P.mp4
:: second pass
ffmpeg -y -i video.flv -acodec libfaac -ar 44100 -ab 96k -pass 2 -threads 2 -vcodec libx264 -s 640x400 -b 512k -bf 3 -subq 6 -cmp 256 -refs 5 -qmin 10 -qmax 51 -qdiff 4 -coder 1 -loop 1 -me_method hex -me_range 16 -trellis 1 -flags +mv4 -flags2 +bpyramid+wpred+mixed_refs+dct8x8 -partitions parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -g 240 -r 20 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 video_HD2P.mp4
:: move MOOV atoms
qt-faststart video_HD2P.mp4 video_HD2P.out.mp4

qt-faststart is similar to MP4Box, but I'm finding that MP4Box is better.

Reply

Great, thats for mp4 though,

I'm thinking if I can achieve the same quality that vimeo uses for videos(they convert to flv) - as convert X to *.flv - and then inject with flvtool2 to allow psstreaming, any thoughts?

Reply

OH! Yeah... Well...

H.264 will always give you a smaller file, much higher quality, less CPU (display + download) usage on the client. Still, there's that pesky startup delay.

(I don't know if you've noticed it, but Flash is a real CPU hog when it is displaying and simultaneously downloading. The CPU usage drops considerably once the download is done.)

For FLV, it depends on the source codec. Is it already compressed with something like XviD, or straight off of a DVD?

There's a lot of good transcoding information at the doom9 forums and videohelp.com.

Reply

How long should I expect MP4Box end.mp4 to take? it happens all too fast on a 360mb video (40 minutes long)

Reply

Sorry, I think I shortened the command line too much. And there should be a progress bar.

It should take about 10seconds per 100MB.

MP4Box -inter 0.5 video.mp4
Reply

Rick, the command line above outputs the following:

Seems stream 0 codec frame rate differs from container frame rate: 29.98 (65535/2186) -> 29.97 (2997003/100000)
Input #0, avi, from 'mythbusters.avi':
Duration: 00:43:02.76, start: 0.000000, bitrate: 1132 kb/s
Stream #0.0: Video: mpeg4, yuv420p, 624x352 [PAR 1:1 DAR 39:22], 29.97 tbr, 29.97 tbn, 29.98 tbc
Stream #0.1: Audio: mp3, 48000 Hz, stereo, s16, 128 kb/s
Invalid value '1' for option 'loop'

and closes right after that, do you get the same?

Reply

ffmpeg is constantly changing.

Run it with the -h switch to get the help output.

Depending on the version that you are using, the correct switch to enable the loop filter may be:

-flags +loop

Reply

None of this works....

Is any chance of a MP4BOX line to use to convert mp4 in order they play on the JW PLAYER?

Hope anyone test and post their working result here...

Cheers

Reply

The JW FLV Player plays H.264 video in the MP4 container; no conversion is necessary.

MP4Box is only used to shift the MOOV atoms to the beginning of the MP4 container so the file starts playing quickly.

Reply

@rick: have you tried pseudostreaming h264 in flv with scrubbing, with JWPlayer v3 series, or Flowplayer v3 series? Works just fine. The metadata injectors are fine even though they don't parse and include all the extra data they normally do (e.g. video size); they get the keyframe offsets correctly, which is all that matters in this case.

Must be b/c the client player caches info about the video, b/c the actual scrubbed video returned by lighttpd is not a well-formed FLV on its own. I.e. mplayer will not play it.

Reply

JW v3 is quite obsolete; it's almost time for JW v5.

The available H.264 streaming solutions such as nginx, LightTPD, and Apache2 with the streaming modules work great, so there's no need to fool around with H.264 in the FLV container anymore.

Red5 with RTMP H.264 will also be available soon.

Reply

Sure JW v3 obsolete, but Flowplayer v3 is not. Just pointing out where its feasible (h264 in flv scrubbing).

Sure, mp4 pseudo-streaming w/scrubbing works too, but here you have the hassle of the size of the moov atom. Hassle for the server, hassle for the client to wait for the new moov atom once scrubbed. Depends on the number of keyframes, of course. None of these hassles with h264 in flv though. :)

Reply

@mark: h264 in FLV? how ? :D

Reply

@Mario - have you tried with jw3.x or FlowPlayer v3? (I had it working with FlowPlayer 3.07 - haven't tested more recent 3.1, 3.1.1).

Reply

Regarding MP4 and the moov atom, I tried MP4Box and just couldn't get it to work. I then switched to Atomic Parsley, and have never had a problem. Like X264, Atomic Parsley is open source, freely available, and my host easily installed it. :)

Reply

I've found that Adobe Media Encoder CS4 v4.2.0.006 places the moov atoms where they belong for progressive downloading of .f4v files (at the start).

Reply

Hello, I'm trying to play a video on JWPlayer . I was wondering what it would take to create an mp4 with ffmpeg to be able to psudostream?

I can stream flv's without any problem.

Here is my ffmpeg capture command.

ffmpeg -i test.mp4 -metadata title="Test" -metadata artist="Test" -metadata date="2011" -acodec copy -vcodec copy test2.mp4

The resulting file has these attributes...

Output #0, mp4, to 'test2.mp4':
Metadata:
date : 1995
artist : Test
title : Test
Stream #0.0(und): Video: mpeg4, yuv420p, 426x320 [PAR 1:1 DAR 213:160], q=2-
31, 867 kb/s, 24 tbn, 24 tbc
Stream #0.1(und): Audio: aac, 44100 Hz, stereo, 128 kb/s
Stream mapping:
Stream #0.0 -> #0.0
Stream #0.1 -> #0.1
Press [q] to stop encoding
frame= 4293 fps= 0 q=-1.0 size= 21289kB time=177.89 bitrate= 980.4kbits/s
frame= 5362 fps=5047 q=-1.0 size= 25763kB time=222.40 bitrate= 949.0kbits/s
frame= 5850 fps=3707 q=-1.0 size= 29562kB time=242.74 bitrate= 997.7kbits/s
frame= 6631 fps=3596 q=-1.0 Lsize= 33711kB time=276.13 bitrate=1000.1kbits/s

video:29248kB audio:4315kB global headers:0kB muxing overhead 0.441274%

when i run the video file it starts buffering, waits until completion of the buffer and plays the video. Here video not working but audio working.

my metadata looks like this

(Array)#0
[aacaot] 2
[audiochannels] 2
[audiocodecid] "mp4a"
[audiosamplerate] 44100
[duration] 276.1317006802721
[moovposition] 34368017
[tags] (Array)#1
[©nam] "Test"
[©too] "Lavf52.48.0"
[trackinfo] (Array)#2
[0] (Object)#3
language = "und"
length = 6631
sampledescription = (Array)#4
[0] (Object)#5
sampletype = "mp4v"
timescale = 24
[1] (Object)#6
language = "und"
length = 12177408
sampledescription = (Array)#7
[0] (Object)#8
sampletype = "mp4a"
timescale = 44100

Can anyone Help. How to inject an mp4 file
Thanks in Advance.

Reply

There seem to be two problems:

*) Your video doesn't seem to be in H264 format. Anything but H264 will not play in Flash. You probably use Mpeg4 / DivX / XviD?

*) The "MOOV" box of your MP4 seems to be at the end of the video. That prevents streaming, since the entire file needs loading until metadata is available. There's tools out there that can move this box to the start of the file, e.g. "qtfaststart".

Reply

This thread is pretty old, just case people still interested in doing it in php. I am in the assumption here that the mp4 video has already been processed or encoded, and what is there to do is to move the moov atom.

given:
$mp4_loc = "somedirectory"; //directory where the mp4 is located this is important. Do not include the video. Just the directory for the mp4box to write temp file. make sure the directory is writable.

$mp4_file_loc = "somedirectory/somefile.mp4"; //here you need to include the video..
$mp4box_loc = "/usr/bin/MP4Box"; //your server location of mp4Box
$atom_m = ($mp4box_loc." -tmp ".$mp4_loc." -hint $mp4_file_loc");
@exec($atom_m." > /dev/null &"); //execute the process in the background.

I hope this helps, courtesy of PoorBoy from http://veedeoo.com/forum/

Reply

What would be the best settings to encode HD and SD files with ffmpeg 2 pass encoding with H264 video codec, format Mp4, any idea of the commands lines?

Reply

Please see our blog, we have a few articles on this:

http://www.longtailvideo.com/support/blog/tag/transcoding/entries

Reply

Hi, very good approach.

But i can't find a lot about the command lines for ffmpeg to achieve the presets you recommend...

Do you have a link for that?
I found a lot, but I am not sure to use the correct ones.
thanks.

Reply

I'm afraid that's also a bit too techie for me.

One tool that always produces great encodes is Handbrake. Why don't you download that tool and look at the setting they use? I believe they display it in one of the settings tabs.

Reply

how to insert meta data in mp4 video

Reply

Post new comment

  • Allowed HTML tags: <code> <blockquote> <em> <strong> <strike> <ul> <li> <ol>
  • You may post code using <code>...</code> .
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options