-HELP ME -batch replacement of audio tracks in video files-

Started by unity, October 21, 2023, 05:03:29 AM

Previous topic - Next topic

unity

HELP ME -batch replacement of audio tracks in video files-

(I really wish Avidemux had such a solution)

Avidemux has the ability to replace the audio track in a video file with another audio file, but doing this manually with a large number of files is VERY time consuming :-)

Help me write a script!
task
- batch replacement of audio tracks in video files.
(large number of videos: 100, 200, 300 video files)

(I really wish Avidemux had such a solution)
(window in the program) video folder
(window in the program) folder with audio

and prepared by name
video and audio files

-------------------------------------------------- -------------
(window in the program) video folder
01_Avidemux-video .mp4

(window in the program) folder with audio
01_Avidemux-audio .mp3

and Avidemux software simply replaces
in a video file
01_Avidemux-video .mp4
audio track included
01_Avidemux-audio .mp3

also with other videos and audios
by name

02_Avidemux-video .mp4
02_Avidemux-audio .mp3

03_Avidemux-video .mp4
03_Avidemux-audio .mp3

04_Avidemux-video .mp4
04_Avidemux-audio .mp3

--------------------------------------------------
Help me write a script!

------------------------------------------------------------------------
------------------------------------------------------------------------
NB. And the possible embedding of subtitles into all these videos.
------------------------------------------------------------------------
------------------------------------------------------------------------
HELP ADAPT
THIS WROTE
gpt
On the page you provided, https://www.avidemux.org/admWiki/doku.php?id=tutorial:batch_processing
  There is a sample Python code for batch processing video files using Avidemux.
You can copy this code and adapt it to suit your needs.

The code for replacing audio tracks in a large number of video files might look something like this:

-------------------------------------------------- --------------------------
import os

def process_video(input_file, output_file, audio_track):
    cmd = f'avidemux3_cli --nogui --load "{input_file}" --audio-codec copy --audio-track {audio_track} --save "{output_file}"'
    os.system(cmd)

def batch_process_videos(input_directory, output_directory, audio_track):
    for filename in os.listdir(input_directory):
        if filename.endswith(".avi") or filename.endswith(".mp4"):
            input_file = os.path.join(input_directory, filename)
            output_file = os.path.join(output_directory, filename)
            process_video(input_file, output_file, audio_track)

# Usage example:
input_directory = "path_to_source_video files"
output_directory = "path_to_output_video files"
audio_track = 1

batch_process_videos(input_directory, output_directory, audio_track)

-------------------------------------------------- --------------------------
Please keep in mind that you must install Avidemux and its command line components on your computer
  and update the paths to the avidemux3_cli executable file and directories in the code,
  as well as specify the correct audio track numbers in the audio_track variable.

eumagga0x2a

Please do not ask for support by means of personal messages when you are able to post (sometimes the external anti-spam service CleanTalk makes it impossible to post for new users, this is the only valid exception), thank you.

Please ignore the totally outdated Avidemux Wiki, it brings you onto a wrong path. There is an audioAddExternal(str fileName) method in Avidemux() class of internal Python-like scripting in Avidemux, which should help to implement a solution using internal scripting.

Unless someone steps in, I'll try to look into our options on Tuesday or Wednesday next week.


unity

eumagga0x2a
Moderator"I'll try to look into our options on Tuesday or Wednesday next week."

?
need you help !

eumagga0x2a

#4
The following script requires mp4 video files named by pattern of e.g. 01*.mp4, 02*.mp4, 03*.mp4 etc to allow unambiguous sorting by filename with corresponding external mp3 audio tracks following the same pattern placed in a different folder. Output mp4 files will be stored in a third folder, selected by user. Output filenames will be modified by adding "_with_audio" before filename extension.

All filename extensions must be lowercase.

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

    adm.videoCodec("Copy")
    if not adm.audioAddExternal(audin):
        ui.displayError("Error", "Cannot use \"" + audin + "\" as audio track")
        return 0
    nbInternalTracks = adm.audioTracksCount()
    if nbInternalTracks:
        adm.audioClearTracks()
    adm.audioAddTrack(nbInternalTracks)
    adm.audioCodec(nbInternalTracks, "copy")
    filename = (splitext(vidin))[0]
    adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "displayWidth=1280", "rotation=0", "clockfreq=0")
    filename += "_with_audio.mp4"
    filename = basename(filename)
    return adm.save(outdir + sep + filename)
#
# Main
#
# -------- select input video directory --------
vidInputFolder = ui.dirSelect("Select video source folder")
if vidInputFolder is None:
    ui.displayError("Error", "No video source folder selected")
    return 0

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

# -------- read content --------
vidFileList = get_folder_content(vidInputFolder, "mp4")
if vidFileList is None:
    ui.displayError("Oops", "No mp4 files found in \"" + vidInputFolder + "\"")
    return 0

audFileList = get_folder_content(audInputFolder, "mp3")
if audFileList is None:
    ui.displayError("Oops", "No mp3 files found in \"" + audInputFolder + "\"")
    return 0

nbVid = len(vidFileList)
nbAud = len(audFileList)

if nbVid != nbAud:
    ui.displayInfo("Warning", "The count of video and audio files does not match: " + str(nbVid) + " vs " + str(nbAud) + ". Using the lowest.")
    if nbVid > nbAud:
        nbVid = nbAud

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

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

# -------- process --------
total = 0
success = 0

for video in vidFileList:
    success += mux(video, audFileList[total], outputFolder)
    total += 1
    if total >= nbVid:
        break

if not success:
    ui.displayInfo("Warning", "No video files processed")
elif success == 1:
    ui.displayInfo("Finished", "One video file out of " + str(total) + " processed")
else:
    ui.displayInfo("Finished", str(success) + " video files out of " + str(total) + " processed")

return success

On non-Windows platforms, string sep should be set to "/" instead of "\\". The filename extension of the attached script should be changed back from ".txt" to ".py" for ease of use (Avidemux doesn't care about extension, but the file dialog is pre-set to filter out files without ".py" as extension when opening a project script).

NB: Please don't hurry people up who volunteer to contribute their free time free of charge to open source projects – not to mention that I was still perfectly within the promised timespan in my time zone.

eumagga0x2a

I've missed the subtitle part.

Quote from: unity on October 21, 2023, 05:03:29 AMNB. And the possible embedding of subtitles into all these videos.

Do you mean hardcoding subtitles or adding them as a track, which would allow to turn them on and off? The latter is not supported by Avidemux. The former is possible, but mandates re-encoding the video track.

unity

Thanks a lot!!! tested - it works!!!
you are a genius :-)
Ideally, it would be possible to add several similar scripts to tasks in a batch. but how to do this?
avidemux_jobsbatch ( ) there is no possibility of adding, only through avidemux itself (but the script you suggested, you immediately start rendering and cannot be saved for batch conversion, maybe this can be fixed somehow, for batch processing of several scripts?)

but thank you very much for helping!
you are a genius :-)

eumagga0x2a

#7
Avidemux Jobs GUI is a wrong tool. Delayed processing via job queue makes sense for time-consuming encoding operations, not here as copy mode is usually very fast (limited mostly by storage performance).

The point of the suggested tinyPy script is to let user choose directories interactively. If you don't need interactivity, just hardcode full paths (vidInputFolder, audInputFolder, outputFolder) into the script instead of obtaining the values using methods of the Gui() class. You can then pass the path to the script as argument for the --run option of either graphical or command-line Avidemux executable.

unity

Thank you!
you are a genius :-)