News:

--

Main Menu

Question about Python script?

Started by justausername, June 23, 2023, 03:11:35 AM

Previous topic - Next topic

justausername

I have attempted to match a python batch file using the example,

https://www.gaelanlloyd.com/blog/batch-processing-video-files-with-avidemux/

however, I keep getting errors all over the place just opening the first one.

All I need to do is this,

Settings are all on default,

Nothing needs to be changed.

When I open them one by one, it works fine.

I just need to process 20 separate video files, into 20 separate files.

No adding, combining, joining, changing, or anything.

They are all in the same folder, the default saved settings are fine, and leaving the default file name that says _edit at the end is also fine. I can change it later.

However, I keep having to open them one by one.

Anything thoughts on how to get around this? Also, they are all .mp4, and I am saving them as .mp4. I think I am overthinking this...

-----------------------------------------------------

I wrote this script to open files in a test folder.

here is the script I wrote.

------------------------------------------------

import os
import subprocess

def process_files_in_folder(folder_path):
    avidemux_path = r"C:\Program Files\Avidemux 2.8 VC++ 64bits\avidemux.exe"  # The actual Avidemux executable path

    file_list = []
    for file_name in os.listdir(folder_path):
        if file_name.endswith(".mp4"):
            file_path = os.path.join(folder_path, file_name)
            file_list.append(file_path)

    if not file_list:
        print("No files found with the '.mp4' extension in the specified folder.")
        return

    for file_path in file_list:
        # Construct the Avidemux command to open and save the file
        avidemux_command = [
            avidemux_path,
            "--load",
            file_path,
            "--save",
            f"{file_path}_processed.mp4"  # Output file name (change extension if needed)
        ]

        # Execute the Avidemux command
        try:
            subprocess.run(avidemux_command, check=True)
            print(f"File '{file_path}' processed successfully.")
        except subprocess.CalledProcessError as e:
            print(f"Error processing file '{file_path}': {e}")

# Usage example
folder_path = r"C:\test"  # The actual folder path
process_files_in_folder(folder_path)

------------------------------------------------------

but it does not work. Any ideas on how I can fix this?


justausername

This is the error I get when I try to run it.

https://ibb.co/fkhqjRj

eumagga0x2a

You try to use a python script for the fully-fledged, external Python interpreter from within Avidemux. Please have a look at topic https://avidemux.org/smif/index.php/topic,20025.msg94521.html as an example how things can be from within Avidemux.

justausername

#3
I have tried every single python script on this that I can find, but I get an error every time I try to run it. Is there a simple one that I could put in that would work that can process files in a folder and I can work off that?

After 4+ hours of trying to read everything I could, I have this code...

#PY <- Needed to identify #
#--automatically built--

from avidemux import Avidemux
from glob import glob

adm = Avidemux()

def process_files_in_folder(folder_path):
    file_list = glob(folder_path + "\*.mp4")

    if not file_list:
        print("No files found with the '.mp4' extension in the specified folder.")
        return

    for file_path in file_list:
        adm.loadVideo(file_path)

        # Perform processing operations here using Avidemux functions
        # For example:
        adm.videoCodec("Copy")
        adm.audioClearTracks()
        adm.setContainer("MP4")

        output_file_path = file_path + "_processed.mp4"  # Output file name (change extension if needed)
        adm.save(output_file_path)
        adm.clearSegments()

        print("File '%s' processed successfully." % file_path)

# Usage example
folder_path = "C:\test"  # Replace with the actual folder path and escape backslashes
process_files_in_folder(folder_path)

However, the error I get when I run it is

Cannot find a demuxer for C:/test/123455555222.py

Any ideas how to fix the code that would process default settings for all the .mp4 files in the folder?

The file opens fine when I hit file, open. It is also a .mp4 file

eumagga0x2a

Please do it the way it is done in the example I linked above. Not via glob.

justausername

I tried it, and it does not work. I got this code to work,

#PY  <- Needed to identify #
#--automatically built--

def process_videos():
    video_folder = "C:/test"  # Replace with the actual folder path containing the videos

    videos = ["test1.mp4", "test2.mp4", "test3.mp4", "test4.mp4", "test5.mp4"]
   
    for video in videos:
        video_path = video_folder + "/" + video
        output_path = video_folder + "/" + video[:-4] + "a.mp4"

        adm = Avidemux()

        if not adm.loadVideo(video_path):
            raise Exception("Failed to load video: " + video_path)

        adm.videoCodec("Copy")
        adm.audioClearTracks()
        adm.setSourceTrackLanguage(0, "und")

        if adm.audioTotalTracksCount() <= 0:
            raise Exception("Cannot add audio track for video: " + video_path)

        adm.audioAddTrack(0)
        adm.audioCodec(0, "copy")

        adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False",
                         "aspectRatio=1", "displayWidth=1280", "rotation=0", "clockfreq=0")

        adm.save(output_path)

        print("Video '" + video + "' processed successfully. Output saved as '" + output_path + "'")

# Usage example
process_videos()




But it doesn't always work. This code only works on one computer and only half the time if that. Keep getting Cannot find a demuxer. Please if there is anything you can help me, I have been at this for over 12+ hours now and I am stuck.

justausername

I am going insanse. I have been this at this all most 24 hours, and still can not get it to work. I did get it to work a few times, but not the way I wanted. This is the code I have now. I am sure it just needs a few tweaks. Please look at it for me and tell me what I am doing wrong.

gui = Gui()
adm = Avidemux()
ext = "mp4"

def convert(filein):   
    if 0 == adm.loadVideo(filein):
        gui.displayError("oops", "cannot load " + filein)
        raise
    print("Done")

def process_videos():
    inputFolder = gui.dirSelect("Select the source folder")
    list = get_folder_content(inputFolder, ext)
   
    if list is None:
        raise Exception("No files found in the selected folder")

    for file in list:
        convert(file)
        output_path = inputFolder + "/" + file[:-4] + "_output.mp4"

        adm.videoCodec("Copy")
        adm.audioClearTracks()
        adm.setSourceTrackLanguage(0, "und")

        if adm.audioTotalTracksCount() <= 0:
            raise Exception("Cannot add audio track for file: " + file)

        adm.audioAddTrack(0)
        adm.audioCodec(0, "copy")

        adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False",
                         "aspectRatio=1", "displayWidth=1280", "rotation=0", "clockfreq=0")

        adm.save(output_path)

        print("File '" + file + "' processed successfully. Output saved as '" + output_path + "'")

# Main
process_videos()


It opens each one, but when attempting to save it says "Muxer can not open" What else can I do? I am pulling my hair out here??????

eumagga0x2a

Quote from: justausername on June 23, 2023, 09:50:25 PMoutput_path = inputFolder + "/" + file[:-4] + "_output.mp4"

inputFolder is an absolute (full) path, as is file, thus you end up with something like "C:/test/C:/test/source-file-without-extension_output.mp4" and the resulting failure to save to an non-existent (and invalid in case of Windows paths) location. You meant it to be

output_path = file[:-4] + "_output.mp4"
if you are fine with using the same directory as input for output.

Quote from: justausername on June 23, 2023, 09:50:25 PMdef process_videos():
    inputFolder = gui.dirSelect("Select the source folder")
    list = get_folder_content(inputFolder, ext)

As done in the example script, it is better to check that inputFolder is not equal None (it will be equal None if the file dialog gets discarded).