Cutting out adverts with trim and in-sync audio

Support forum for DGDecNV
Post Reply
DAE avatar
BarryW
Posts: 56
Joined: Fri Mar 18, 2011 7:50 pm

Cutting out adverts with trim and in-sync audio

Post by BarryW »

Could you give me a clue as to how to use trim to cut out adverts and have in-sync audio upto the trim point ?

Here's 9 seconds of HD with the start of an advert - the advert starts on frame 175.
Filename monsterpython.ts
http://db.tt/x9h9URDt

I pre-converted the AC3 to WAV using Audacity 2.0.3 so that I could use WAVSource.

Here's the AVS script I used:
loadplugin("C:\Program Files\megui\tools\dgindexnv\DGDecodeNV.dll")
DGSource("D:\DVD\monsterpython.dgi", deinterlace=1)
V = Spline64Resize(1280,720).trim(0,174)
A = WAVSource("D:\DVD\monsterpython.wav")
AudioDub(V,A)
#DelayAudio(-0.552)

The MKV contains perfectly trimmed video but no audio.
The DelayAudio() was commented out so the audio (if it existed) would be out-of-sync.
The TS file shows an audio offset of -813ms w.r.t video stream but DGIndexNV 2044 creates an offset of -552ms.
I assume the -552ms is w.r.t to the PCR clock.

The question is, how do we create MKV with in-sync audio upto the trim point?

Update:
The only way I've managed to do it is to first put the AC3 audio through MeGUI and remove the -552ms offset - this just shifts the audio to the left.
Then import the shifted audio into Audacity and delete the audio after 7 seconds (175/25) and export the corrected audio.
At this point the video and audio lengths should match so I then muxed the video (in the encoded MKV) with the corrected AC3 audio - this gives in-sync audio upto the trim point.

The problem with this technique is that it only works for small files. When the audio filesize is much bigger Audacity has trouble accurately saving an exact time duration. If the video and audio don't match exactly then it's a waste of time. I've gone back to cutting on the 'I' frame at the scene change using mkvtoolnix - this usually works as long as there's a change in picture when the advert starts.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: Cutting out adverts with trim and in-sync audio

Post by admin »

I can't advise you on making MKV files. I would process it like this:

vid=dgsource("monsterpython.dgi")
aud=nicac3source("monsterpython PID 902 2_0ch 48KHz 384Kbps DELAY -552ms.ac3").delayaudio(-0.552)
audiodub(vid,aud)
trim(0,173)

Note that I adjust for the audio delay, then dub them, and then trim the result.
DAE avatar
Nick007
Posts: 38
Joined: Wed Sep 29, 2010 12:20 pm

Re: Cutting out adverts with trim and in-sync audio

Post by Nick007 »

You're probably better off with cutting the audio separately from avisynth, without reencoding. eac3to or delaycut can easily cut ac3 (and it's fast and lossless operation). Combined delay for -552ms and -173 frames (-173/23.976*1000 = -7215ms) would be -7767ms.
DAE avatar
BarryW
Posts: 56
Joined: Fri Mar 18, 2011 7:50 pm

Re: Cutting out adverts with trim and in-sync audio

Post by BarryW »

