Log in

View Full Version : Audio sync function. Need help.


ajp_anton
14th January 2011, 07:20
This is my script for syncing audio from captures. You give it multiple sync points in a string:
lda("0,.1,1000,.5,5000,-1.2")
and it will delay the audio at frames 0, 1000 and 5000 by .1, .5 and -1.2 seconds.
##lda = linear delayaudio
function lda(clip v,string pd){v

## find the 4 first values (2 points, 2 delays)
c1=findstr(pd,",")
c2=findstr(midstr(pd,c1+1),",")+c1
c3=findstr(midstr(pd,c2+1),",")+c2
c4=findstr(midstr(pd,c3+1),",")+c3
c4=(c4==c3?strlen(pd)+1:c4)

## extract those 4 values
p1=int(value(leftstr(pd,c1-1)))
d1=value(midstr(pd,c1+1,c2-c1-1))
p2=int(value(midstr(pd,c2+1,c3-c2-1)))
d2=value(midstr(pd,c3+1,c4-c3-1))

## sync the whole clip according to those points
timestretch(rate=100-100.0*framerate*(d2-d1)/(p2-p1))
delayaudio((d1*p2-d2*p1)/(p2-p1))

## trim only the relevant section. If more points, call the function again.
c4-1==strlen(pd)?trim(p1,0):trim(p1,p2-1)+lda(v,midstr(pd,c2+1))
}
Now, I'd like to add some info text that tells me the stretch and delay for each frame, to more easily add more sync points.

The stretch part is easy, since it's constant in each segment:
subtitle(string(100-100.0*framerate*(d2-d1)/(p2-p1)))

But how to display the delay?
I can only think of calculating it in scriptclip:
scriptclip("""subtitle(string(d1+(d2-d1)*(current_frame-p1)/(p2-p1)))""")
But it doesn't recognize the variables p1,p2,d1,d2. Making them global doesn't help because scriptclip will only use them from the last time the function calls itself.

Gavino
14th January 2011, 11:30
That's precisely the problem that GRunT was designed to solve (amongst others).

With GRunT, you write
scriptclip("""subtitle(string(d1+(d2-d1)*(current_frame-p1)/(p2-p1)))""",
\ args="d1, d2, p1, p2")

Incidentally, is the first point required to be at frame 0?
I can see that the function will trim off anything before that point.
Perhaps the last line should be
c4-1==strlen(pd)?last:trim(0, p2-1)+lda(v,midstr(pd,c2+1)).trim(p2, 0)

ajp_anton
14th January 2011, 19:00
Thanks, works like a charm!

I was aware of the frame 0 problem but never got to fixing it as it was only for private use so user friendliness had low prio...
Also the solutions I had in my head were more complex than yours and looked more ugly than not fixing it. Can't believe I didn't see how easy it really was, so thanks for that too =).