PDA

View Full Version : mencoder, x264, vorbis, matroska - bad a/v sync problems


matt_garman
9th February 2007, 04:29
Hello!

I using a procedure similar to the one described here (http://www.smorgasbord.net/howto_rip_dvds_in_mpeg_4_avc_x264_multi_audio_subtitles_matroska) to rip DVDs (the ones I own of course) to my hard drive.

I'm having pretty bad A/V sync problems, particularly with TV episodes. So far I've experimented with the H264+Vorbis in the Matroska container with Batman Begins (the movie) and episodes of House, MD season 2.

With Batman Begins, the A/V was almost perfectly in sync, possibly so little that I can live with it.

House on the other hand is awful---the A/V sync is unbearable.

Based on the research I've done, I've been able to find the following suggestions, all of which I've tried, but have not helped:

Use "harddup" as the last argument to my -vf filter chain
Use -mo 0 in my mencoder commands
Use -noskip in my mencoder commands
Use -oac copy when encoding the video


Of course, if I play these episodes directly from the DVD with mplayer, the audio and video is perfectly sync'ed.

I know there's ways to manually tweak the sync while muxing, but I'd really like this fully automated, as I intend to batch process all my DVDs with a script.

I would greatly appreciate any suggestions or ideas.

Thanks!
Matt

jruggle
9th February 2007, 05:42
Hello!

I using a procedure similar to the one described here (http://www.smorgasbord.net/howto_rip_dvds_in_mpeg_4_avc_x264_multi_audio_subtitles_matroska) to rip DVDs (the ones I own of course) to my hard drive.

I'm having pretty bad A/V sync problems, particularly with TV episodes. So far I've experimented with the H264+Vorbis in the Matroska container with Batman Begins (the movie) and episodes of House, MD season 2.

With Batman Begins, the A/V was almost perfectly in sync, possibly so little that I can live with it.

House on the other hand is awful---the A/V sync is unbearable.

Based on the research I've done, I've been able to find the following suggestions, all of which I've tried, but have not helped:

Use "harddup" as the last argument to my -vf filter chain
Use -mo 0 in my mencoder commands
Use -noskip in my mencoder commands
Use -oac copy when encoding the video


Of course, if I play these episodes directly from the DVD with mplayer, the audio and video is perfectly sync'ed.

I know there's ways to manually tweak the sync while muxing, but I'd really like this fully automated, as I intend to batch process all my DVDs with a script.

I would greatly appreciate any suggestions or ideas.

Thanks!
Matt

doing something like the following works for me:

don't use softskip, pullup, or noskip. just use -ofps with whatever output frame rate you want (e.g. 24000/1001). you can use harddup if you're increasing the fps, but I would recommend leaving it out and going with an equal or lower fps. also, do a dummy audio transcode along with the video. using "-oac lavc -lavcopts acodec=mp2:abitrate=224" should be fast enough not to slow things down too badly. output to -of avi instead of rawvideo then just leave out the mp2 audio when you remux everything into mkv.

hope this helps.

-Justin

matt_garman
9th February 2007, 06:08
doing something like the following works for me:

don't use softskip, pullup, or noskip. just use -ofps with whatever output frame rate you want (e.g. 24000/1001). you can use harddup if you're increasing the fps, but I would recommend leaving it out and going with an equal or lower fps. also, do a dummy audio transcode along with the video. using "-oac lavc -lavcopts acodec=mp2:abitrate=224" should be fast enough not to slow things down too badly. output to -of avi instead of rawvideo then just leave out the mp2 audio when you remux everything into mkv.

hope this helps.

-Justin

I'll definately try this, but I have a couple questions:

I believe the video source is interlaced, so don't I need to use softskip,pullup
The frame rate jumps many times during playback/encoding. See this page ("http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-dvd-mpeg4.html) under "Identifying source material"
Why should I output to AVI instead of rawvideo? I actually started encoding into the AVI container but moved to Matroska for its various advantages (mostly because my rips are high bitrate, resulting in files bigger than 2 gb
Is the dummy audio transcode supposed to "fool" mencoder into getting the AV sync correct?


Thank you for the suggestions... I'm slowly trying to learn the ins-and-outs of all this. :)

Thanks again,
Matt

jruggle
10th February 2007, 06:13
I'll definately try this, but I have a couple questions:

I believe the video source is interlaced, so don't I need to use softskip,pullup
The frame rate jumps many times during playback/encoding. See this page ("http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-dvd-mpeg4.html) under "Identifying source material"
Why should I output to AVI instead of rawvideo? I actually started encoding into the AVI container but moved to Matroska for its various advantages (mostly because my rips are high bitrate, resulting in files bigger than 2 gb
Is the dummy audio transcode supposed to "fool" mencoder into getting the AV sync correct?


Thank you for the suggestions... I'm slowly trying to learn the ins-and-outs of all this. :)

Thanks again,
Matt

I've never tried transcoding interlaced source material, so I re-read the mplayer man page...you can use pullup and softskip.

The frame rate changing throughout the stream is one thing that could be causing your problems. Those frame rate changes don't make it into the raw video. Setting a fixed output frame rate using -ofps will skip any necessary frames...if you encode it along with audio.

As far as avi, it's arbitrary. You need to remux into a container since you'll have that dummy audio stream. mpeg would work too, but avi is less complex. You should be able to feed the video only from your avi into the matroska muxer...then discard it just like you would the raw video.

edit:
If you're worried about the 2GB file size limit, you'll probably have to do more complex things...

-Justin

matt_garman
12th February 2007, 01:29
I've never tried transcoding interlaced source material, so I re-read the mplayer man page...you can use pullup and softskip.

The frame rate changing throughout the stream is one thing that could be causing your problems. Those frame rate changes don't make it into the raw video. Setting a fixed output frame rate using -ofps will skip any necessary frames...if you encode it along with audio.

As far as avi, it's arbitrary. You need to remux into a container since you'll have that dummy audio stream. mpeg would work too, but avi is less complex. You should be able to feed the video only from your avi into the matroska muxer...then discard it just like you would the raw video.

edit:
If you're worried about the 2GB file size limit, you'll probably have to do more complex things...

-Justin

Well, that fixed my audio sync problems... but the video got scaled! It definately happend at the mencoder stage, though I'm not sure why/how. I'm not using any -vf scale or cropping options (i.e. I want to leave the video in its native resolution). The only thing that's really different is using -of avi and also doing the "throwaway" audio encoding.

So close!

Thanks again,
Matt

nm
12th February 2007, 09:43
Are you sure it isn't just an aspect ratio change? What does your current command line look like?

matt_garman
12th February 2007, 14:45
Are you sure it isn't just an aspect ratio change? What does your current command line look like?

I double-checked my command line, and I had a typo. :rolleyes:

I accidentally specified lavc as the video codec. I wondered why, in addition to what I posted above, the quality looked so bad and the encoding went so fast.

I'll try again and report back!

Matt

matt_garman
12th February 2007, 23:19
If you're worried about the 2GB file size limit, you'll probably have to do more complex things...

Just for clarification: I don't care how big the resulting files are (mkv or otherwise). I was more concerned with 2GB avi files, which, if I understand correctly, are kind of an abomination. Mplayer plays them okay (at least on my system), but I was worried about their "portability".

As for which is more portable, 2+ GB AVI files or Matroska files w/h.264 and Vorbis audio... well... the latter at least wins in correctness, (IMO) coolness, and portability---at least in the theoretical sense. :) Based on my (admittedly limited) research, h264 + vorbis are the two best codecs when quality and filesize are the two biggest factors.

Thanks again for all the assistance,
Matt

matt_garman
13th February 2007, 15:02
Still no luck :(

I had high hopes too: I've been running the encodes remotely, from work, and test them at night when I get home. Yesterday, I used midentify to check the length of the Ogg-Vorbis and H264 files: they were only different by about 0.10 seconds.

However, I got home and started watching. I was getting really excited, because for the first 10 minutes or so, everything was perfectly sync'ed. Then, almost totally abruptly, the AV got out of sync by about 0.5--1.0 sec (my guess).

Any more ideas? Here's the command I used:


mencoder dvd://1 -v -alang en -aid 128
-oac lavc -lavcopts acodec=mp2:abitrate=224
-ovc x264 -x264encopts
bitrate=2000:partitions=all:8x8dct:me=umh:subq=6:frameref=5:threads=auto:pass=2:bframes=4:b_pyramid:weight_b
-idx -ofps 24000/1001 -of avi
-vf pullup,softskip,pp=ac,hqdn3d=2:1:2,harddup
-o file.avi


That is, of course, the 2nd pass; first pass had pass=1:turbo=1 in the x264encopts, and output sent to /dev/null.

Regards,
Matt

jruggle
14th February 2007, 06:47
Still no luck :(

I had high hopes too: I've been running the encodes remotely, from work, and test them at night when I get home. Yesterday, I used midentify to check the length of the Ogg-Vorbis and H264 files: they were only different by about 0.10 seconds.

However, I got home and started watching. I was getting really excited, because for the first 10 minutes or so, everything was perfectly sync'ed. Then, almost totally abruptly, the AV got out of sync by about 0.5--1.0 sec (my guess).

Any more ideas? Here's the command I used:


mencoder dvd://1 -v -alang en -aid 128
-oac lavc -lavcopts acodec=mp2:abitrate=224
-ovc x264 -x264encopts
bitrate=2000:partitions=all:8x8dct:me=umh:subq=6:frameref=5:threads=auto:pass=2:bframes=4:b_pyramid:weight_b
-idx -ofps 24000/1001 -of avi
-vf pullup,softskip,pp=ac,hqdn3d=2:1:2,harddup
-o file.avi


That is, of course, the 2nd pass; first pass had pass=1:turbo=1 in the x264encopts, and output sent to /dev/null.

Regards,
Matt

Hmm... Try taking out the harddup? Also, I've never used -idx. If the avi is only temporary, I don't see the need to create an index. Besides that, your commandline looks a lot like mine does when I extract DVD's. You might try out the avi to see if it also has sync problems. If not, then it's probably how matroska is reading that avi. If so, then maybe it has to do with the deinterlacing?

Thasp
28th February 2007, 03:18
I am having a similar issue with mencoder encoding HDTV to h.264. It's driving me insane.

It is probably stupid that you try what hasn't worked for me, but try it anyway, read the last post of my thread http://forum.doom9.org/showthread.php?t=122082&page=2 and if one of those things works for you, all the better.

I'm close to thinking there is no solution to this problem. I've googled, experimented, and ran test encodes for hours to no avail.

The worst part is, as you said, it'll appear halfway through the episode. A ten minute clip is useless. It takes time to encode x264, even with settings that make the video look like crap it's realtime on the linux machine I am using. The desktop is faster, but the desktop uses megui and thus doesn't have all of the trivial issues I associate with using linux.

jruggle
28th February 2007, 04:17
I am having a similar issue with mencoder encoding HDTV to h.264. It's driving me insane.

It is probably stupid that you try what hasn't worked for me, but try it anyway, read the last post of my thread http://forum.doom9.org/showthread.php?t=122082&page=2 and if one of those things works for you, all the better.

I'm close to thinking there is no solution to this problem. I've googled, experimented, and ran test encodes for hours to no avail.

The worst part is, as you said, it'll appear halfway through the episode. A ten minute clip is useless. It takes time to encode x264, even with settings that make the video look like crap it's realtime on the linux machine I am using. The desktop is faster, but the desktop uses megui and thus doesn't have all of the trivial issues I associate with using linux.

This may not help, but I'll tell you how I would approach it on my linux machine. I would first try encoding to MPEG2 at vqscale=2. This is much faster than x264. If you can get the sync right with MPEG2, you could re-encode to x264 using mencoder with -noskip or FFmpeg.

Thasp
28th February 2007, 06:09
Thanks a lot for the advice. If that works, I owe you a cookie.

edit: no sync in mpeg2.

jruggle
2nd March 2007, 15:04
Thanks a lot for the advice. If that works, I owe you a cookie.

edit: no sync in mpeg2.

oh well...no cookie. :( but at least you know the problem is not related to x264. looks like it's a general mencoder issue. you might try one of the mplayer mailing lists for help.

matt_garman
2nd March 2007, 15:21
oh well...no cookie. :( but at least you know the problem is not related to x264. looks like it's a general mencoder issue. you might try one of the mplayer mailing lists for help.

I've pursued this a bit on the mplayer-users mailing list in this thread (http://lists.mplayerhq.hu/pipermail/mplayer-users/2007-February/065674.html) and this thread (http://lists.mplayerhq.hu/pipermail/mplayer-users/2007-February/065422.html).

I'm still stuck. My latest finding is that some of my source material (i.e. the DVD) seems to have A/V sync problems. Someone on the mailing list suggested that, actually, it's probably a problem with mplayer's synchronizing functionality.

I'm taking a break from this for now... kind of waiting for someone else to have a breakthrough :)

Matt

nm
2nd March 2007, 23:20
The sync problem you're having with AVC in AVI output is due to the hacks needed to store the stream (and especially b-frames). See the discussion on why VFW is bad for H.264 in the MPEG-4 AVC forum if you are interested in details. The sync difference is constant within a file, so if you find out how much it is, it can be compensated when remuxing the stream to a MP4 or Matroska file. Avi2raw or the new mkvmerge might do this automatically, but I haven't tried them.