Thanks, I've give both ways a go and see what happens.
I noticed you used 173 as the trim point - that's probably a good idea with interlaced video.
Assuming frame 0 is the first frame that gives 174 frames in total.
The sample video is 25fps so the audio would be cut at (174*1000)/25 + 552 = 7512ms
---
When I previously re-loaded the cut audio back into Audacity (to check it's exact duration) and zoomed in at the end point you'll find the audio duration isn't exactly same as the original cut point. Obviously the audio packets have a finite size and the overall duration will be slightly shorter than expected.
DAE avatar
BarryW
Posts: 56
Joined: Fri Mar 18, 2011 7:50 pm

Re: Cutting out adverts with trim and in-sync audio

Post by BarryW »

loadplugin("C:\Program Files\megui\tools\dgindexnv\DGDecodeNV.dll")
vid = DGSource("D:\DVD\monsterpython.dgi", deinterlace=1).Spline64Resize(1280,720)
aud= nicac3source("monsterpython PID 902 2_0ch 48KHz 384Kbps DELAY -552ms.ac3").delayaudio(-0.552)
audiodub(vid,aud)
trim(0,173)

AudioDub doesn't create an audio stream in the MKV - not sure why this is.

Trimming the audio in Audacity including the offset and muxing into the MKV works which gives fewer steps than pre-shifting the audio to remove the offset and then trimming.
Using the video trim method will make life much easier than relying on x264 creating an 'I' frame at the scene change.

I muxed 384Kbps AC3 and also 160K AAC-LC with the trimmed MKV video then demuxed the audio from the MKV's and took a look at the AC3 and AAC files using Audacity.
The AC3 was now -16ms short of 6.96 seconds while the AAC was +16ms over the 6.96 seconds.
If the audio packet size is 32ms then having an error of +/-0.5 a packet is about as good as you can get I assume.

The 6.96 seconds comes from 174/25.
DAE avatar
Nick007
Posts: 38
Joined: Wed Sep 29, 2010 12:20 pm

Re: Cutting out adverts with trim and in-sync audio

Post by Nick007 »

Oh, sorry, should've written 174 frames.
DAE avatar
BarryW
Posts: 56
Joined: Fri Mar 18, 2011 7:50 pm

Re: Cutting out adverts with trim and in-sync audio

Post by BarryW »

AudioDub still doesn't work for me.

I installed a standard AviSynth 2.5.8 ST version with just the default plugins and added the nicaudio.dll (v2.0.0.5) to the Avisynth plugins folder on a clean PC running Windows XP SP3.

Here's the 49seconds HD TS file I used for testing:
Filename: B01.ts (60Mbytes)
http://db.tt/tqxMoChU

Here's the AVS script I used:
loadplugin("C:\Program Files\megui\tools\dgindexnv\DGDecodeNV.dll")
Vid = DGSource("E:\DVD\B01.dgi", deinterlace=1).Spline64Resize(1280,720)
Aud = NicAc3Source("E:\DVD\B01 PID 157d 2_0ch 48KHz 192Kbps DELAY -128ms.ac3").DelayAudio(-0.128)
AudioDub(Vid,Aud)
Trim(0,287)

MediaInfo shows that the MKV only contains the video stream with no audio. I used both the command line batch file method and MeGUI to run x264. I can't find any x264 parameters to allow audio pass through. I suspect that the Avisynth AudioDub() command just doesn't work anymore.

Here's the batch file for running x264 (the batch file is called high41.bat):
x264 --profile high --level 4.1 --crf %1 --min-keyint 15 --keyint 250 --ref 6 --me umh --merange 24 --subme 9 --no-fast-pskip --output B01.mkv B01.avs

To start the command prompt in low priority from the Run field type
cmd /c start /low

To run the batch file type
high41 20

AudioDub also doesn't work with DirectShowSource either:

Load_Stdcall_Plugin("C:\Program Files\MeGUI\tools\yadif\yadif.dll")
DirectShowSource("E:\DVD\B01.ts")
Yadif(order=1)
Vid = Spline64Resize(1280,720)
Aud = NicAc3Source("E:\DVD\B01 PID 157d 2_0ch 48KHz 192Kbps DELAY -128ms.ac3").DelayAudio(-0.128)
AudioDub(Vid,Aud)
Trim(0,287)

It might work with Xvid output.
DAE avatar
kypec
Posts: 22
Joined: Tue Sep 21, 2010 2:37 am

Re: Cutting out adverts with trim and in-sync audio

Post by kypec »

BarryW wrote:AudioDub still doesn't work for me.
I can't find any x264 parameters to allow audio pass through. I suspect that the Avisynth AudioDub() command just doesn't work anymore.

AudioDub also doesn't work with DirectShowSource either:
Are you aware that x264 doesn't process/pass-through audio at all? :o
x264 is just a video encoder, it has never supported audio encoding in any way (there were some experimental builds with audio processing enabled but I doubt you're the one who uses them).
You need to include audio from your Avisynth script into target MKV through some other tools, not x264!
DAE avatar
BarryW
Posts: 56
Joined: Fri Mar 18, 2011 7:50 pm

Re: Cutting out adverts with trim and in-sync audio

Post by BarryW »

AC3 packet size is 32ms. If using the trim command in the AVS script and pre-editing the AC3 audio length using Audacity (to match the trimmed video) then the formula for calculating the audio length needs to be something like this (assuming the audio delay is 0ms).

tA = ((INT((tV*1000)/32+0.5)*32)-1)/1000
where tV is video run-time in seconds. It's the Number-of-video-frames/Frame-Rate.
If you try to edit the audio very accurately with Audacity you'll get another 32ms packet added - that's why I added -1 to the formula. When you double check the audio run-time (by reloading the saved audio file back into Audacity) the run-time will have +1ms added to bring it back onto a 32ms boundary. To get an audio delay of 0ms means pre-converting the AC3 audio to bring it into sync with the video.
Post Reply