View Full Version : Problems with Mpasource & Delayaudio
kevinm
8th August 2003, 00:44
Hi,
I am a new user of avisynth, and have spent the past couple of months bashing my head against a wall trying to resolve audio sync problems with NTSC to PAL conversion. I have read a great many articles about possible causes but have decided to try rule out the NTSC to PAL conversion itself as the cause. I put together a simple flow:
(all the following steps were done using TMPGenc frameserved by avisynth)
Step 1, Using MPEG Tools/Demultiplex, extract the video and audio streams (.m2v & .mp2)
Step 2, In TMPGEnc load 'merge.avs' (see below) ..
Observation: TMPGEng is busy for a few minutes then returns, BUT the audio file field in the TMPGenc window is blanked out !
(I understand that it is normal for TMPGenc/avisynth to take a while to load the avs script as MPASOURCE has to parse the whole file to determine the number of frames)
But why is the audio file field blanked out?
Next, using the trim command I trimmed the video so that I only read in about the first five minutes, now 'merge.avs' works correctly, i.e. both audio and video fields are populated. But if I increase the size of the video clip using trim I reach a point where the audio field gets blanked out again. Has anyone seen this problem before?
Another experiment which I did was to output the audio from TMPGenc as a WAV file (this is an option under /TOOLS) , then I modified my avs script to read in the WAV instead of the MP2 (see merge2.avs). Now it doesn't matter what length the video is I always get the video and audio fields in TMPGenc populated. For some reason the problem appears to be caused by reading the MP2 audio. Is this is known issue with MPASOURCE?
However, regardless of whether I read MP2 or WAV into avisynth my audio is always out of sync. At the start of the 1hr video the audio is out by 0.4 seconds, at the end it is out by 1.4 seconds. The audio is early with respect to the video. I have been experimenting with Delayaudio to fix this but it is tough as the delay is not constant. Maybe I have to use trim to break the video into a number of segments and apply a different delayaudio value to each segment and stitch them back together.
(I have also had problems using Delay audio, I found that as I am using +ve delay numbers that I have to trim the start of the clip by the amount of frames I am delaying the audio by otherwise I get 'out of memory' errors in TMPGenc. This is probably worthy of a separate post on this topic.)
Another thing I have noticed is when I read the original NTSC video into TMPGenc it says the framerate is 29.97, BUT I know that it is actually 30 fps. I determined this because of the fact that my test video is a recording of a digital timer, and I can see the time in the clip and I know how many frames I have to trim in order to skip by 5 minute chunks. I guess that TMPGEnc must be determining the framerate by looking at the header, is this possible? Is it likely to cause a problem if the header says it is 29.97 but in reality is 30fps?
BTW I am using avisynth 2.52 and TMPGenc 2.520
merge.avs
========
PluginPath = "c:\AVISynth25\Plugins\"
LoadPlugin(PluginPath + "MPEGDecoder.dll")
LoadPlugin(PluginPath + "mpasource.dll")
v=MPEGSource("D:\audio_test_8000_ntsc.m2v")
a=MPASource("D:\audio_test_8000_ntsc.mp2") <<< using MP2 won't load if the clip is longer than 5 minutes!
video=audiodub(v,a)
return video
merge2.avs
========
PluginPath = "c:\AVISynth25\Plugins\"
LoadPlugin(PluginPath + "MPEGDecoder.dll")
LoadPlugin(PluginPath + "mpasource.dll")
v=MPEGSource("D:\audio_test_8000_ntsc.m2v")
a=WAVSource("D:\audio_test_8000_ntsc.wav") <<< Using WAV always loads ok whatever the clip length.
video=audiodub(v,a)
return video
thanks,
Kevin
Wilbert
8th August 2003, 21:42
If that would be the case, I come to 3.6 seconds instead of 1.4. What is the exact duration of the video and audio files?
kevinm
8th August 2003, 23:51
Wilbert,
thanks for your reply, I am sorry but I can't read my own notes, I was mixing up the numbers between two testcases. I am sorry to have caused any confusion.
I have just checked and here are the correct file statistics:
Original MPG (audio&video)
==========================
41mins, 30 secs long (according to TMPGenc preview)
Framecount = 74,630 (according to TMPGenc preview)
Video only stream (.m2v) (extracted using TMPGenc demux)
========================================================
41mins, 30 secs long (according to TMPGenc preview)
Framecount = 74,633 (according to TMPGenc preview)
Audio stream, MP2
=================
41:30.000 long (according to Goldwave)
Audio stream, WAV
=================
41:30.158 long (according to Goldwave)
(MP2 was obtained using Demultiplex in TMPGenc. WAV was obtained using TMPGEnc, output file as WAV option.
One intesting thing I just noticed is that by using the Subtitle command and .framecount I managed to output the original framecount as text in the final video.
Avisynth thinks the combined audio/video is 74,695 frames long !!
Kevin
kevinm
9th August 2003, 00:32
Wilbert,
I don’t want to add confusion to this thread but I thought I would post some results I got last night using IanB's findAudioSyncScript.
First an explanation of how I made my test video:
I took a digital timer (of the type you use to turn lights on/off around the house). I built a simple circuit consisting of a LED and a piezo buzzer and circuit to limit its on duration to about 1/2 second. I programmed the timer to turn on for one minute, every five minutes. This results in a video where I can see the time on the digital timer, see the LED turn on and hear the buzzer.
Using the findAudioSyncScript I can work out what value I have to delay the audio by to sync up with the video. I do this for each five minute event which occurs through the 41 minutes 30 sec video. Here are the results ..
(Note that the first 'event' occurs when the digital timer reads 12:00, this is 1 minutes, 28 seconds into the video, frame 2647 according to TMPGenc preview)
1st event, clock reads 12:00, sync requires audioDelay setting of -0.35 sec
2nd event, clock reads 12:05, sync requires audioDelay setting of -0.20 sec
3rd event, clock reads 12:10, sync requires audioDelay setting of +0.10 sec
4th event, clock reads 12:15, sync requires audioDelay setting of +0.35 sec
5th event, clock reads 12:20, sync requires audioDelay setting of +0.55 sec
6th event, clock reads 12:25, sync requires audioDelay setting of +0.80 sec
7th event, clock reads 12:30, sync requires audioDelay setting of +1.10 sec
8th event, clock reads 12:35, sync requires audioDelay setting of +1.40 sec
You can appreciate from the results the sort of audio sync problems which I have been battling with.
I also loaded the MP2 and WAV files into Goldwave and checked the times at which I could see the events happening (ie. the buzzer sounding). This is not 100% accurate as there is a startup time for the buzzer since it is a piezo device (it's a crystal and hence has a startup time). Even so I think these numbers are pretty accurate ..
Event# WAV MP2
1 01:27.745 01:27.744
2 06:27.733 06:27.731
3 11:27.728 11:27.725
4 16:27.719 16:27.717
5 21:27.845 21:27.717
6 26:27.834 26:27.705
7 31:27.830 31:27.701
8 36:27.826 36:27.698
so from this it appears that the two audio streams (MP2 and WAV) are good, in that the 'event's are occuring at the correct times (i.e. five minute intervals)
Kevin
Wilbert
10th August 2003, 14:15
Thus the duration of your merge2.avs is 74,695 frames (instead of 74,630)? Could you try:
1)
PluginPath = "c:\AVISynth25\Plugins\"
LoadPlugin(PluginPath + "MPEGDecoder.dll")
LoadPlugin(PluginPath + "mpasource.dll")
v=MPEGSource("D:\audio_test_8000_ntsc.m2v",-2)
a=WAVSource("D:\audio_test_8000_ntsc.wav")
audiodub(v,a)
2) Make a d2v file first, and then load the d2v with mpeg2dec3.
kevinm
11th August 2003, 19:05
Wilbert,
I ran the two experiments you suggested but I am not sure if the way I created the .d2v file was correct ..
I modified the MPEGSource line so it didn't have the '-2' switch, then when I loaded the .avs script into TMPGEnc it created the '-d2v' file for me ..
PluginPath = "c:\AVISynth25\Plugins\"
LoadPlugin(PluginPath + "MPEGDecoder.dll")
LoadPlugin(PluginPath + "mpasource.dll")
v=MPEGSource("D:\audio_test_8000_ntsc.m2v") # Omitting the '-2' allows avisynth/TMPGenc to create the '-d2v' file
a=WAVSource("D:\audio_test_8000_ntsc.wav")
audiodub(v,a)
Is this the correct way to create the '-d2v' ?
Anyway, assuming that this is ok I carried out the two experiments which you suggested (with reference to your previous message) ..
1) Length (as reported by 'framerate' in my avs script) = 74,631
2) Length (as reported by 'framerate' in my avs script) = 74,692
I tried confirming these frame counts using 'preview' in TMPGeng but the step by frame wasn't working reliably and sometimes would cause the program to hang altogether! If you can suggest some other MPEG viewer where I can single step frames I could try that. I also tried WinDVD4 but you don’t seem to be able to step by individual frame either,
Kevin
Wilbert
11th August 2003, 20:05
I meant: load your m2v file in dvd2avi, and make a d2v project. Open this d2v file in AviSynth ...
Script becomes:
v=MPEG2Source("D:\audio_test_8000_ntsc.d2v")
a=WAVSource("D:\audio_test_8000_ntsc.wav")
audiodub(v,a)
kevinm
12th August 2003, 02:13
Wilbert,
I created the .d2v using DVD2AVI, it ends up the same size as the one created via TMPGenc. In TMPGEnc I sourced the following .avs script ..
PluginPath = "c:\AVISynth25\Plugins\"
LoadPlugin(PluginPath + "MPEG2Dec3.dll")
v=mpeg2source("D:\DVD2AVI.d2v")
a=WAVSource("D:\audio_test_8000_ntsc.wav")
audiodub(v,a)
global original_length = framecount
Return Subtitle(Info()," Original length = " + String(original_length), size=30, text_color=$F0FF0F)
The resulting MPG was identical size to the one I created earlier using the .d2v from TMPGenc.
I played the MPG in TMPGEnc preview, here are the details:
Total framecount (according to .framecount) = 74,695
Last frame = 74,692
Last Info() message in stream says frame # is 74,691
Length = 00:41:32
I am suspicious of the preview function in TMPGenc, it doesn't seem very stable, I can't be certain that I am viewing the last frame,
Kevin
kevinm
12th August 2003, 19:11
Wilbert,
I have some more information which may help ..
Up till now I have been using my preferred capture/edit software 'Cyberlink's Power Director 2.55', but I have been suspicious all the way along of the frame rate. Avisynth says it is 29.97 but from all my audiosync experiments they suggest it is really 30fps. I am wondering if it is infact encoded to 30fps but it is just the header which says it is 29.97. Anyway, I decided to record from my DV camcorder to two different apps and them load these MPG into avisynth/TMPGEnc ..
Setting used in both apps: (generic) NTSC DVD, 29.97fps
I loaded the MPG from the two apps into TMPGenc and demuxed to give me the .d2v and .mp2 files. I then used audiodub to merge them back together. Then I noted the frame number at which the 30 minute event occurs. (this is one of the events when the LED on my circuit board is switched on)
Using MPG from Power Director Pro 2.55
===================================
Timer reads 12:29:59
LED comes on at frame # 56674
Using MPG from Inetervideo's Win Producer 2.0
==========================================
Timer reads 12:29:59
LED comes on at frame # 56626
Note,
at the start of the video the timer reads 11:58:30, so the elapsed time up to this 'event' is 1889 seconds. (this assumes that my digital timer which I filmed on my DV camcorder is keeping good time)
Now to do the 'maths' ..
For PD PRO: 1/30 * 56674 = 1889.1 secs
For Win Prod: 1/29.97 * 56626 = 1889.4 secs
So, doesn't this mean that Power Director Pro is in fact running at 30fps even though avisynth thinks it is running at 29.97? Does this mean that PR Pro is outputting a bad MPG stream, or at least getting the header wrong? And if so, is this likely to be causing the sync problems which I am seeing?
Do you know if there is any freeware out there that I could use to check the integrity of the MPG stream that PD Pro is producing?
BTW, I use PD Pro because it is relatively cheap, gives me the editing features I need and produces superior quality video compared to WinProducer,
Kevin
WarpEnterprises
12th August 2003, 21:37
if only the framerate in the header is incorrect,
try AssumeFPS(30) after mpeg2source and the framerate is "assumed" to be 30fps.
cweb
13th August 2003, 08:31
The Audio field in tmpgenc might be blanked out if the template you're using doesn't specify audio e.g. a kvcd template. The reason is that it expects you to encode audio separately using another program e.g. Besweet, and then mux the two.
kevinm
13th August 2003, 22:28
Hi all,
thanks for your suggestions, I think I am getting somewhere now ..
Re: AssumeFPS(30)
=================
Using the following .avs script I now get a result where audio is *almost* in sync with the video, also I don't see TMPGenc preview hanging any more when I try preview different parts of the stream ..
PluginPath = "c:\AVISynth25\Plugins\"
LoadPlugin(PluginPath + "MPEGDecoder.dll")
LoadPlugin(PluginPath + "mpasource.dll")
v=MPEGSource("D:\audio_test_8000_ntsc.m2v") # NOTE, this is without the '-2' option
v=assumefps(v,30)
a=WAVSource("D:\audio_test_8000_ntsc.wav")
audiodub(v,a)
global original_length = framecount
Return Subtitle(Info(),"Original length = " + String(original_length))
TMPGenc preview gives the following for the resulting MPG ..
Frames per second = 30
'Original Length' = 74,695
Length = 00:41:30
LED comes on at frame 56,627 (timer says 12:29:59)
BUT, if I used the '-2' option to MPEGSource the 'Original Length' is 74,631. The audio is out of sync and when try to view the result in TMPGEnc preview it hangs frequently.
Q/ What does the '-2' option do?
Re: Audio field blanked out.
========================
This only happens when I load my original .avs script which used Trim and DelayAudio. Also, this only happened if I trimmed the clip to certain lengths. When the trimmed clip was short (5 minutes or so) the audio would always load ok.
I am wondering if the problems with Trim have been due to this 'bad' MPG which I got from PowerDirector PRO. (where the header entry for fps doesn't match the actual frame rate).
I will now run the findAudioSyncScript to see what the delay is between the audio and video. Hopefully the delay will now be more or less constant through the clip so I should be able to fix it using delayAudio.
BTW what is Cyberlink shipping a product that claims to output files at 29.97fps when it is actually 30fps? I would contact their tech support, but whatever I send them goes into a black hole!
Kevin
WarpEnterprises
14th August 2003, 07:19
1) You don't need LoadPlugin when the DLLs are in the autoloading folder
2) Don't use MPEGDECODER if not really necessary (and it isn't in your case) - it's not so exact as MPEG2SOURCE from MPEG2DEC3
3) You can test if your DelayAudio problem comes from the audio: MPASource produces a tiny 8x8 video clip, too. So if you only use MPASource and Trim/Delay and get the error, there is something wrong with either your mp2 file or MPASource or else.
4) The fps the programs show is the value stored in the header, so that means quite nothing.
5) For reading that header use BitrateViewer and tell us what it says
6) MPASource produces a small file .d2a with 3 numbers: bytes of file, pcm samples and max volume.
pcm samples = sample frequ * 2(bytes) * 2(channels) * length (sec), so you can calc back the duration of the audio (that MPASource can read).
kevinm
14th August 2003, 17:08
'WarpEnterprises',
thanks for the suggestions. I have a question ..
Previously I used MPEG2SOURCE to read a .d2v file which I obtained using DVD2AVI, but this is an extra step in the flow. Is there a way of getting MPEG2SOURCE to read a .m2v video stream directly?
Are there and docs for MPEG2DEC3? I checked Nic's website and couldn't find any and I see from reading threads in this forum that there are a number of switches and I don't know what they do.
BTW with the .avs I included in my last post I was able to encode the whole video AND do the NTSC to PAL conversion and downsize to SVCD format and end up with the audio in sync! This is amazing and comes as some relief after battling with this for about three months.
Now I will try MPEG2SOURCE and see if I can improve the quality a little more,
thanks,
Kevin
kevinm
14th August 2003, 17:19
Sorry for the stupid question about the docs, I see now that there is a html doc in the zip file included in the download,
better go read up on it now,
Kevin
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.