Revgen
6th January 2006, 23:47
Poutnik posted this function in this thread (http://forum.doom9.org/showthread.php?t=105126) that allows multiple avi captures to be joined while keeping audio in sync. I felt that it would be better to have a thread of it's own. So I've decided to repost his original post here.
Revgen
--Poutnik's original post---
I decided to post my script, related to A/V syncing, it may help somebody..
I do TV captures, splitted to 2GB files. My PVR has setting "master stream"
if set to none - A/V can be out of sync. if video/audio is set to master, the latter get rate that is not nominal to be in sync with former.
The function GetNC is applicable for all these cases.
I get this sequence: capture.avi, capture[1].avi, capture[2].avi, ........
Each file can have slightly different FPR and audiorate - it does not matter.
GetNC("capture") function produces synced joined clip with A/V with nominal rates.
Function can easily be optimized, e.g. by Avisynth script recursion,
tailored for other naming patterns too.
In time of writing I did not know about script recursion, now I "do not touch running system" :-)
#Counts video time (sec)
function VLen(clip c) { return float(framecount(c))/float(framerate(c)) }
#Counts audio time (sec)
function ALen(clip c) { return float(audiolength(c))/float(audiorate(c)) }
#Counts Audio/video time ratio
function AVSync(clip c) { return ALen(c)/VLen(c) }
function NominalAudioRate(clip c) {
#Returns supposed nominal AudioRate value, e.g. 44106 -> 44100
ar=audiorate(c)
return (ar > 46000 ) ? 48000 : ((ar > 37000 ) ? 44100 : \
((ar > 28000 ) ? 32000 : ((ar > 23000 ) ? 24000 : ((ar > 19000 ) ? 22050 : ((ar > 14000 ) ? 16000 : 11025 )))))
}
function NominalFPS(clip c) {
#Returns supposed nominal FrameRate value, e.g. 25.007 -> 25.000
fps=framerate(c)
return (fps > 27.5 ) ? 29.997 : ((fps > 24.5 ) ? 25.0 : 23.976)
}
function NClip(clip c) {
#create A/V synced clip with nominal FPS+Audiorate to allow joining splitted files
FPSNom=NominalFPS(c)
ARNom=NominalAudioRate(c)
assumefps(c,FPSNom,false).assumesamplerate(ARNom)
return timestretch(tempo=100.0*avsync())
}
function GetNC(string s, string "ptype") {
ptype=default(ptype,"YUY2")
n=0
filename = s+".avi"
last=avisource(filename, pixel_type=ptype).NClip()
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
return last
}
Revgen
--Poutnik's original post---
I decided to post my script, related to A/V syncing, it may help somebody..
I do TV captures, splitted to 2GB files. My PVR has setting "master stream"
if set to none - A/V can be out of sync. if video/audio is set to master, the latter get rate that is not nominal to be in sync with former.
The function GetNC is applicable for all these cases.
I get this sequence: capture.avi, capture[1].avi, capture[2].avi, ........
Each file can have slightly different FPR and audiorate - it does not matter.
GetNC("capture") function produces synced joined clip with A/V with nominal rates.
Function can easily be optimized, e.g. by Avisynth script recursion,
tailored for other naming patterns too.
In time of writing I did not know about script recursion, now I "do not touch running system" :-)
#Counts video time (sec)
function VLen(clip c) { return float(framecount(c))/float(framerate(c)) }
#Counts audio time (sec)
function ALen(clip c) { return float(audiolength(c))/float(audiorate(c)) }
#Counts Audio/video time ratio
function AVSync(clip c) { return ALen(c)/VLen(c) }
function NominalAudioRate(clip c) {
#Returns supposed nominal AudioRate value, e.g. 44106 -> 44100
ar=audiorate(c)
return (ar > 46000 ) ? 48000 : ((ar > 37000 ) ? 44100 : \
((ar > 28000 ) ? 32000 : ((ar > 23000 ) ? 24000 : ((ar > 19000 ) ? 22050 : ((ar > 14000 ) ? 16000 : 11025 )))))
}
function NominalFPS(clip c) {
#Returns supposed nominal FrameRate value, e.g. 25.007 -> 25.000
fps=framerate(c)
return (fps > 27.5 ) ? 29.997 : ((fps > 24.5 ) ? 25.0 : 23.976)
}
function NClip(clip c) {
#create A/V synced clip with nominal FPS+Audiorate to allow joining splitted files
FPSNom=NominalFPS(c)
ARNom=NominalAudioRate(c)
assumefps(c,FPSNom,false).assumesamplerate(ARNom)
return timestretch(tempo=100.0*avsync())
}
function GetNC(string s, string "ptype") {
ptype=default(ptype,"YUY2")
n=0
filename = s+".avi"
last=avisource(filename, pixel_type=ptype).NClip()
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
n=n+1
filename = s + "[" + string(n) + "].avi"
last=exist(filename) ? (last ++ avisource(filename, pixel_type=ptype).NClip() ) : last
return last
}