View Full Version : Using filename as variable in avisynth script
wthreex
24th January 2020, 13:04
I do hardsub on my videos, The .ass file and video always have the same filename, I'm using VSFillter to do that:
LoadPlugin("C:\Users\Downloads\Programs\MeGui\tools\avisynth_plugin\VSFilter.dll")
LoadPlugin("C:\Users\Downloads\Programs\MeGui\tools\ffms\ffms2.dll")
FFVideoSource("C:\Users\Downloads\Video\DASH_480.mp4", cachefile="C:\Users\Downloads\DASH_480.mp4.ffindex", fpsnum=30, fpsden=1, threads=1)
But is it possible to get and store video filename into variable an use it on this line ?
TextSub("C:\Users\Downloads\Other\$filenameVariable.ass")
I'm asking this because every time i have to manually rename .ass file in script
sneaker_ger
24th January 2020, 13:17
Yes, AviSynth knows variables.
http://avisynth.nl/index.php/Script_variables
A simple example
file="DASH_480"
ffvideosource(file+".mp4")
TextSub(file+".ass")
wthreex
24th January 2020, 14:10
Yes, AviSynth knows variables.
http://avisynth.nl/index.php/Script_variables
A simple example
file="DASH_480"
ffvideosource(file+".mp4")
TextSub(file+".ass")
Ok but how to get filename automatically?
file = someMethodToGetCurrentFilename() ?
sneaker_ger
24th January 2020, 14:19
http://avisynth.nl/index.php/Internal_functions/Runtime_functions#ScriptName ff
wthreex
24th January 2020, 15:05
@sneaker_ger thanks, But that function returns filename with '.avs' extension is there anything that i explode or replace the .avs to .mp4 ?
StainlessS
24th January 2020, 17:05
FN=".\whatever.avs" # whatever.avs in current directory
NoExtName = RT_FilenameSplit(FN,7) # Drive + Dir + Name (no extension)
Mp4Name=NoExtName + ".mp4"
From RT_Stats v1.43+
RT_FilenameSplit(string filename,int "get"=15)
Splits the un-named filename string into component parts selected by 'get' bit flags arg and returns the
parts joined together.
'Get' (default 15, 1 -> 15), If set, Bit 0=DRIVE, 1=Dir, 2=Name, 4=Extension.
Add 1 for Drive (bit 0), add 2 for Dir (bit 1), add 4 for Name (bit 2), add 8 for Extension (bit 3).
Some combinations do not make sense, eg Drive + Extension (1+8=9). Below sensible options.
1 = Drive (includes trailing ':')
2 = Dir (includes trailing '\')
3 = Drive + Dir
4 = Name
6 = Dir + Name
7 = Drive + Dir + Name
8 = Extension (includes leading '.')
12 = Name + Extension
14 = Dir + Name + Extension
15 = Drive + Dir + Name + Extension
Assuming a current working directory of eg "D:\avs\avi\", 'filename'="test.avi" and 'get'=15, returns "D:\avs\avi\test.avi",
so given a relative filename and default 'get'=15, is equivalent to RT_GetFullPathName(filename).
Maybe also handy, eg RT_GetFullPathName(".\FileInCurrentDir.avi") # might return ""D:\Myfolder\FileInCurrentDir.avi"
RT_GetFullPathName(string "name")
Creates an absolute or full path name for the specified relative path 'name'.
eg 'RT_GetFullPathName("test.avs")' might return "D:\avs\test.avs".
Throws an error if cannot return path.
StainlessS
25th January 2020, 11:48
Or with no dependancies,
Function ReplaceExtension(String fn,String "Ext") { # https://forum.doom9.org/showthread.php?p=1896882#post1896882
Ext=Default(Ext,"") # Default is "", ie STRIP extension, replace with "" (Ext should include DOT eg ".mp4")
rfn=fn.RevStr
i=rfn.FindStr(".")
Return (i==0) ? fn : rfn.MidStr(i+1).RevStr + Ext
}
BlankClip(width=320,height=240)
fn="D:\TEST\Something.avs"
s1=fn.ReplaceExtension()
s2=fn.ReplaceExtension(".mp4")
Subtitle(fn + "\n" + s1 +"\n" + s2,lsp=0)
https://i.postimg.cc/d1CH5sHt/t-00.jpg (https://postimages.org/)
EDIT: It is not bullet proof, eg fn="D:\TEST.DIRECTORY\Something" would fail chopping off ".DIRECTORY\Something".
wthreex
25th January 2020, 20:16
@StainlessS Thanks man much appreciated!
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.