Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

Reply
 
Thread Tools Search this Thread Display Modes
Old 26th June 2017, 21:40   #1  |  Link
Richard1485
Guest
 
Posts: n/a
Stretching Audio: N+1,2,3,etc Frames in AviSynth

I wonder if it's possible to stretch a section of audio and video so that it occupies the same number of frames as the original +1,2,3 etc. The important thing is for the audio to be stretched in as high a quality method as possible so that it occupies its new length; the video can make up the difference by adding duplicates (as long as it is returned in sync along with the audio). At present, I approximate the effect that I want by trimming out a section and doing the following.

Code:
#Assuming a 23.976fps A/V source...
Trim()
ChangeFPS(25000,1001)
AssumeFPS(24000,1001, sync_audio=true)
SSRC(48000)# or ResampleAudio(48000) or whatever
Then I call AlignedSplice() to reinsert the clip. Obviously, I change the numerator and denominator in ChangeFPS() if I want to vary the extent to which the clip is stretched. This works for very short clips but has obvious limitations, not least the fact that the stretched section stands out a bit. Surely it's possible to write a function that stretches longer sections by a frame or so and results in less obvious distortion. Any help with this would be appreciated!
  Reply With Quote
Old 26th June 2017, 23:05   #2  |  Link
johnmeyer
Registered User
 
Join Date: Feb 2002
Location: California
Posts: 2,691
For syncing audio and video it is always best to leave the video length alone (i.e., don't duplicate or decimate frames). The audio can be shortened or lengthened by quite a bit, without changing pitch, and without a noticeable hit in quality, if you use high-quality audio software. I use Vegas Pro, and once I've changed the audio length, I make sure that Vegas is using the "Elastique" plugin, which produces infinitely superior quality to the stretching plugin that originally shipped with that product.
johnmeyer is offline   Reply With Quote
Old 26th June 2017, 23:20   #3  |  Link
Richard1485
Guest
 
Posts: n/a
Thanks, johnmeyer! But I was hoping for an AviSynth-based solution if possible. :-)

In respect of the video, I agree with you. The reason that I'm stretching the video here is that it's a way of making AssumeFPS()'s sync_audio=true parameter do something it's probably not meant to do, and adding/dropping frames just helps me to visualize what's going on and has no effect on my output. Doing unorthodox things in AviSynth is something of a hobby of mine.
  Reply With Quote
Old 26th June 2017, 23:47   #4  |  Link
johnmeyer
Registered User
 
Join Date: Feb 2002
Location: California
Posts: 2,691
I've done a lot of tests for people, where they provide the audio clip and then various people stretch it. I don't remember AVISynth having a really good audio stretch plugin, especially when the result is compared to what the more professional, non-AVISynth software can do.
johnmeyer is offline   Reply With Quote
Old 26th June 2017, 23:54   #5  |  Link
Richard1485
Guest
 
Posts: n/a
I'm sure you're right, but I'd like to see if what I outlined above can be accomplished in AviSynth.
  Reply With Quote
Old 27th June 2017, 00:37   #6  |  Link
poisondeathray
Registered User
 
Join Date: Sep 2007
Posts: 5,346
you can use timestretch() . The quality actually isn't that bad
http://avisynth.nl/index.php/TimeStretch

I hardcoded this example for 90% speed, for some reason you can't use variables to calculate in the changefps field, but the value would be 100/90 or 100/value , where value is expressed in percentage

you can check with info that the new a/v length is the same (or very close)

Code:
#Assuming a 23.976fps A/V source...
whateversource()
trim()
timestretch(tempo=90)
changefps(1.111111*last.framerate)
assumefps(24000,1001)
#info()
It would be more elegant to use variables, so you can tweak the speedup/slowdown more easily, but the "m" isn't calculated, not sure why here
Code:
#Assuming a 23.976fps A/V source...
p=90 #in percent
m=(100/p)

whateversource()
trim()
timestretch(tempo=90)
changefps(m*last.framerate)
assumefps(24000,1001)
#info()

Last edited by poisondeathray; 27th June 2017 at 00:40.
poisondeathray is offline   Reply With Quote
Old 27th June 2017, 07:43   #7  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,431
Quote:
Originally Posted by poisondeathray View Post
It would be more elegant to use variables, so you can tweak the speedup/slowdown more easily, but the "m" isn't calculated, not sure why here
Code:
#Assuming a 23.976fps A/V source...
p=90 #in percent
m=(100/p)
100/90 in integer division gives 1
Try this ...
Code:
m = 100.0/p
__________________
GScript and GRunT - complex Avisynth scripting made easier
Gavino is offline   Reply With Quote
Old 27th June 2017, 09:43   #8  |  Link
Richard1485
Guest
 
