Log in

View Full Version : MP4Box Delay


LexManos
15th January 2011, 11:41
Alright So here is my situation, I have a few videos that I retardily used mencoder to encode into avi/x264/mp3.
Turns out my media player {the 360} doesn't support the 264/mp3 combo and I need to re-encode the audio into mp4/x264/aac.

So, instead of re-encoding the video, I figured i'd use MP4Box to extract the different tracks, re-encode the audio, and recombine.

I did that, and tried to play the file, and the audio was out of sync. Found out about the audio delay.

This is the MediaInfo (http://mediainfo.sourceforge.net/en/Download) of the original file:

General
Complete name : j:\Home071509.avi
Format : AVI
Format/Info : Audio Video Interleave
File size : 298 MiB
Duration : 44mn 15s
Overall bit rate : 941 Kbps
Writing application : MEncoder 1.0rc1-3.4.2
Writing library : MPlayer

Video
ID : 0
Format : AVC
Format/Info : Advanced Video Codec
Format profile : High@L5.1
Format settings, CABAC : Yes
Format settings, ReFrames : 8 frames
Codec ID : h264
Duration : 44mn 15s
Bit rate : 736 Kbps
Width : 720 pixels
Height : 576 pixels
Display aspect ratio : 16:9
Frame rate : 23.976 fps
Standard : PAL
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.074
Stream size : 233 MiB (78%)
Writing library : x264 core 54
Encoding settings : cabac=1 / ref=5 / deblock=1:0:0 / analyse=0x3:0x133 / me=umh / subme=6 / br
do=0 / mixed_ref=0 / me_range=16 / chroma_me=1 / trellis=0 / 8x8dct=1 / cqm=0 / deadzone=21,11 / chroma_qp_off
set=0 / slices=1 / nr=0 / decimate=1 / mbaff=0 / bframes=3 / b_pyramid=1 / b_adapt=1 / b_bias=0 / direct=1 / w
predb=1 / bime=0 / keyint=250 / keyint_min=25 / scenecut=40 / rc=cqp / qp=26 / ip_ratio=1.40 / pb_ratio=1.30

Audio
ID : 1
Format : MPEG Audio
Format version : Version 1
Format profile : Layer 3
Mode : Joint stereo
Mode extension : MS Stereo
Codec ID : 55
Codec ID/Hint : MP3
Duration : 44mn 15s
Bit rate mode : Constant
Bit rate : 192 Kbps
Channel(s) : 2 channels
Sampling rate : 44.1 KHz
Compression mode : Lossy
Video delay : 83ms
Stream size : 60.8 MiB (20%)
Alignment : Aligned on interleaves
Interleave, duration : 26 ms (0.63 video frame)
Interleave, preload duration : 522 ms
Writing library : LAME3.96.1If you notice the 'Video Delay' is 83ms, which i'm guessing is the right delay as there are no others.

This is the command set I used:

mp4box -aviraw audio %1 -out c:\tmp\aud.mp3
mp4box -aviraw video %1 -out c:\tmp\vid.h264

start /wait audioconverter c:\tmp\aud_audio.mp3 c:\tmp\aud.aac -c aac

"c:\Program Files\MediaCoder\tools\mediainfo.exe" --inform=Audio;%%Delay%% %1 > c:\tmp\log.txt

set /p delay=<c:\tmp\log.txt

mp4box -add "c:\tmp\aud.aac" -add "c:\tmp\vid_video.h264" -delay 1=%delay% -new "%_file%.mp4"

Which results in this file:
General
Complete name : j:\Home071509.mp4
Format : MPEG-4
Format profile : Base Media
Codec ID : isom
File size : 276 MiB
Duration : 44mn 15s
Overall bit rate : 871 Kbps
Encoded date : UTC 2011-01-15 09:57:49
Tagged date : UTC 2011-01-15 09:57:49

Video
ID : 2
Format : AVC
Format/Info : Advanced Video Codec
Format profile : High@L5.1
Format settings, CABAC : Yes
Format settings, ReFrames : 8 frames
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Duration : 44mn 8s
Bit rate mode : Variable
Bit rate : 738 Kbps
Maximum bit rate : 4 469 Kbps
Width : 720 pixels
Height : 576 pixels
Display aspect ratio : 16:9
Frame rate mode : Constant
Frame rate : 23.976 fps
Standard : PAL
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.074
Stream size : 233 MiB (85%)
Title : Imported with GPAC 0.4.6-DEV (internal rev. 8)
Encoded date : UTC 2011-01-15 09:57:52
Tagged date : UTC 2011-01-15 10:15:58

Audio
ID : 1
Format : AAC
Format/Info : Advanced Audio Codec
Format version : Version 4
Format profile : LC
Codec ID : 67
Duration : 44mn 15s
Bit rate mode : Variable
Bit rate : 132.3 Kbps
Maximum bit rate : 186 Kbps
Channel(s) : 2 channels
Channel positions : Front: L R
Sampling rate : 44.1 KHz
Compression mode : Lossy
Stream size : 41.4 MiB (15%)
Title : Imported with GPAC 0.4.6-DEV (internal rev. 8)
Encoded date : UTC 2011-01-15 09:57:49
Tagged date : UTC 2011-01-15 10:15:58
The MediaInfo doesn't say anything about a delay, the audio is still out of sync. And I really want to figure out a simple way to re-encode the audio without touching the video.


Also, Somehow according to MediaInfo, the video looses about 7 seconds... I don't know what that is about.

Selur
16th January 2011, 16:25
you could try something like:
MP4Box -fps 23.976 -add "input.264"#video -brand avc1 -add "audio.ac3"#audio:delay=83 -new "output.mp4"
also iirc MP4Box only supports positive delays so, if the audio delay is negative you have to delay the video
MP4Box -fps 23.976 -add "input.264"#video:delay=80 -brand avc1 -add "audio.ac3"#audio -new "output.mp4"
(so you might want to add some sort of if/else to your script)

Cu Selur

Ps.: There are also a bunch of GUIs out there that should be able to do the job,.. i.e. AviDemux and Hybrid,...

LexManos
17th January 2011, 09:12
I've tried the GUIs for this, most notibly AviDemux
It converts the audio, and creates the mp4, but the video is not there, in any of my media players. Only in AviDemux itself.

Lighto
17th January 2011, 09:38
Try re-encoding the audio with the delay set, then mux the streams together.

LexManos
20th January 2011, 23:52
Know a good command line audio converter that can do mp3->aac with added delay?
I've also tried the full command lines you provided, the audio is STILL out of sync.


Anyways AviDemux works fine if I set the container to AVI. Video shows up and audio is synced.
But sadly the 360 only support h264 in mp4 containers.

Any other suggestions?

montython
21st January 2011, 00:09
LexManos, which audio encoder is this?


start /wait audioconverter c:\tmp\aud_audio.mp3 c:\tmp\aud.aac -c aac


I think Nero's command-line aac encoder can do what you want. I will check and update my post.

LexManos
21st January 2011, 00:33
http://www.coolutils.com/totalaudioconverter

With AviDemux {to MP4 container}, the video is in the file.. MediaInfo sees it.. but none of my media players aside from AviDemux see it...
VLC, MPC, Win Media Player, Winamp, RealPlayer, QuickTime, or the 360.

montython
21st January 2011, 00:39
OK LexManos. Unfortunately Nero aac encoder (http://www.nero.com/eng/downloads-nerodigital-nero-aac-codec.php) doesn't have the delay option. I guess you know about AviSynth. What about trying that? You can adjust the delay in your avs script and then feed the decoded audio to Nero encoder (or whatever aac encoder you prefer).

http://avisynth.org/mediawiki/FAQ_different_types_content#The_video_and_audio_in_my_final_encoding_is_out_of_sync.2C_what_should_I_do.3F

LexManos
21st January 2011, 01:13
No I'm not familiar with AviSynth, though from what i've been looking at right now its a pretty coll tool.
Just havent figured out how to extract the modified audio to a file to send to compression yet. So ya still stuck >.<


Edit:
Alright so I found a tool called wavi that extracts the audio from a avi file {in particular avisynth created avis}
So I got passed that part, but STILL the audio/video are out of sync.. though, i think its a framerate issue or something, because at the beginning of the vid the desync of rather small and twards the end its like 2 seconds.

montython
21st January 2011, 16:20
I am assuming that you have AviSynth 2.58 (http://sourceforge.net/projects/avisynth2/files/AviSynth%202.5/AviSynth%202.5.8/Avisynth_258.exe/download) and a directshow h.264 decoder already installed. Otherwise, you cannot load your video to AviSynth.

I will refer to your original media file (h.264 video and mp3 audio tracks in avi container) as media.avi.

This is what I suggest:

1) Load video and audio to AviSynth as separate tracks.

media.avs:

V=DirectShowSource("media.avi", fps=23.976, audio=false, pixel_type="YV12")
A=DirectShowSource("media.avi", fps=23.976, video=false, pixel_type="YV12")
AudioDub(V, A)
DelayAudio(0.083)


Play around with the DelayAudio option (negative numbers for playing the audio earlier, and positive numbers for delaying the audio) until video and audio are in sync. You can use Media Player Classic (http://sourceforge.net/projects/guliverkli/files/Media%20Player%20Classic/MPC%206.4.9.0/mpc2kxp6490.zip/download) to play back media.avs and test video/audio sync each time you change DelayAudio setting.

2) When you are done with step 1, leave the audio track alone in the avs script with the appropriate DelayAudio setting and save it to a wave file using wavi (http://sourceforge.net/projects/wavi-avi2wav/files/wavi/1.06/wavi106.zip/download). (Some encoders accept avs input. I thought neroaacenc.exe would also accept it, but apparently it doesn't.)

audio.avs:

DirectShowSource("media.avi", fps=23.976, video=false, pixel_type="YV12")
DelayAudio(0.083)



wavi audio.avs audio.wav


3) Use Nero AAC encoder (http://www.nero.com/eng/downloads-nerodigital-nero-aac-codec.php) for encoding to AAC.


neroAacEnc -if audio.wav -of audio.mp4


4) Finally, mux the h.264 video stream from media.avi and the audio stream from audio.mp4 using MP4Box.


I hope the audio has a constant delay and this helps.

chrisgwd
21st January 2011, 17:12
Guys,

I've been having the same problem using StaxRip. What I have found is that recent versions of x264 are doing something that MP4Box doesn't like. x264 notes a number of encoded frames, but when MP4Box imports them, it drops a few...about equal to the delay I see. I got things to work by reverting back to x264 r1542, just another version I had access to. I am in a rush at the moment...will post some data later.

Update - check out this thread over on doom10...
http://doom10.org/index.php?topic=932.0

Chris

djenyc
4th April 2011, 21:36
3) Use Nero AAC encoder (http://www.nero.com/eng/downloads-nerodigital-nero-aac-codec.php) for encoding to AAC.


neroAacEnc -if audio.wav -of audio.mp4


4) Finally, mux the h.264 video stream from media.avi and the audio stream from audio.mp4 using MP4Box.


I hope the audio has a constant delay and this helps.

Encoding from wav to AAC with Nero AAC adds it's own delay of about 100ms - I've observed it myself and there is a discussion on it in this thread:
http://www.hydrogenaudio.org/forums/lofiversion/index.php/t69803.html

This delay is put in to Chapters tag, e.g.:
Chapters:
Chapter #1 - 00:00:00.097 - ""

This does not seem to be a standard format for setting audio delay, so I'm wondering if a delay need to be added manually in to MP4Box at the muxing stage to keep the audio in sync. I'm not an expert on this, so wanted to hear an outside opinion.

Thanks

ganymede
5th April 2011, 00:05
This delay is put in to Chapters tagI usually get good results with MP4Box (audio and video in sync) by muxing only audio, not chapters info generated by neroAacEnc :MP4Box -add video.m4v -add audio.m4a#audio -mpeg4 -new file.mp4

space1999
5th April 2011, 00:46
Encoding from wav to AAC with Nero AAC adds it's own delay of about 100ms - I've observed it myself and there is a discussion on it in this thread:
http://www.hydrogenaudio.org/forums/lofiversion/index.php/t69803.html

Also here:

http://forum.doom9.org/showthread.php?t=144346

and here:

http://webcache.googleusercontent.com/search?q=cache:18Nfynew0TkJ:https://roundup.libav.org/issue1401+neroaacenc+delay&cd=13&hl=en&ct=clnk&source=www.google.com

This delay is put in to Chapters tag, e.g.:
Chapters:
Chapter #1 - 00:00:00.097 - ""


I think the MP4 chapter tag just indicates there is a delay, and "how big" it is.

EDIT

So obvious, that I could not see it :o eac3to is able to minimize the audio delay for AAC streams as well:

eac3to input.aac output.aac -97ms

BTW, NeroAacEnc seems to be the worst AAC compressor at introducing audio delays --- faac 1.28 sucks less, and enc_aacplus is the winner. :cool:

djenyc
6th April 2011, 08:14
Also here:

http://forum.doom9.org/showthread.php?t=144346

and here:

http://webcache.googleusercontent.com/search?q=cache:18Nfynew0TkJ:https://roundup.libav.org/issue1401+neroaacenc+delay&cd=13&hl=en&ct=clnk&source=www.google.com



I think the MP4 chapter tag just indicates there is a delay, and "how big" it is.

EDIT

So obvious, that I could not see it :o eac3to is able to minimize the audio delay for AAC streams as well:

eac3to input.aac output.aac -97ms

BTW, NeroAacEnc seems to be the worst AAC compressor at introducing audio delays --- faac 1.28 sucks less, and enc_aacplus is the winner. :cool:

Thanks for the tip! I ended up trimming wav files that go in to NeroAacEnc. I have 22kHz audio tracks that Nero always pads with 119ms - so as long as I take that out of the wav files everything should be in-sync.

Checked playback of my muxed mp4 video with several players - following player played audio/video in sync: VLC, Mplayer, Kmplayer with Haali splitter, AviSynth with FFVideoSource/FFAudioSource.

However, in the following players the audio is playing about 120 ms before the video: KMplayer with internal splitter, QuickTime, AviSynth with DirectShowSource using FFDShow and Haali splitter. May be there is still some info from NeroEncoder about the delay that they are reading, but I'm pretty sure that chapters tag was stripped when I mux aac and x264 in to an output mp4 - I could see Chapters tag with mp4box -info in mp4 from Nero, but it's gone when I check info for the muxed mp4 (h264/aac), so the only thing I can think of is that there is some delay in the elementary aac stream, is that even possible?

Really puzzled by this... one thing I noticed was discrepancy in the frame count between AVISynth using FFVideoSource/FFAudioSourceand and AviSynth using DirectShowSource. AVISynth with FFVideoSource/FFAudioSourceand is reporting the correct frame count of 222 frames , and AVISynth with DirectShowSource is reporting four extra frames in the video (226 frames total) - not sure if that has something to do with the audio sync problem (the video I'm testing this on is at 30fps, so that would be 130ms).

Any ideas?