[MPlayer-dev-eng] solution of av desync on capturing with mencoder

Jarek Czekalski jarekczek at poczta.onet.pl
Sun Jun 12 09:57:08 CEST 2011


After years of using mencoder to capture and encode videos I found a way 
to achieve a/v synchronization which is better than the current mencoder 
solution. Instead of adjusting video I suggest adjusting audio. I have 2 
patches to mencoder, which I'll post if I get a positive receive.

PROBLEM DESCRIPTION

Capturing from v4l2 and alsa. The sound card has a different timer than 
the video frames timer which results in a skew between audio and video. 
Mencoder currently drops or duplicates frames when it detects the time 
difference. This solution synchronizes audio with video but has the 
following drawbacks:

1. Duplicated or dropped frames produce jerky videos, especially on 
interlaced source.
2. Sound card can produce strange fluctuation which forces mencoder to 
duplicate and insert frames in a short period which spoils the video 
quality even more. If a 1 hour video should have 10 duplicated frames, 
instead it has 60 duplicated and 50 inserted frames.

ALTERNATIVE SOLUTION

Adjusting sound to video seems more reasonable to me. So we need to 
capture with -noskip and scale the audio to match the video duration. 
What do we need?

1. Calculation of the audiorate
2. Correction of the initial skew between audio and video. If I turn on 
-noskip, then I have initial skew between audio and video of about 0.1s. 
With skipping allowed mencoder would fix this.

The user makes the audio adjustment in his script using sox or the 
native scaletempo filter.

Patch for 1.
Report audiorate in status line. I suggest introducing -reportaudiorate 
switch. The status line could then look like this:
Pos:3569.4s  89234f ( 0%) 25.00fps Trem:   0min   0mb  tc:-0.513 
(r:1.000144)

r is the audiorate that should be passed to an audio scaler.

After finished encoding a following line is also printed:

duplicated: 2, bad: 0, skipped: 0, v_timer_corr: -0.523, audio_scale: 
1.0001453

The audio_scale here is the final audio rate.

Patch for 2.
The switch -noskipafter would allow mencoder to skip frames during the 
first 2 seconds of encoding. After 2 seconds it behaves as if -noskip 
was specified.

ADVANTAGES OF A NEW SOLUTION

Clean video without any corrections. No noticeable sound quality loss, 
at least using sox. No noticeable a/v desync on my sound card.


Is there a chance for commiting these 2?



More information about the MPlayer-dev-eng mailing list