How can I batch cut first minute of multiple video files

Started by Willoo, May 30, 2025, 11:32:15 AM

Previous topic - Next topic

Willoo

Hello

I would like to cut first 2 minute of multiple video files and keep the rest of those video without reencoding (using copy only)
How can I do that easily?

Thank you

eumagga0x2a

Using internal Python-like scripting in Avidemux, you could do the following:

#PY  <- Needed to identify #
#
ui = Gui()
adm = Avidemux()
ed = Editor()
ext = "mp4"
outputs = ["MKV","MP4","MOV"]
sep = "/"
nbsek = 120
#
# Function to process an individual video
#
def convert(filein,outdir,outidx):
    if not adm.loadVideo(filein):
        ui.displayError("Oops","cannot load "+filein)
        return 0

    offset = nbsek * 1000 * 1000

    if ed.getVideoDuration() < offset:
        ui.displayError("Input too short", "Duration is shorter than " + str(int(nbsek)) + " seconds, skipping \"" + filein + "\"")
        return 0

    adm.markerA = offset

    adm.videoCodec("Copy")
    nbTracks = adm.audioTracksCount()
    if nbTracks > 0:
        adm.audioClearTracks()
        for track in range(nbTracks):
            adm.audioAddTrack(track)
            adm.audioCodec(track, "Copy")
    filename = (splitext(filein))[0]
    if outidx == 0:
        adm.setContainer("MKV", "forceAspectRatio=False", "displayWidth=1280", "displayAspectRatio=2", "addColourInfo=False", "colMatrixCoeff=2", "colRange=0", "colTransfer=2", "colPrimaries=2")
        filename += ".mkv"
    elif outidx == 1:
        adm.setContainer("MP4", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "displayWidth=1280", "rotation=0", "clockfreq=0")
        filename += ".mp4"
    elif outidx == 2:
        adm.setContainer("MOV", "muxerType=0", "optimize=1", "forceAspectRatio=False", "aspectRatio=1", "displayWidth=1280", "rotation=0", "clockfreq=0")
        filename += ".mov"
    else:
        ui.displayError("Error", "Invalid output preset index \"" + str(outidx) + "\"")
        return 0
    filename = basename(filename)
    return adm.save(outdir + sep + filename)
#
# Main
#
# -------- select extension, output format and offset --------
extensions = ["avi","m2ts","mkv","mov","mp4","mpg","ts","vob","webm"]
mxExt = len(extensions)

menuExt = DFMenu("Select extension:")
for entry in range(0, mxExt):
    menuExt.addItem(extensions[entry])

mxOut = len(outputs)

menuOut = DFMenu("Select output format:")
for entry in range(0, mxOut):
    menuOut.addItem(outputs[entry])

dialOffset = DFInteger("Select output start in seconds:", 0, 600)
dialOffset.value = nbsek

dia = DialogFactory("Configuration")
dia.addControl(menuExt)
dia.addControl(menuOut)
dia.addControl(dialOffset)
if not dia.show():
    return

idxExt = menuExt.index

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

ext = extensions[idxExt]

idxOut = menuOut.index

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

nbsek = dialOffset.value

# -------- select input directory --------
inputFolder = ui.dirSelect("Select source folder")
if inputFolder is None:
    ui.displayError("Error", "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("Error", "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, idxOut)

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")

Save it as a text file with ".py" as extension. You might need to replace the value of sep with "\\" if you use Windows instead of *nix operating systems.

Avidemux will automatically seek to the last keyframe before the specified time offset (default value: 120 seconds = 2 minutes) when exporting the video. If you need more output formats than MKV, MP4 and MOV, you will need to extend the script accordingly. Please keep in mind that most output formats have some limitations regarding compatibility with particular video and audio codecs.