View Full Version : shift video to audio synch with another source
buddha9
12th December 2024, 11:24
path="..."
pathDGD="..."
filename1="...mkv"
filename2="...mkv"
##################################################
LoadPlugin(pathDGD+"dgdecode.dll")
clip1 = FFmpegSource2(path+filename1)
clip2 = FFmpegSource2(path+filename2)
StackHorizontal(clip1, clip2)
Hi, how can I add a frame offset (Ex.: 5245 from frame 0) to the clip2 ?
Thanks!
tebasuna51
12th December 2024, 11:46
LoadPlugin(pathDGD+"dgdecode.dll") # Not used, you need ffms2.dll
clip1 = FFmpegSource2(path+filename1)
clip2 = FFmpegSource2(path+filename2).Trim(5245,0)
StackHorizontal(clip1, clip2)
Maybe a trim()?
buddha9
12th December 2024, 11:53
Maybe a trim()?
Thanks for quick reply :)
I need add a +5245 (positive shift) offset frames/milliseconds to the clipv2
And what about need to -3520 (negative shift) offset frames/milliseconds? Does trim support negative values?
How can I do? Has Trim function this parameter?
VoodooFX
12th December 2024, 12:46
Be aware that you desync with audio by "shifting" video frames:
clip1 = FFmpegSource2(path+filename1)
clip2 = FFmpegSource2(path+filename2)
blank = BlankClip(clip2, length=5245)
clip2_shifted = blank + clip2
StackHorizontal(clip1, clip2_shifted)
buddha9
12th December 2024, 13:29
Be aware that you desync with audio by "shifting" video frames:
clip1 = FFmpegSource2(path+filename1)
clip2 = FFmpegSource2(path+filename2)
blank = BlankClip(clip2, length=5245)
clip2_shifted = blank + clip2
StackHorizontal(clip1, clip2_shifted)
on MKVToolNix GUI there is this option
https://i.imgur.com/dYiJ2EC.png
You can insert positive/negative millisecond values for a video/audio track.
I need something like that on AVS+ :)
Is there any addons?
Thanks
Trim and BlankClip aren't not good for my needs :(
VoodooFX
12th December 2024, 13:48
Trim and BlankClip aren't not good for my needs :(
It does what you described.
Why it's not good?
buddha9
12th December 2024, 14:22
It does what you described.
Why it's not good?
I need possibility to shift forward/backward (specifying frames or milliseconds) video (w/o audio desynch)
It wolud be nice to have something like this :)
clip1 = FFmpegSource2(path+filename1)
clip2 = FFmpegSource2(path+filename2)
StackHorizontal(clip1, clip2.offset(type="frames", n="5344")) # or other possibility --> .offset(type="ms", n="-2345")
poisondeathray
12th December 2024, 14:40
I need possibility to shift forward/backward (specifying frames or milliseconds) video (w/o audio desynch)
It wolud be nice to have something like this :)
eg.
StackHorizontal(clip1, clip2.offset(5344) )
function Offset(clip c, int "frames")
{
frames = Default(frames, 0)
c1=BlankClip(c,length=frames)+c
return c1
}
buddha9
12th December 2024, 14:42
eg.
StackHorizontal(clip1, clip2.offset(5344) )
function Offset(clip c, int "frames")
{
frames = Default(frames, 0)
c1=BlankClip(c,length=frames)+c
return c1
}
Thanks poisondeathray!
does it work with a negative need? :scared:
VoodooFX
12th December 2024, 14:46
I need possibility to shift forward/backward (specifying frames or milliseconds) video (w/o audio desynch)
Sorry but that doesn't make sense.
And of course MKVToolNix doesn't do such nonsense.
buddha9
12th December 2024, 14:52
Sorry but that doesn't make sense.
Example:
You have a movie (OPEN MATTE version). [vid1]
You have also the same movie but "standard" version. [vid2]
These 2 are from different sources. Vid1 from HDTV, vid2 from DVD.
Same FPS but the "real" movie (on vid2) starts after some milliseconds comparing with first version [vid1]
You can play with Audacity. But my solution (with AVS+) is more "visual" :)
path1="I:\_WIP\path1"
filename1="file1.mkv"
path2="I:\_WIP\path2"
filename2="file2.avi"
clipv1 = FFmpegSource2(path1+filename1)
clipa1 = FFAudioSource(path1+filename1)
clipv2 = FFmpegSource2(path2+filename2)
clipa2 = FFAudioSource(path2+filename2)
clipt1= AudioDub(clipv1,clipa1)
clipt2= AudioDub(clipv2,clipa2)
StackHorizontal(Waveform(clipt1, window=5, height=0.333, zoom=5.0, under=false, smooth=true, aa=true), \
Waveform(clipt2.BilinearResize(968,720), window=5, height=0.333, zoom=5.0, under=false, smooth=true, aa=true))
poisondeathray
12th December 2024, 15:17
Thanks poisondeathray!
does it work with a negative need? :scared:
It appears to work ok, but it's pretty easy for you to test it
buddha9
12th December 2024, 15:26
It appears to work ok, but it's pretty easy for you to test it
...
StackHorizontal(Waveform(clipt1, window=4, height=0.333, zoom=5.0, under=false, smooth=true, aa=true), Waveform(clipt2.BilinearResize(968,720).Offset(-5200), window=4, height=0.333, zoom=5.0, under=false, smooth=true, aa=true))
function Offset(clip c, int "frames")
{
frames = Default(frames, 0)
c1=BlankClip(c,length=frames)+c
return c1
}
...
It does work with positive and negative frames.
Thanks so much! :)
Ps
maybe do you have also a version with milliseconds?
VoodooFX
12th December 2024, 15:28
Same FPS but the "real" movie (on vid2) starts after some milliseconds comparing with first version [vid1]
Then we back again, why Trim and BlankClip is not good for you?
poisondeathray
12th December 2024, 16:21
Ps
maybe do you have also a version with milliseconds?
Not really possible for ms accuracy because video "frames" have a quantized ms duration. eg. a "23.976p" film source would have about 41.708 ms duration per frame. You cannot have something like a quarter frame
buddha9
12th December 2024, 17:02
Not really possible for ms accuracy because video "frames" have a quantized ms duration. eg. a "23.976p" film source would have about 41.708 ms duration per frame. You cannot have something like a quarter frame
Ok no problem.
I calculate ms (difference btw 2 points on timeline) with help of AVS+ status bar :)
Thans so much for support!
# film 1
path1="I:\_WIP\path1\"
filename1="file1.mkv"
clipv1 = FFmpegSource2(path1+filename1)
clipa1 = FFAudioSource(path1+filename1)
src1w=Width(clipv1)
src1h=Height(clipv1)
clipt1= AudioDub(clipv1,clipa1)
# film 2
path2="I:\_WIP\path2\"
filename2="file2.avi"
clipv2 = FFmpegSource2(path2+filename2)
clipa2 = FFAudioSource(path2+filename2)
clipt2= AudioDub(clipv2,clipa2)
# HorizontalStack
StackHorizontal(Waveform(clipt1.DelayAudio(-1.6), window=4, height=0.333, zoom=5.0, under=false, smooth=true, aa=true), Waveform(clipt2.BilinearResize(src1w,src1h).AssumeFPS(23.976,sync_audio=true).Offset(25).DelayAudio(0.18), window=4, height=0.333, zoom=5.0, under=false, smooth=true, aa=true))
# Thanks to poisondeathray
function Offset(clip c, int "frames")
{
frames = Default(frames, 0)
c1=BlankClip(c,length=frames)+c
return c1
}
VoodooFX
12th December 2024, 21:13
So, in the end BlankClip IS good. :D
StainlessS
13th December 2024, 02:40
Nifty Offset() function PDR.
Possibility of 0 or -ve Length not mentioned on Wiki.
Some time ago, Pinterf [EDIT: qyot27] was gonna throw error (I think) on BlankClip(Length=0), because it caused some problem if not used correctly.
I was a bit miffed about it (I used Length=0 in some scripts), but accepted decision to make it illegal, but then
I think he changed his mind and accepted as legal again.
Looks like he also allowed for -ve length [as your Offset() func works], maybe could do with a mention of it on wiki.
[Dont know if Length=-ve also accepted in v2.60 Std or v2.58.]
EDIT: I think the problem with allowing 0 length clip was the possibility of trying to display a zero length clip in VDub/Player
which caused some kind of exception/illegal instruction. [but only if used incorrectly, ie trying to show a 0 len clip]
BlankClip on Wiki:- http://avisynth.nl/index.php/BlankClip
poisondeathray
13th December 2024, 03:06
Nifty Offset() function PDR.
Possibility of 0 or -ve Length not mentioned on Wiki.
Honestly I had no idea (-) would work. I just wrapped VoodooFX's suggestion into a helper function. ( "Wrapped" because some people like presents to be wrapped this time of year :D )
Some time ago, Pinterf was gonna throw error (I think) on BlankClip(Length=0), because it caused some problem if not used correctly.
I was a bit miffed about it (I used Length=0 in some scripts), but accepted decision to make it illegal, but then
I think he changed his mind and accepted as legal again.
Probably this
https://github.com/AviSynth/AviSynthPlus/commit/792a8aceec5f20526c1ac45731aa2621d4b333a8
EDIT: I think the problem with allowing 0 length clip was the possibility of trying to display a zero length clip in VDub/Player
which caused some kind of exception/illegal instruction. [but only if used incorrectly, ie trying to show a 0 len clip]
BlankClip on Wiki:- http://avisynth.nl/index.php/BlankClip
I think zero total frames still throws an error in some programs - but when total number of frames is not zero - programs don't seem complain.
a=colorbars().trim(0,9).showframenumber() #10 total frames
#a.offset(-9) #works; 1 total frame
#a.offset(-10) #zero frames
# error message in avspmod about display_pitch, but still usable , can edit script, open other videos
# vdub2 doesn't crash , still usable, can open other scripts/videos
# mpchc doesn't crash, still usable, can open other vids/scripts
Or can you suggest a better way of handling it ? It seems ok to me
StainlessS
13th December 2024, 17:46
by ssS
Some time ago, Pinterf was gonna throw error
Actually qyot27.
by PDR
Probably this
Yep, and this is the point where Length=0 was initially (temporarily) made illegal
https://github.com/AviSynth/AviSynthPlus/commit/9c79d295b6917741d99a26bb28eee829223d1d54
Avisynth Developement thread, post about illegal Length=0,
https://forum.doom9.org/showthread.php?p=1986258#post1986258
And a thread from about same time as above changes about Length=0, actually using Length=0 example.
https://forum.doom9.org/showthread.php?p=1986352&highlight=length%3D0#post1986352
Post in avs/devs where qyot27 got a bit spooked by the Length=0 whotsit
This. FFmpeg reads video and audio packets separately, and if either reaches EOF, it attempts to read the other one before exiting, because it is easily possible - and not a bug - that either video or audio might be longer than the other, and it shouldn't be truncated.
The error is that length=0 is treated by BlankClip as somehow valid when it isn't, and ends up creating an audio stream of infinite length, which FFmpeg then attempts to read/output. Forcing an output length via the -t parameter would probably demonstrate that it is indeed attempting to write an ever-increasing number of audio samples, until the process is killed.
Post in avs/devs where qyot27 reverted from the illegal Length=0 thingy.
There happened to be some stuff concerning general timestamps/duration/framecount in FFmpeg during April (and coincidentally after the no zero-length clips commit on our side), and if I had to guess, those commits addressed this from FFmpeg's side without ever touching the AviSynth demuxer, since a reverted version of AviSynth+ no longer seems to trip anything with a script consisting of just BlankClip(length=0) (ffplay doesn't reject it as empty, but that's probably just ffplay being ffplay; it doesn't close regular videos when they finish).
So tentatively, it's been reverted. If it re-emerges, though, it will have to be addressed. Memory overflows are not something I'm willing to roll the dice with just because there's useful edge cases.
by PDR
Or can you suggest a better way of handling it ? It seems ok to me
Seems ok to me tooooo.
poisondeathray
13th December 2024, 18:44
ffmpeg seems ok too and doesn't crash
ffmpeg -i "test_zero_frames.avs" -c:v rawvideo -an -f null NUL
[out#0/null @ 0000004d0260cbc0] Output file is empty, nothing was encoded(check-ss / -t / -frames parameters if used)
frame= 0 fps=0.0 q=0.0 Lsize=N/A time=N/A bitrate=N/A speed=N/A
Passing zero length video and audio (no -an) is ok too
gispos
13th December 2024, 22:54
I don't know if it's just about looking? If so, that's what I do.
Split_View_Offset (https://drive.google.com/file/d/1l_0PksYFCHh4mXz8iM94X17-60ym9Rs9/view?usp=drive_link)
buddha9
15th December 2024, 12:01
I don't know if it's just about looking? If so, that's what I do.
Split_View_Offset (https://drive.google.com/file/d/1l_0PksYFCHh4mXz8iM94X17-60ym9Rs9/view?usp=drive_link)
Inmpressive!
Thanks so much gispos! :D
Can you give me this script please? :)
gispos
15th December 2024, 16:28
Inmpressive!
Thanks so much gispos! :D
Can you give me this script please? :)
This is not a script, it works in AvsPmod with SplitView and Video > Tools > 'Locate frame'.
A group must be set for the current tab and the tab to the right of it. Then start SplitView in the left script and start Video > Tools > 'Locate frame'. The non selected script is frozen with 'Freeze frame'.
To find the offset automatically with 'Locate frame', the wonderful 'LocateFrames' function from Doom9 member StainlessS is needed.
https://www.mediafire.com/folder/hb26mthbjz7z6/StainlessS or http://www.sendspace.com/folder/2mwrco and you can also read Help > 'Locate frame readme'
However, you have to limit the search radius with start and stop, otherwise the search will take too long. So manually scroll in the right script to close to the same position as the left script and then click on 'Start'.
The threshold may need to be set high (50) if the two videos are visually very different.
FranceBB
15th December 2024, 19:26
ffmpeg seems ok too and doesn't crash
Not any longer, but it used to. I remember the conversation StainlessS pointed at very well: Link (https://forum.doom9.org/showthread.php?t=181351&page=119)
It was April 2nd 2023 and during the night someone started uploading several images via Aspera as they were recording a sailing competition in New Zealand and they also shot some pictures. Those got downloaded automatically, then they were indexed, converted with all the various adjustments, a mute audio track was inserted and they were eventually supposed to be re-encoded to XDCAM-50 (i.e MPEG-2 50 Mbit/s + PCM 24bit 48000Hz audio muxed in mxf) and checked in into Interplay automatically. Unfortunately, with images being "a picture" they resulted in one single frame called frame 0, so a BlankClip(length=0) was added to create the mute audio track but that turned out to be a very bad choice 'cause what FFMpeg did was reading the script and it just kept going... and going... and going... and going... forever. Quickly enough all the nodes in the farm ended up being saturated and overwhelmed and the whole thing crashed. Restarting didn't help as it started processing the next batch of images until it crashed again. I even saw one server with Windows Server 2019 Standard x64 and 128 GB of RAM reaching 1 TB of paging file before giving up and crashing.
The short term solution was forcing BlankClip() to use a length that started from at least 1, but it took me a bit to figure out the issue, change the code and re-compile.
BlankClip was using the $i_src_vid_frames variable and sure enough I added
If $i_src_vid_frames = 0 Then $i_src_vid_frames = 1
and it started working again.
Anyway, I'm glad to see that it has been fixed in FFMpeg as well now. :)
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.