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 5th March 2009, 22:06   #1  |  Link
martin53
Registered User
 
Join Date: Mar 2007
Posts: 405
Can I access the script filename in a predefined string variable?

I usually name .avs scripts same as the video files.
Usually, my first script line reads something like 's= "myVideo" ' and I refer to that string in the source functions, appending ".avi", ".wav" or whatever.

It would be extremely convenient to have a predefined variable (like "last"), that allowed me to use the name of the script file within the script.

I would then just set 's' to the script name and as soon as I copied an .avs template to the filename of a video file, the script would work instantly on that file - no more need to edit the script!

Maybe even calling a script that is called "myVideo--TFF.avs" is able to open "myVideo.avi" and setting a TFF switch - but that would require string separation commands, and I don't think I saw functions like BASIC's Left(), Mid() and InStr() in the doc..?

Can anyone please help me on this?

EDIT: I am copying stickboy's previous solution from the other thread here. stickboy!
- I just changed from complete name to basename for *.avs files, because this way you just need to write 'GetScriptBasename()+".avi"' to reference your video file -

Code:
function GetScriptBasename()
{
    Try
    {
        assert(false)
    }
    Catch(err_msg)
    {
        err_msg = MidStr(err_msg, FindStr(err_msg, "(") + 1)
        filename = LeftStr(err_msg, StrLen(err_msg) - FindStr(RevStr(err_msg), ",") - 4)
        return filename
    }
}
Please note that you must enter the function directly into the .avs script. You cannot use it from an .avsi include file.

Last edited by martin53; 6th March 2009 at 18:43.
martin53 is offline   Reply With Quote
Old 6th March 2009, 01:19   #2  |  Link
squid_80
Registered User
 
Join Date: Dec 2004
Location: Melbourne, AU
Posts: 1,963
There's LeftStr, MidStr, RightStr and FindStr functions. Unfortunately there's no function (that I know of) to give the name of the current script, maybe IanB will add it if we ask nicely enough.
squid_80 is offline   Reply With Quote
Old 6th March 2009, 10:01   #3  |  Link
krieger2005
Registered User
 
krieger2005's Avatar
 
Join Date: Oct 2003
Location: Germany
Posts: 377
I remember someone asked for something similar before. Till them nothing happend. Maybe none asked IanB :-D.
krieger2005 is offline   Reply With Quote
Old 6th March 2009, 13:07   #4  |  Link
juhok
Registered User
 
juhok's Avatar
 
Join Date: Dec 2005
Posts: 110
I've also been looking for something like this.
juhok is offline   Reply With Quote
Old 6th March 2009, 14:22   #5  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,380
It's been done before. See this thread; in particular, stickboy's solution.
Gavino is offline   Reply With Quote
Old 6th March 2009, 14:28   #6  |  Link
squid_80
Registered User
 
Join Date: Dec 2004
Location: Melbourne, AU
Posts: 1,963
Lol stickboy's right, that is sick
squid_80 is offline   Reply With Quote
Old 7th March 2009, 01:04   #7  |  Link
stickboy
AviSynth Enthusiast
 
Join Date: Jul 2002
Location: California, U.S.
Posts: 1,268
mf really deserves the credit/blame for the concept.
stickboy is offline   Reply With Quote
Old 11th March 2009, 14:04   #8  |  Link
jmartinr
Registered User
 
jmartinr's Avatar
 
