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
Register FAQ Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread Display Modes
Old 16th October 2008, 19:37   #1  |  Link
tacman1123
Registered User
 
Join Date: Jun 2007
Location: Washington, DC
Posts: 130
Trim and Empty Clip

I'm looking at the Replace() function that someone wrote, and I think there's a problem if you're trying to replace the very first part of a clip. Here's the function (from somewhere in this forum, or maybe in the wiki):

Code:
    function Replace (clip old, clip inclip, int frame, int numframes) 
    {
        numframes = ((numframes == 0) || (numframes > inclip.framecount)) ? inclip.framecount : numframes 
        return old.Trim(0, frame - 1) + inclip.Trim(0, numframes) + old.Trim(frame + numframes, 0) 
    }
If I'm trying to replace the first x frames, I'd call

c.Replace(b, 0, 10)

However, that would call old.Trim(0, -1), which returns the first frame (according to the documentation on the Trim() page.

Part of the problem is that Trim() is overloaded -- if the second parameter is negative, then it means "framecount", so get 1 frame starting a frame zero. But what I want is zero frames.

Because Avisynth doesn't have if statements, something like this can be confusing for all but expert script writers. But hopefully someone will point me in the right direction!

Tac

PS Yes, I realize that Replace starting with zero could be rewritten as an align without ever calling replace, but I still would like to know how to do it, so that if it's called programmatically the function works as expected.
tacman1123 is offline   Reply With Quote
Old 16th October 2008, 19:54   #2  |  Link
Guest
Guest
 
Join Date: Jan 2002
Posts: 21,901
Code:
    function Replace (clip old, clip inclip, int frame, int numframes) 
    {
        numframes = ((numframes == 0) || (numframes > inclip.framecount)) ? inclip.framecount : numframes 
        return (frame > 0) ? old.Trim(0, frame - 1) + inclip.Trim(0, numframes) + old.Trim(frame + numframes, 0) : \
                 inclip.Trim(0, numframes) + old.Trim(frame + numframes, 0)
    }
Guest is offline   Reply With Quote
Old 16th October 2008, 19:57   #3  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,431
tac, I don't know where you got that Replace function from, but it's rubbish. Not only does it fail to handle frame=0 correctly, it adds one frame more than it should.

For any question about function writing, your first port of call should be stickboy's functions. There you will find JDL_ReplaceRange and lots of other good stuff as well.

If you want to see how to write functions well, spend a little while studying the code of all these functions.
Gavino is offline   Reply With Quote
Old 16th October 2008, 20:20   #4  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,431
Quote:
Originally Posted by neuron2 View Post
Code:
    function Replace (clip old, clip inclip, int frame, int numframes) 
    {
        numframes = ((numframes == 0) || (numframes > inclip.framecount)) ? inclip.framecount : numframes 
        return (frame > 0) ? old.Trim(0, frame - 1) + inclip.Trim(0, numframes) + old.Trim(frame + numframes, 0) : \
                 inclip.Trim(0, numframes) + old.Trim(frame + numframes, 0)
    }
I guess you just copied tac's version without looking too closely, but this also fails when frame=1 (a common mistake with functions like this). Here's a better one.
Code:
    function Replace (clip old, clip inclip, int frame, int numframes) 
    {
        numframes = ((numframes == 0) || (numframes > inclip.framecount)) ? inclip.framecount : numframes 
        return (frame > 0) ? old.Trim(0, -frame) + inclip.Trim(0, -numframes) + old.Trim(frame + numframes, 0) : \
                 inclip.Trim(0, -numframes) + old.Trim(frame + numframes, 0)
    }
Even then it doesn't cater for the replacement clip reaching the end of the old clip.

Last edited by Gavino; 16th October 2008 at 20:54. Reason: highlight changes
Gavino is offline   Reply With Quote
Old 16th October 2008, 22:50   #5  |  Link
stickboy
AviSynth Enthusiast
 
Join Date: Jul 2002
Location: California, U.S.
Posts: 1,267
Quote:
Originally Posted by Gavino View Post
Here's a better one.
Code:
    function Replace (clip old, clip inclip, int frame, int numframes) 
    {
        numframes = ((numframes == 0) || (numframes > inclip.framecount)) ? inclip.framecount : numframes 
        return (frame > 0) ? old.Trim(0, -frame) + inclip.Trim(0, -numframes) + old.Trim(frame + numframes, 0) : \
                 inclip.Trim(0, -numframes) + old.Trim(frame + numframes, 0)
    }
Even then it doesn't cater for the replacement clip reaching the end of the old clip.
Does that work for Replace(old, inclip, 0, 0) ? I don't remember offhand; does Trim(0, 0) return the entire clip?

It's much, much simpler to avoid trying to use Trim in functions like this. Use my Trim2/Trim3 functions instead. (See Caveats of using Trim in functions.)

Last edited by stickboy; 16th October 2008 at 22:55.
stickboy is offline   Reply With Quote
Old 16th October 2008, 22:52   #6  |  Link
tacman1123
Registered User
 
Join Date: Jun 2007
Location: Washington, DC
Posts: 130
More on Replacement

You're absolutely right, the first place to go is JDL's routines. The solution isn't simple, but he's provided it in JDL_ReplaceRange (which calls Trim2, which calls NullClip, both things I couldn't figure out how to handle in my original post).

I'd love to see more references to routines like this in the wiki, rather than just in the forums. Unfortunately, the capcha routine in the wiki is broken more often than not. My browser has been spinning for about 4 minutes trying to bring up an image, which is so cleverly convoluted that in the few times it's come up, I often don't type it correctly because I can't read it. I can't be the only one having this problem, and so I'm frustrated, not only because I can't add to the wiki, but because the wiki generally gets better with more contributions, and I'm sure I'm not the only one that's had something worthwhile to add but given up because the technology was in the way.

Anyway, I'm both using the JDL routines now and learning a lot from them!

Thx,

Tac
tacman1123 is offline   Reply With Quote
Old 16th October 2008, 23:38   #7  |  Link
tacman1123
Registered User
 
Join Date: Jun 2007
Location: Washington, DC
Posts: 130
@Stickyboy: FYI, there's a typo on line 490 of jdl-util.avsi, says "ruturn", should be "return". Do you plan to upgrade the scripts for 2.5.8? Is there any particular value to doing so?
tacman1123 is offline   Reply With Quote
Old 16th October 2008, 23:44   #8  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,431
Quote:
Originally Posted by stickboy View Post
Does that work for Replace(old, inclip, 0, 0) ? I don't remember offhand; does Trim(0, 0) return the entire clip?
Yes, it does.
Quote:
It's much, much simpler to avoid trying to use Trim in functions like this. Use my Trim2/Trim3 functions instead. (See Caveats of using Trim in functions.)
Agreed. This should be required reading for all function writers.
Gavino is offline   Reply With Quote
Old 17th October 2008, 01:01   #9  |  Link
stickboy
AviSynth Enthusiast
 
Join Date: Jul 2002
Location: California, U.S.
Posts: 1,267
Quote:
Originally Posted by tacman1123 View Post
@Stickyboy: FYI, there's a typo on line 490 of jdl-util.avsi, says "ruturn", should be "return". Do you plan to upgrade the scripts for 2.5.8? Is there any particular value to doing so?
Thanks for finding that. It should be fixed now.

I'm planning on pushing all the deprecated functions into a separate jdl-glue.avsi file, but I haven't gotten around to it yet. AviSynth development has slowed down, so there's a lot less need for keeping those backward-compatibility functions around.
stickboy is offline   Reply With Quote
Reply

Tags
trim


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 07:07.


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