Log in

View Full Version : Need help writing a Python macro for avsP


Blue_MiSfit
3rd April 2008, 21:09
Hey folks,

So I'm trying to automate some batch processing - and I'm hoping you can help me out!

My objective is this:
1) Iterate through a directory tree (specifying a top level)
2) For all D2V files encountered, create an avisynth script with a simple MPEG2Source(...). I will add some filters to these scripts later.

I have copied here an example script that does something similar. This is ripped straight out of the avsP Macro manual. I'm just new to Python and have a little trouble wrapping my head around all this.

I think I can figure most of it out, but I'm not sure how to specify creating an AviSynth script for only d2v files, as this script apparently creates a script for _all_ files.


import os

# Get the directory containing source files
dirname = avsp.GetDirectory()

if dirname:
# Generate each of the avisynth scripts
for filename in os.listdir(dirname):
fullname = os.path.join(dirname, filename)
if os.path.isfile(fullname):
# Get the extension-based template string
srctxt = avsp.GetSourceString(fullname)
# Create the script string
scripttxt = srctxt + '\n' + 'Sharpen(1.0)\nInfo()'
# Write the script text to a file
f = open(fullname + '.avs', 'w')
f.write(scripttxt)
f.close()


This script creates an AVS for each file, attempts to auto-detect the file type, and assign the appropriate source filter (very cool), then puts

Sharpen(1.0)
Info()

in the body of each script. It's a start!

Your help is appreciated!

~MiSfit

kumi
3rd April 2008, 21:58
Have you tried fnmatch?

http://docs.python.org/lib/module-fnmatch.html

Blue_MiSfit
3rd April 2008, 22:11
Have not, this is my first ever experience with Python. I'll check it out. Thanks for the pointer!

~MiSfit

Blue_MiSfit
3rd April 2008, 22:37
Got it working perfectly!


# batchCreateScripts
# by Derek Prestegard
# Last Modified 4/3/08
# Based on the example batch script in the AvsP macro documentation
#
# This macro automagically creates AviSynth scripts for all D2Vs in a
# directory tree. These scripts are blank, save the MPEG2Source.
# It then opens these scripts in AvsP

import os
import fnmatch

# Get the directory containing source files
dirname = avsp.GetDirectory()

if dirname:
# Generate each of the avisynth scripts
for filename in os.listdir(dirname):
fullname = os.path.join(dirname, filename)
if os.path.isfile(fullname):
# Only work on D2Vs (for now)
if fnmatch.fnmatch(filename, '*.d2v'):
# Get the extension-based template string
srctxt = avsp.GetSourceString(fullname)
# Create the script string
scripttxt = srctxt + '\n'
# Write the script text to a file
f = open(fullname + '.avs', 'w')
f.write(scripttxt)
f.close()
# Open the script in AvsP
avsp.OpenFile(fullname + '.avs')


Did the job. I had to manually add fnmatch.py from the Python distribution package to AvsP's library.zip.

Thanks! :D

Now all I have to do is write a little python script to automate d2v creation! I imagine it will be similar...

~MiSfit