Audio and video not sync

Discuss about generic usage of MediaCoder.

Moderator: HuggiL

Post Reply
triacc
Beginner
Beginner
Posts: 4
Joined: Mon Jul 31, 2006 2:54 am
Location: Fin

Audio and video not sync

Post by triacc » Mon Jul 31, 2006 2:56 am

When I use h.264 video and nero aac target quality, then audio is not sync with video. Container I use is .mkv or .mp4. How to fix this?

everling
Amateur
Amateur
Posts: 48
Joined: Sat Jul 22, 2006 9:39 pm

Post by everling » Mon Sep 11, 2006 3:54 pm

Simple solution, do not use -nosound when encoding the video. The encoded video stream will get variable audio desync if there is no audio stream during the video encoding process.

Lengthy explanation as follows:
14.1.11. Audio wrote: Do not use -nosound when encoding a file with audio, even if you will be encoding and muxing audio separately later. Though it may work in ideal cases, using -nosound is likely to hide some problems in your encoding command line setting. In other words, having a soundtrack during your encode assures you that, provided you do not see messages such as “Too many audio packets in the buffer”, you will be able to get proper sync.

You need to have MEncoder process the sound. You can for example copy the orignal soundtrack during the encode with -oac copy or convert it to a "light" 4 kHz mono WAV PCM with -oac pcm -channels 1 -srate 4000. Otherwise, in some cases, it will generate a video file that will not sync with the audio. Such cases are when the number of video frames in the source file does not match up to the total length of audio frames or whenever there are discontinuities/splices where there are missing or extra audio frames. The correct way to handle this kind of problem is to insert silence or cut audio at these points. However MPlayer cannot do that, so if you demux the AC3 audio and encode it with a separate app (or dump it to PCM with MPlayer), the splices will be left incorrect and the only way to correct them is to drop/dup video frames at the splice. As long as MEncoder sees the audio when it is encoding the video, it can do this dropping/duping (which is usually OK since it takes place at full black/scenechange, but if MEncoder cannot see the audio, it will just process all frames as-is and they will not fit the final audio stream when you for example merge your audio and video track into a Matroska file.

stanley
Site Admin
Site Admin
Posts: 4135
Joined: Mon May 15, 2006 7:43 pm
Location: Sydney

Re: Audio and video not sync

Post by stanley » Mon Sep 11, 2006 4:06 pm

triacc wrote:When I use h.264 video and nero aac target quality, then audio is not sync with video. Container I use is .mkv or .mp4. How to fix this?
Have u tried specifying fps.
When things work together, things work.

everling
Amateur
Amateur
Posts: 48
Joined: Sat Jul 22, 2006 9:39 pm

Post by everling » Mon Sep 11, 2006 4:42 pm

Stanley, specifying the fps helps a bit, but there will still be audio desync. You need the audio data to get it right for lengthy videos.

Edit: The audio data is only needed on the last pass when the video data is to be written.

stanley
Site Admin
Site Admin
Posts: 4135
Joined: Mon May 15, 2006 7:43 pm
Location: Sydney

Post by stanley » Mon Sep 11, 2006 4:55 pm

Did u see audio being encoded in a seperate pass?
When things work together, things work.

everling
Amateur
Amateur
Posts: 48
Joined: Sat Jul 22, 2006 9:39 pm

Post by everling » Mon Sep 11, 2006 5:17 pm

I can't relate directly because I've been using the commandlines (mencoder, oggenc, mkvmerge) lately, instead of MediaCoder. But what I do (to create a x264 + Vorbis MKV) is:
  1. Use mencoder to extract the audio stream as .wav.
  2. Encode the .wav file with oggenc.
  3. Calculate the video bitrate needed by hand: (target size - audio size) / time <- incomplete formula
  4. Modify the video encoding commandline with the bitrate and replace the '-nosound' parameter with '-oac pcm -channels 1 -srate 4000'.
  5. Encode with the modified commandline.
  6. Use mkvmerge to create the MKV from the .ogg and .avi (throw away the dummy pcm audio in the AVI).

Post Reply