News:

--

Main Menu

Script error Avidemux 2.7.5

Started by MadsenO, February 24, 2021, 01:52:57 PM

Previous topic - Next topic

MadsenO

Hi there -

With the following script, that hopefully with convert a folderful of .mkv files to .mp4:


#PY  <- Needed to identify #
#

adm = Avidemux()
gui = Gui()
#inputFolder = "/Users/Ole/Desktop/TV Shows/Unconverted"
#outputFolder = "/Users/Ole/Desktop/TV Shows/Converted"
#
#

def convert(filein,folderout):

# -------- begin project --------
    if not adm.loadVideo(filein):
        raise("Cannot load " + filein) #Exception displayed in msgbox popup

    adm.loadVideo(filein)
    adm.videoCodec("x264")
    adm.audioCodec(0, "LavAAC");
    adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "rotation=0", "clockfreq=0")
    filename = basename(filein)
    separator = "/"
    adm.save(folderout + separator + filename)
# --------  end project --------


#
# Main
#
#
inputFolder=gui.dirSelect("Select the source folder")
outputFolder=gui.dirSelect("Select the output folder")
list = get_folder_content(inputFolder,ext)
if(list is None):
    raise("Seemingly no files in " + inputFolder) #Exception displayed in msgbox popup
for i in list:
    convert(i,outputFolder)
print("Done")

I get this error message after choosing the input and output folders:

TinyPy:Exception

Exception: (_tp_dict_get) KeyError: ext
BackTrack:
File: py2bc.py, line 53
File: /Users/Ole/Desktop/BatchMKV2MP4.py, line 35
File: py2bc.py, line 53
File: /Users/Ole/Desktop/BatchMKV2MP4.py, line 35

- I have nu idea what the py2bc.py file is ;)

(and the same error actually happens on a Windows setup)

Can someone work out to point me in a right direction?

eumagga0x2a

#1
You haven't set ext. If you want filter for .mkv files, it should be set to "mkv" before you call get_folder_content() (or you can just use "mkv" as function argument).

I doubt that you want to use raise() rather than just display a warning. Apart from that, you should use the latest nightly which gives you splitext() method, else you do get MP4 files, but with .mkv filename extension:

#PY  <- Needed to identify #
#
ui = Gui()
adm = Avidemux()
ext = "mkv"
sep = "/"
#
# Function to convert an individual video
#
def convert(filein,out):
    if not adm.loadVideo(filein):
        ui.displayError("oops","cannot load "+filein)
        return 0

    adm.videoCodec("x264")
    if adm.audioTracksCount() > 0:
        adm.audioClearTracks()
        adm.audioAddTrack(0)
        adm.audioCodec(0, "LavAAC")
    adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "rotation=0", "clockfreq=0")

    filename = (splitext(filein))[0]
    filename += ".mp4"
    filename = basename(filename)
    return adm.save(out + sep + filename)
#
# Main
#
# -------- select directories --------
inputFolder = ui.dirSelect("Select source folder")
if inputFolder is None:
    ui.displayError("Oops", "No source folder selected")
    return

outputFolder = ui.dirSelect("Select output folder")
if outputFolder is None:
    ui.displayError("Oops", "No output folder selected")
    return

if(inputFolder == outputFolder):
    ui.displayError("Error","Output folder cannot be the same as the input one")
    return

# -------- read content --------
list = get_folder_content(inputFolder, ext)
if list is None:
    ui.displayError("Oops", "No files to convert")
    return

# -------- process --------
total = 0
counter = 0

for i in list:
    total += 1
    counter += convert(i, outputFolder)

if not counter:
    ui.displayInfo("Warning", "No files converted")
    return

if counter == 1:
    ui.displayInfo("Finished", "One file out of " + str(total) + " converted")
    return

ui.displayInfo("Finished", str(counter) + " files out of " + str(total) + " converted")

eumagga0x2a

Below a slightly enhanced version of the above script with a menu to select filename extension to filter the content of the source folder:

#PY  <- Needed to identify #
#
ui = Gui()
adm = Avidemux()
ext = "mkv"
sep = "/"
#
# Function to convert an individual video
#
def convert(filein,out):
    if not adm.loadVideo(filein):
        ui.displayError("oops","cannot load "+filein)
        return 0

    adm.videoCodec("x264")
    if adm.audioTracksCount() > 0:
        adm.audioClearTracks()
        adm.audioAddTrack(0)
        adm.audioCodec(0, "LavAAC")
    adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "rotation=0", "clockfreq=0")

    filename = (splitext(filein))[0]
    filename += ".mp4"
    filename = basename(filename)
    return adm.save(out + sep + filename)
#
# Main
#
# -------- select extension --------
extensions = ["avi","m2ts","mkv","mov","mp4","mpg","ts","vob","webm","wmv"]
mx = len(extensions)

menuExt = DFMenu("Select extension:")
for entry in range(0, mx):
    menuExt.addItem(extensions[entry])
dia = DialogFactory("Filter directory content")
dia.addControl(menuExt)
if not dia.show():
    return

idx = menuExt.index

if idx < 0 or idx >= mx:
    ui.displayError("Oops", "Internal error: invalid menu index")
    return

ext = extensions[idx]

# -------- select input directory --------
inputFolder = ui.dirSelect("Select source folder")
if inputFolder is None:
    ui.displayError("Oops", "No source folder selected")
    return

# -------- read content --------
list = get_folder_content(inputFolder, ext)
if list is None:
    ui.displayError("Oops", "No " + ext + " files found in \"" + inputFolder + "\"")
    return

# -------- select output directory --------
outputFolder = ui.dirSelect("Select output folder")
if outputFolder is None:
    ui.displayError("Oops", "No output folder selected")
    return

if(inputFolder == outputFolder):
    ui.displayError("Error","Output folder cannot be the same as the input one")
    return

# -------- process --------
total = 0
counter = 0

for i in list:
    total += 1
    counter += convert(i, outputFolder)

if not counter:
    ui.displayInfo("Warning", "No files converted")
    return

if counter == 1:
    ui.displayInfo("Finished", "One file out of " + str(total) + " converted")
    return

ui.displayInfo("Finished", str(counter) + " files out of " + str(total) + " converted")

MadsenO

Thanks a bunch - takes it to some new levels.

And with 2.7.7 nightly, it does seem to get away and run 👍🏻👍🏻