Join Date: Dec 2007
Location: Enschede, NL
Posts: 285
I tried to solve the problem by writing a plugin that retrieves the commandline and from there gets the name of the script. It was my first C++ program and fun to make, and it works ok in a lot of cases, but not always (it doesn't work if you open the script from an open file dialog).

The other solution that's posted before is better.

Getting the commandline from a plugin also gives the opportunity of getting the name of the calling program, which in turn gives a lot of possibilities for other tweaks.

If anyone is interested in my new ProgramName solution which retrieves the name of the calling program, just say so.
jmartinr is offline   Reply With Quote
Old 11th March 2009, 14:10   #9  |  Link
squid_80
Registered User
 
Join Date: Dec 2004
Location: Melbourne, AU
Posts: 1,963
Getting the program name using VC++ is easy: _get_pgmptr().
squid_80 is offline   Reply With Quote
Old 11th March 2009, 15:00   #10  |  Link
jmartinr
Registered User
 
jmartinr's Avatar
 
Join Date: Dec 2007
Location: Enschede, NL
Posts: 285
Quote:
Originally Posted by squid_80 View Post
Getting the program name using VC++ is easy: _get_pgmptr().
OK. It was my first C++ avisynth thingy. The interesting part is maybe the possibility in avisynth to generate different output for different programs, for example a simple fast output for previewing (with for example framenumbers) and a denoised slow output for encoding. You could also use this value to process things differently in the avisynth filter of ffdshow.
jmartinr is offline   Reply With Quote
Old 11th March 2009, 15:07   #11  |  Link
J_Darnley
Registered User
 
J_Darnley's Avatar
 
Join Date: May 2006
Posts: 959
Quote:
Originally Posted by jmartinr View Post
If anyone is interested in my new ProgramName solution which retrieves the name of the calling program, just say so.
/me sticks his hand up with great enthusiasm

I was looking for this about a week ago so that the script could automatically switch from the previews I was doing to the slow(ish) encoding script.

In the end I just ended up using (true) ? a : b and manually editing the true/false.
__________________
x264 log explained || x264 deblocking how-to
preset -> tune -> user set options -> fast first pass -> profile -> level
Doom10 - Of course it's better, it's one more.
J_Darnley is offline   Reply With Quote
Old 11th March 2009, 16:28   #12  |  Link
jmartinr
Registered User
 
jmartinr's Avatar
 
Join Date: Dec 2007
Location: Enschede, NL
Posts: 285
Quote:
Originally Posted by J_Darnley View Post
/me sticks his hand up with great enthusiasm

I was looking for this about a week ago so that the script could automatically switch from the previews I was doing to the slow(ish) encoding script.

In the end I just ended up using (true) ? a : b and manually editing the true/false.
You can get it at: http://www.roelofs-coaching.nl/blobs...rogramName.zip.

Last edited by jmartinr; 20th December 2015 at 23:52. Reason: update download link
jmartinr is offline   Reply With Quote
Old 11th March 2009, 17:21   #13  |  Link
jmartinr
Registered User
 
jmartinr's Avatar
 
Join Date: Dec 2007
Location: Enschede, NL
Posts: 285
Quote:
Originally Posted by squid_80 View Post
Getting the program name using VC++ is easy: _get_pgmptr().
I get using that a failed assertion: _pgmptr !=NULL
jmartinr is offline   Reply With Quote
Old 21st November 2011, 05:02   #14  |  Link
vampiredom
Registered User
 
Join Date: Aug 2008
Posts: 233
I know this is a fairly old thread... I remember reading it when it was new; as determining the script name is something that would be important and handy for me. I have revisited the problem from time to time, finding no perfect solution. (and to my knowledge, no complete solution for AviSynth 2.5.8 exists)

I do believe, however, that I have made a "slightly" more elegant modification to this wonderfully dirty method of getting the script name. Given the limitation that the error itself (from which the script name is parsed) can only work in a script and not in an .avsi file, I have striven to pare down the scripting as much as possible.

First, let's make a parsing function called _ScriptName() and put that into an .avsi file:

Code:
function _ScriptName(string err_msg) {
        err_msg = MidStr(err_msg, FindStr(err_msg, "(") + 1)
        filename = LeftStr(err_msg, StrLen(err_msg) - FindStr(RevStr(err_msg), ","))
        return filename
}
Then, in your .avs script, you can use the _ variable to catch the error and hold the filename, so long as no other variables in your script are called _ :

Code:
Try {_} Catch(_) {_=_ScriptName(_)}

# now access it for some function
subtitle(_)
It seems to me that this is something you could call once and only once, at the very beginning of a script, since the script name will never change.

I like the _ because it is brief as can be and it reminds me of Special Variables in other scripting languages. Plus, you only need one line of code in your script to get the variable - and it is a cool and bizarre-looking line at that!

Last edited by vampiredom; 21st November 2011 at 17:10. Reason: fixed an error, heeded Gavino's advice.
vampiredom is offline   Reply With Quote
Old 21st November 2011, 05:50   #15  |  Link
Chikuzen
typo lover
 
Chikuzen's Avatar
 
Join Date: May 2009
Posts: 597
avs2.6 has internal functions ScriptName()/ScriptFile()/ScriptDir().
What is lost does not have anything and many obtain, that is the reason I use 2.6.0a3.
__________________
my repositories
Chikuzen is offline   Reply With Quote
Old 21st November 2011, 10:26   #16  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,380
Quote:
Originally Posted by vampiredom View Post
... I have striven to pare down the scripting as much as possible.
Yes, good idea, extract the common part into a function which need be written only once.

Quote:
Code:
function _ScriptName(err_msg) {
        err_meg = default(err_msg, "")
        ...
}
No need for the 'default' line, since you have made err_msg a mandatory parameter (by not putting quotes around the name in the function header). However it should be declared as a string:
function _ScriptName(string err_msg)

Actually, just spotted you have used err_meg in the default line, so the assigned value was never used.
__________________
GScript and GRunT - complex Avisynth scripting made easier

Last edited by Gavino; 21st November 2011 at 10:30.
Gavino is offline   Reply With Quote
Old 21st November 2011, 10:29   #17  |  Link
jmartinr
Registered User
 
jmartinr's Avatar
 
Join Date: Dec 2007
Location: Enschede, NL
Posts: 285
Quote:
Originally Posted by vampiredom View Post
Code:
Try {_} Catch(_) {_=_ScriptName(_)}

# now access it for some function
subtitle(_)
Why don't you keep it simple and call your variable ScriptName?
__________________
Roelofs Coaching
jmartinr is offline   Reply With Quote
Old 21st November 2011, 17:12   #18  |  Link
vampiredom
Registered User
 
Join Date: Aug 2008
Posts: 233
Quote:
No need for the 'default' line, since you have made err_msg a mandatory parameter (by not putting quotes around the name in the function header). However it should be declared as a string:
Oops! Pure sloppiness on my part. Edited and fixed. Thanks!

Quote:
Why don't you keep it simple and call your variable ScriptName?
Well, whether "_" or "ScriptName" is simpler is a matter of opinion, I guess. I like the _ because it is so amazingly brief (I am actually surprised you can use that as a variable name!) Really though, it reminds of using Perl's $_ variable ... and I love Perl.

Last edited by vampiredom; 21st November 2011 at 17:20.
vampiredom is offline   Reply With Quote
Old 22nd November 2011, 20:09   #19  |  Link
vampiredom
Registered User
 
Join Date: Aug 2008
Posts: 233
For completeness and forward-compatibility, it would make sense to have the _ScriptName() function attempt to return the results of the native function for those who have AviSynth 2.6 installed (or more future versions, presumably)

Code:
# in .avs script
Try {_} Catch(_) {_=_ScriptName(_)}
AVSfilename = _

# in .avsi file
function _ScriptName(string err_msg) {
        Try {
                # If AviSynth 2.6 is installed, use the native function
                ScriptName()
        } Catch(err) {
                err_msg = MidStr(err_msg, FindStr(err_msg, "(") + 1)
                filename = LeftStr(err_msg, StrLen(err_msg) - FindStr(RevStr(err_msg), ","))
                filename
        }
}
vampiredom is offline   Reply With Quote
Old 23rd November 2011, 00:34   #20  |  Link
jmartinr
Registered User
 
jmartinr's Avatar
 
Join Date: Dec 2007
Location: Enschede, NL
Posts: 285
Or maybe:

Code:
# in .avs script
Try {_ = ScriptName()} Catch(_) {_=_ScriptName(_)}
AVSfilename = _

# in .avsi file
function _ScriptName(string err_msg) {
                err_msg = MidStr(err_msg, FindStr(err_msg, "(") + 1)
                filename = LeftStr(err_msg, StrLen(err_msg) - FindStr(RevStr(err_msg), ","))
                filename
        }
}
__________________
Roelofs Coaching
jmartinr is offline   Reply With Quote
Reply

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 03:58.


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