Examples :: Load a text file with arbitrary clips
This script demonstrates a way to use the ability offered by AVSLib to change the default array delimiter in order to create a script that can load and edit non-predefined clips.
The later allows to treat the script as an Avisynth "program" which, in cooperation with a command line driven encoder (such as Avs2Avi, ffmpeg or VirtualDub plus a Sylia script) can fully automate the related NLE process, since it will be reduced to a single shell command invocation.
# load required modules LoadModule("avslib", "base", "constants") LoadModule("avslib", "base", "conversion") LoadPackage("avslib", "array") LoadModule("avslib", "filters", "stack") # load a text file containing dir /b or equivalent output # surrounded by triple quotes at the start and end of file dir = Import("files.txt") # after the line below dir is an array of filenames! # but be careful if you hand-type arrays... ArrayDelimiterSet(CRLF) # cleanup dir from invalid files (CInt is used to convert true/false to 1/0) okflag = dir.ArrayOpFunc("Exist").ArrayOpFunc("CInt") dir = dir.ArrayReduce(okflag) # read in clips clp = dir.ArrayOpFunc("DirectShowSource") # stack them - preserving aspect ratio by cropping - into a 960x640 frame return StackToFit(clp, 960, 640, RTF_CROP)
As it is apparent from the code, the key aspects of the "trick" presented is
- to insert triple quotes at the start and end lines of the input file so that Import() will consider the contents to be an Avisynth string,
- to change the array delimiter to a CR/LF pair so that each line of the file be treated as an array element.
Because this setup produces an empty initial array element, but also as a safety check since input is unpredictable because it is not hard-coded into the script anymore, the standard Avisynth function Exist is applied to all script elements and those where it returns false are removed from the array by ArrayReduce.
Furthermore, since ArrayReduce needs a zero / non-zero "flag" array in order to decide which elements will keep, the conversion function CInt is applied to the array that resulted from the application of Exist (see the OOP-style chain at the line assigning to okflag variable) to produce a 0 / 1 value for each false / true value.
The creation of the "files.txt" in the above example can easily be automated with a custom shell script. A working example (say named "avidir.cmd") is provided below:
@echo off echo """ for %%f in (%1) do @echo %%~ff echo """
The shell script takes one (needed) argument, a wildcard specification which can include path information in case the files are not at the current directory. The script outputs absolute filenames, so that the calling avisynth script can be located anywhere in the filesystem.
The final step to automate the process is to make another custom shell script (say "makeavi.cmd") that will update the contents of "files.txt" and (optionally, depending on your needs) invoke the encoder. For example:
@echo off avidir %1 > files.txt avs2avi script.avs -l %2
The top level shell script takes the same one (needed) argument, a wildcard specification, as the previous plus an additional 2nd argument (a filename with the codec settings). It is assumed that the encoder executable is placed at a folder included in the PATH environment variable.
Thus you can invoke a single shell command in order to import, NLE process and encode a set of clips, such as:
makeavi [wildcard] [codecsettings]
If another encoder is used , the top level script must be modified appropriately to account for the different command line options needed by the encoder.