Posts: n/a
Gavino and poisondeathray, thank you both, but 90% is quite a big difference. Is there a way to stretch a clip so that it returns the original number of frames +1 (or 2 or whatever) with video and audio still in sync?
  Reply With Quote
Old 27th June 2017, 11:05   #9  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,431
Quote:
Originally Posted by Richard1485 View Post
Is there a way to stretch a clip so that it returns the original number of frames +1 (or 2 or whatever) with video and audio still in sync?
You can easily change the ratio to suit any requirement.
Code:
n = 1 # or 2, 3, ... (no of frames to add)

whateversource()
trim(...)
fc = Framecount()
m = float(fc+n)/fc
timestretch(tempo=100/m)
changefps(m*last.framerate)
assumefps(24000,1001)
__________________
GScript and GRunT - complex Avisynth scripting made easier
Gavino is offline   Reply With Quote
Old 27th June 2017, 15:13   #10  |  Link
Richard1485
Guest
 
Posts: n/a
^ Thanks! I have two problems though.

First, I don't know how to adapt the above to a script in which a clip is already defined. Obviously, in your example, everything is being assigned to last. Normally, I would do something like...

Code:
s1=WhateverSource()
Trim(s1,x,y).Whatever().Whatever()
...but I can't do that here, because I have to define quantities (fc and m) in the middle of calls to various functions and AviSynth throws an error.

Secondly, if I change n to one, the above doesn't seem to be increasing the number of frames by one. I'm not sure why.

Code:
m = float(fc+1)/fc

Last edited by Richard1485; 22nd November 2018 at 19:30. Reason: clarification
  Reply With Quote
Old 27th June 2017, 15:16   #11  |  Link
manolito
Registered User
 
manolito's Avatar
 
Join Date: Sep 2003
Location: Berlin, Germany
Posts: 3,078
Quote:
Originally Posted by poisondeathray View Post
you can use timestretch() . The quality actually isn't that bad
http://avisynth.nl/index.php/TimeStretch
Agreed, just keep in mind that the current stable AviSynth v2.60 contains a somewhat outdated version of TimeStretch which cannot handle 6-ch sources.

If you want a newer TimeStretch version which is not restricted to stereo you can either download Wilbert's TimeStretch plugin
https://forum.doom9.org/showpost.php...2&postcount=20

or use the latest AviSynth v2.61 Alpha version which features an even newer version...


Cheers
manolito
manolito is offline   Reply With Quote
Old 27th June 2017, 15:24   #12  |  Link
poisondeathray
Registered User
 
Join Date: Sep 2007
Posts: 5,346
Quote:
Originally Posted by Richard1485 View Post
^ Thanks! I have two problems though.

First, I don't know how to adapt the above to a script in which a clip is already defined. Obviously, in your example, everything is being assigned to last. Normally, I would so something like...

Code:
s1=WhateverSource()
Trim(s1,x,y).Whatever().Whatever()
...but I can't do that here, because I have to define quantities (fc and m) in the middle of calls to various functions and AviSynth throws an error.

Secondly, if I change n to one, the above doesn't seem to be increasing the number of frames by one. I'm not sure why.

Code:
m = float(fc+1)/fc




Probably because of implicit "last"

You can specify exactly what you're referring to (eg. which framecount, the original, cuta , cutb, etc...)

here a1 is defined as segment (1000-2000), and that is what the framecount refers to, not original or some other cut. You would do that for each section you're trying to stretch to keep it organized

Code:
orig=last

n=4

a1=orig.trim(1000,2000)
fc = a1.Framecount()
m = float(fc+n)/fc


a1
timestretch(tempo=100/m)
changefps(m*last.framerate)
assumefps(24000,1001)
poisondeathray is offline   Reply With Quote
Old 27th June 2017, 15:28   #13  |  Link
poisondeathray
Registered User
 
Join Date: Sep 2007
Posts: 5,346
Quote:
Originally Posted by Gavino View Post
100/90 in integer division gives 1
Try this ...
Code:
m = 100.0/p
Thanks gavino



Quote:
Originally Posted by manolito View Post
Agreed, just keep in mind that the current stable AviSynth v2.60 contains a somewhat outdated version of TimeStretch which cannot handle 6-ch sources.

If you want a newer TimeStretch version which is not restricted to stereo you can either download Wilbert's TimeStretch plugin
https://forum.doom9.org/showpost.php...2&postcount=20

or use the latest AviSynth v2.61 Alpha version which features an even newer version...
Cheers, I didn't know about this one
poisondeathray is offline   Reply With Quote
Old 27th June 2017, 15:29   #14  |  Link
Richard1485
Guest
 
Posts: n/a
^^^ Thanks, manolito! I was wondering about that. My clip is dual mono, so I was wondering if I have to process the channels separately with 2.60 or just discard one before applying TimeStretch() and then call MonoToStereo() afterwards.

