News:

--

Main Menu

Batch file with spaces in file names

Started by NoKnees, August 26, 2024, 11:36:31 AM

Previous topic - Next topic

NoKnees

Hi

I'm trying to change the codec in all of my MP4 files because they refuse to play sound properly on a Samsung tv.  If I change the tract codec to MP3 they play fine.  Apparently. it's a known issue.  Obviously don't want to do them one at a time so came up with this batch file.

However, two things seem to go wrong.  It ignores everything after a space in the file name ie 17 again becomes just 17 and doesn't put the mp4 extension on so it's useless

Secondly, it loads the AVIDEMUX Gui but doesn't quit it once the process is finished so doesn't move on to the next file.  Can anyone help?

The PY file was created using the project script option in the gui.

Convert.cmd

set avidemux="C:\Program Files\Avidemux 2.8 VC++ 64bits\avidemux.exe"
set outputfolder="E:\Video\MP4\convert
set myjob="Convert.py"
for %%f in (*.mp4) do %avidemux% --force-alt-h264 --load "%%f" --run convert.py --save "%1%outputfolder%\%%f.mp4" --quit


Convert.py

adm = Avidemux()
adm.clearSegments()
adm.addSegment(0, 80000, 5248600000)
adm.markerA = 0
adm.markerB = 5248600000
adm.setHDRConfig(1, 1, 1, 1, 0)
adm.videoCodec("Copy")
adm.audioClearTracks()
adm.setSourceTrackLanguage(0,"und")
if adm.audioTotalTracksCount() <= 0:
    raise("Cannot add audio track 0, total tracks: " + str(adm.audioTotalTracksCount()))
adm.audioAddTrack(0)
adm.audioCodec(0, "Lame")
adm.audioSetDrc2(0, 0, 1, 0.001, 0.2, 1, 2, -12)
adm.audioSetEq(0, 0, 0, 0, 0, 880, 5000)
adm.audioSetChannelGains(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
adm.audioSetChannelDelays(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
adm.audioSetChannelRemap(0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8)
adm.audioSetShift(0, 0, 0)
adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "displayWidth=1280", "rotation=0", "clockfreq=0")

eumagga0x2a

#1
You don't need cmd scripting (batch scripting) for that, the internal Python-like scripting in Avidemux should suffice:

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

    adm.videoCodec("Copy")
    if adm.audioTracksCount() > 0:
        adm.audioClearTracks()
        adm.audioAddTrack(0)
        adm.audioCodec(0, "Lame", "bitrate=128", "preset=0", "quality=2", "disableBitReservoir=False")
    adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "rotation=0", "clockfreq=0")

    filename = basename(filein)
    extlength = 0

    while filename[-extlength] != ".":
        extlength += 1
        if extlength >= len(filename):
            extlength = 0
            break

    filename = filename[:-extlength] + "_converted.mp4"
    return adm.save(outdir + sep + filename)
#
# Main
#
# -------- select input directory --------
inputFolder = ui.dirSelect("Select source folder")
if inputFolder is None:
    ui.displayError("Oops", "No source folder selected")
    return 0

# -------- read content --------
fullList = []
lowerList = get_folder_content(inputFolder, "mp4")

if lowerList is not None:
    fullList = lowerList

upperList = get_folder_content(inputFolder, "MP4")

if upperList is not None:
    if not fullList:
        fullList = upperList
    else:
        fullList(extend(upperList))

if not fullList:
    ui.displayError("Oops", "No MP4 files found in \"" + inputFolder + "\"")
    return 0

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

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

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

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

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

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

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

I am not really sure about case matching (i.e. whether get_folder_content(inputFolder, "mp4") lists also files with ".MP4" as extension) on Windows. You will find out. The script is written in assumption that get_folder_content() is case-sensitive on Windows as well.

You could speed up conversion by disabling optimizing for streaming in the parameters for the MP4 muxer (using "optimize=0").

Regarding your command line and project script, the internet says that you need to manipulate the list of delimeters delims. More important, the option --force-alt-h264 doesn't exist since well over a decade (where did you pick it up?). Even more important, you absolutely must remove adm.clearSegments(), adm.addSegment(), adm.markerA and adm.markerB calls from the script as they are specific to a single particular video.

NoKnees

You're a star, thank you.  I'm away at the moment but will give it a go when I am back.  Windows isn't usually case sensitive on functions but I'll bear that in mind if I get any errors.

With regards to the --force-alt-h264 I copied a script from another thread but to be honest I wasn't really sure what I was doing.  I have some programming experience but not in Python so it was easier to create the batch file.  What you have done makes perfect sense.  Python seems to be a mix between VBA and C

NoKnees

I have finally returned home and tested the code but have the error as shown in the attachment  Not sure what py2bc.py refers to but it looks like the get directory function puts in / instead of \

eumagga0x2a

#4
Before I get to testing on Windows, could you please verify that basic Python functionality, which extend() is, is not available with...

a) ...the latest VC++ ("vsWin64") nightly (the one from yesterday)?

b) ...the latest MinGW ("win64") nightly (the one from Mid-August at the moment, if I am not mistaken)?

Definitely not related to directory separators, but I'll check as that can be a separate issue.

Thanks!

Edit: please don't try to use the "win64" build from today, it is broken.

sark

#5
@eumagga .... FYI, I'm using latest nightly. 240804_96bcaa6df64-fflibs 7,0 1 on Windows 7 (The dinosaurs OS) :)

Script works fine for me.

Sark

Edit... Didn't realise there was a 240907 update today. Tested, still works for me. However, opening Avidemux throws up the following error...

You cannot view this attachment.

eumagga0x2a

@sark, Thank you, the last "win64" build was generated in a refreshed build environment. The x265 issue, specific to builds created in that new build environment, has been reported by the project maintainer. Going to look into it ASAP (which may be tomorrow). Please use the VC++ build from yesterday for now, sorry for inconvenience.

NoKnees

#7


Just upgraded to the later version and now the error has changed. 

eumagga0x2a

Same error. Does "later version" mean VC++ (compiled natively on Windows) or MinGW (cross-compiled for Windows on Linux)?

eumagga0x2a

Well, finally, I was able to test on Windows with my own cross-build and yes, no extend() with that build either. Therefore, please replace the relevant section of the script with

if upperList is not None:
    if not fullList:
        fullList = upperList
    else:
        fullList += upperList

The script works fine for me with this change.

NoKnees

Hi
I didn't compile the version I used, i downloaded the executable.  It's V2.8.1

I've tried this again and certainly the error has disappeared.  However, the file source dialogue opens, it permits me to select a directory but the dialogue for the output directory never appears.  It whirrs for a few seconds then shuts the GUI down.  I've attached the py file as a txt file but not sure I have made any mistakes from what i can see.  Would you mind please having a look as i may be being stupid.

Many thanks for your help


NoKnees

I appear to have got it working.
I downloaded 2.8.2 rel 240804 and it all seems to be okay.  I need to test the files with the samsung tv but it looks promising.  Appreciate it's not the latest version but for now it's fine.  Will experiment a bit more later.

eumagga0x2a

You can safely take r240908, the packaging error is resolved. As a bonus, your Avidemux won't mess up mono or multi-channel (i.e. non-stereo) LPCM tracks in loaded MOV files, among some other fixes ;D

NoKnees

Thank you for all of your help.  I've now downloaded Python and going to do one of the online courses.  It's remembering all the syntax. 
Bye for now