Need help with tiny py script for processing all files in a directory

Started by iamjohngalt, February 11, 2021, 01:38:32 AM

Previous topic - Next topic

iamjohngalt

I have tried to follow the examples in the wiki to get a py script working but my ignorance is apparently enough to prevent success. It's my first try at a python script.

As the wiki instructed, I saved the last used settings in a script and merged them into
the sample code with hints from the video mentioned in the wiki.
My goal is to process all the avi files in a selected directory and save them as x265 mkv files in the same directory.

The resulting code operates the loop through loading files as long as I don't include any code to do anything.  When I add that code I either get an error on the 'if' statement (in the convert routine) or a tokenize error on the first line of code after the 'raise' that follows that if statement - depending on the indent used for the code that follows. The wiki code sample indents it at the level of the 'if' (resulting in an invalid statement error on the 'if'), but the code below indents to the level of the 'raise' (resulting in the tokenize error on the 'adm.setPostProc'). I haven't coded with anything that uses tab positioning to form blocks of code, so that is a bit confusing for me.
I would appreciate assistance getting this working. I'm using Win7 release 2.7.3.
Code follows.  Thanks in advance.
#PY  <- Needed to identify #

adm = Avidemux()
gui = Gui()

ext="avi"
sep = "\\"

inputFolder=gui.dirSelect("Select the source folder containing files to process:")


def convert(filein):
filename = basename(filein)
dir = dirname(filein)
    if(0 == adm.loadVideo(filein)):
        gui.displayError("oops","cannot load "+filein)
        raise

adm.setPostProc(3, 3, 0)
adm.videoCodec("x265", "useAdvancedConfiguration=False", "general.params=AQ=20", "general.poolThreads=99", "general.frameThreads=0", "general.preset=faster", "general.tuning=none",\
"general.profile=main", "level=-1", "vui.sar_height=1", \
"vui.sar_width=1", "MaxRefFrames=3", "MinIdr=25", "MaxIdr=250", "i_scenecut_threshold=40", "MaxBFrame=3", "i_bframe_adaptive=1", "i_bframe_bias=0", "i_bframe_pyramid=2", \
"b_deblocking_filter=True", "interlaced_mode=0", "constrained_intra=False", "lookahead=40", "weighted_pred=2", "weighted_bipred=True", "cb_chroma_offset=0", "cr_chroma_offset=0", \
"me_method=3", "me_range=16", "subpel_refine=5", "trellis=1", "psy_rd=1.000000", "fast_pskip=True", "dct_decimate=True", "noise_reduction=0", "noise_reduction_intra=0", \
"noise_reduction_inter=0", "strong_intra_smoothing=True", "ratecontrol.rc_method=0", "ratecontrol.qp_constant=0", "ratecontrol.qp_step=4", "ratecontrol.bitrate=0", \
"ratecontrol.rate_tolerance=1.000000", "ratecontrol.vbv_max_bitrate=0", "ratecontrol.vbv_buffer_size=0", "ratecontrol.vbv_buffer_init=1", "ratecontrol.ip_factor=1.400000", \
"ratecontrol.pb_factor=1.300000", "ratecontrol.aq_mode=2", "ratecontrol.aq_strength=1.000000", "ratecontrol.cu_tree=True", "ratecontrol.strict_cbr=False")
adm.audioClearTracks()
adm.setSourceTrackLanguage(0,"und")
adm.audioAddTrack(0)
adm.audioCodec(0, "copy");
adm.audioSetDrc(0, 0)
adm.audioSetShift(0, 0, 0)
adm.setContainer("MKV", "forceDisplayWidth=False", "displayWidth=1280", "displayAspectRatio=0")
adm.save(dir+sep+filename+".x265.mkv")
#
# Main
#
#

list=get_folder_content(inputFolder,ext)
if(list is None):
    raise
for i in list:
        convert(i)
Print("Done")



butterw

your indentation is wrong in the convert function. adm.setPostProc(3, 3, 0) should be at the same level as the preceding if.

In python indentation is an element of syntax.
You can use spaces or tabs for indentation but you can't mix, 4 spaces is probably the most commonly used.

iamjohngalt

Quote from: butterw on February 11, 2021, 12:02:13 PMyour indentation is wrong in the convert function. adm.setPostProc(3, 3, 0) should be at the same level as the preceding if.

In python indentation is an element of syntax.
You can use spaces or tabs for indentation but you can't mix, 4 spaces is probably the most commonly used.

Hi, butterw,
Thanks for your reply and the explanation. Now I know how it should be done
and that the indents can't be mixed, I can display the hidden spaces and tabs
in my editor to check it.
That resolves the issue as the indents had mixed spaces (from the wiki code)
and tabs from my editing of the inserted last used script settings.
I guess my next move is to look for a good IDE to use for Python coding
so I don't make this mistake again.

After fixing that inconsistency, the script is working very nicely.
Thanks again!

butterw

for python, I would recommend using a text editor with syntax highlighting, line numbers and the option to auto-replace tabs with ex:4 spaces.