@Gavino
I've managed to get it working like this.

Code:
clip=AviSource("s.avi",audio=true)

clip2=Trim(clip,	40430,	40451)

fc = Framecount(clip2)
m = float(fc+1)/fc


clip3=					\
clip2						\
.TimeStretch(tempo=100/m)	\
.ChangeFPS(m*clip2.framerate)	\
.assumeFPS(24000,1001)

return clip3
No doubt you can find a more elegant way of doing it.

EDIT: Sorry, pdr. I was typing while you were posting and didn't see your replies.

Last edited by Richard1485; 27th June 2017 at 15:36.
  Reply With Quote
Old 27th June 2017, 16:31   #15  |  Link
johnmeyer
Registered User
 
Join Date: Feb 2002
Location: California
Posts: 2,691
I know the OP wants to use AVISynth, but it is actually a lousy tool for this particular problem. Why? Because you can't set the values simply by interacting with a GUI display of the audio on a timeline. Doing what the OP wants would take me just a few seconds in my NLE (Vegas):

1. Put the video on one track, and the audio on the track below.
2. Sync the audio and video at the beginning of the track.
3. Go to the end of the track, grab the edge of the end of the audio clip, hold the Ctrl key (to force the audio to stretch), and drag the audio until it syncs with the video.

If you already have another track that is synced to the video (i.e., lousy audio, but synced to the video), you can do this all by looking at the waveforms, and it is almost instantaneous.

To get a really good sync, you have to find places where people are talking, in close-up, and find a place where they say something with a "p" or "m" or other syllable where the lips come together. You then sync on these points. You do it at the beginning and the end of the clip, and follow my 1-2-3 instructions above.

I have no idea in the world how you are going to do that by plugging numbers into an AVISynth script, rendering out a result and then trying it out. It is going to take hours instead of seconds.

So, as I've said in other threads on this same subject, AVISynth is not the right tool for this project.

Last edited by johnmeyer; 27th June 2017 at 16:32. Reason: clarity
johnmeyer is offline   Reply With Quote
Old 27th June 2017, 19:07   #16  |  Link
Richard1485
Guest
 
Posts: n/a
^ Yes, you have said that many times. This thread is about syncing in AviSynth, and it's located in the AviSynth forum. You might have "no idea in the world" how I'm going to sync in AviSynth, but I know perfectly well what I'm doing and have been syncing tracks in a similar fashion for years; it will certainly not take me hours, and no rendering apart from the final render is required. This is the third time that I have responded (politely) to a post of yours in a similar vein, and I'd rather not do so again. I appreciate that you have a different approach, and you're welcome to it, but please start a new thread if you want to keep talking about doing this in Vegas.
  Reply With Quote
Old 27th June 2017, 21:55   #17  |  Link
johnmeyer
Registered User
 
Join Date: Feb 2002
Location: California
Posts: 2,691
Quote:
Originally Posted by Richard1485 View Post
^ Yes, you have said that many times. This thread is about syncing in AviSynth, and it's located in the AviSynth forum. You might have "no idea in the world" how I'm going to sync in AviSynth, but I know perfectly well what I'm doing and have been syncing tracks in a similar fashion for years; it will certainly not take me hours, and no rendering apart from the final render is required. This is the third time that I have responded (politely) to a post of yours in a similar vein, and I'd rather not do so again. I appreciate that you have a different approach, and you're welcome to it, but please start a new thread if you want to keep talking about doing this in Vegas.
Gee, I'm really sorry I tried to help. I apologize for that, and believe me, I'll never make the mistake again.

Good luck with your project. You obviously know what you are doing.

Last edited by johnmeyer; 27th June 2017 at 21:55. Reason: typo
johnmeyer is offline   Reply With Quote
Old 27th June 2017, 23:04   #18  |  Link
Richard1485
Guest
 
Posts: n/a
Quote:
Originally Posted by poisondeathray View Post
here a1 is defined as segment (1000-2000), and that is what the framecount refers to, not original or some other cut. You would do that for each section you're trying to stretch to keep it organized

Code:
orig=last

n=4

a1=orig.trim(1000,2000)
fc = a1.Framecount()
m = float(fc+n)/fc


a1
timestretch(tempo=100/m)
changefps(m*last.framerate)
assumefps(24000,1001)
Thanks for that! It works perfectly now.
  Reply With Quote
Old 22nd November 2018, 19:48   #19  |  Link
Richard1485
Guest
 
Posts: n/a
Upon reflection, when splicing a stretched section back into the "timeline", there are audible pops. At first, I thought it was because I was setting n to -1, but it's the same when it is set to 1. Perhaps, it's because the stretched audio doesn't quite match the length of the video.

Any suggestions would be appreciated. I know it's been a while.
  Reply With Quote
Reply

Tags
audio, avisynth, stretch

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 21:58.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.