Why does it happen?
The main reason this occurs is that the Video Capture clock is
not in sync with the Audio capture clock thus you get video frames
faster/slower than you should or Audio samples faster/slower than
you should. This leads to the two streams (audio and Video) being
out of sync by a certain amount, this is usually a very small
percentage but over time (60 to 90 min) can cause 2 or 3 seconds
de-synchronization.
The best way to deal with this is slow down or speed up one of
the streams, the video stream is the easiest as you have two ways
of doing this.
- Add or drop frames to make the stream longer or shorter effectively
speeding up or slowing down the stream. (VirtualDub uses this
method)
- Adjust the frame rate to speed up or slow down the speed of
the stream.
The first way is not as easy as it sounds, you need to work out
what the audio stream is doing, how fast or slow it is etc. and
then drop that many frames etc. The second way is a little easier
and this is how the master stream option works in most DirectShow
video capture applications, the frame rate after the capture is
adjusted to match the duration of the audio, because of the structure
of the AVI file format (fixed frame rate) you can adjust the Video
frame rate quite easily by changing the AVI header.
The big problem you face if you just alter the frame rate is
that you can no longer use the video/audio stream to create an
SVCD or and VCD as VCD requires a set frame rate of XXX where
XXX is 25 for PAL etc. This means you need to use the Dropped
frame method to sync the streams if you want to use the capture
to create SVCD's
There is another solution, instead of altering the Video stream
alter the Audio stream, just insert or remove audio samples to
keep the audio stream at a constant sample/sec rate that is consistent
with the Video. This is the approach I intend to use with Virtual
VCR, I have created a Audio Resample DirectShow filter to speed
up or slow down the audio stream when capturing, this will allow
synching of the audio/video streams with no frames being dropped.
How do I use it?
Just enable the Resample Audio - Dynamically option, the
audio should then be resampled to match the video stream.
Resample Audio- How it works
Rate
Matching:
The actual video and audio rates are measured, this is done by
monitoring the video and audio streams and then working out the
data flow based on what the actual stream are doing.
From this calculation the actual FPS of the video and sample
rate of the audio is calculated. This data is shown in Capture
Stats Dialog at location (1)
and (2).
Next the Delta as a ratio is calculated, this is basically the
difference between the audio and video streams, it is the ratio
the audio needs to be altered by for the streams to match exactly.
This data is shown in (3).
Finally the actual percentage change of the audio is calculated
(it is just 100 + Delta*100) this is the actual amount the audio
is currently being re-sampled to or the Target percentage
and is shown at (4). This
value is sent to the audio resample filter every second and the
audio is re-sampled in real time to this value.
What the Audio Resample filter then does is try to resample the
audio to the Target percentage, what the Audio Resample filter
does to the audio is shown in the Actual Percentage at (5)
this is the actual amount the audio has been altered over the
whole capture.
In theory this should keep the Audio and Video streams in Sync,
I have tested this with 3 hour captures and have have good results
but it is best to test this yourself and see if it works for you.
SyncUsingStreamOffset
This option will take into consideration any latency issues your
capture devices have, if for some reason your audio card is delaying
the audio stream by 2 seconds to fill up its internal buffers
then the FilterGraph will delay the Video by the same amount etc.
If you notice AV sync right from the beginning of the capture
you can try this option to see if it helps, this option will only
help initial delay problems not progressive AV sync problems.
Delay Audio By
I have not had any success with the SyncUsingStreamOffset
option so I have added the Delay Audio by option as well, this
option allows you to delay the audio by a certain amount thus
allowing you manual control over the stream offset.
Master Stream
The Master Stream sets the master stream that other streams must
synchronize to after the file is saved to disk. This means that
if you select audio as the master stream then the video FPS settings
in the AVI header will be set (AFTER the capture) to match the
audio length. This may alter the FPS of the captured file, if
you need exactly 25 FPS for creation of SVCD etc then this option
may not work well for you. The master stream option is also not
available when you are using the resample audio option.
|