Batch file to traverse folders and convert mkv to mp4

Started by lahatte, September 17, 2023, 09:21:26 AM

Previous topic - Next topic

lahatte

Hi,

I have the batch file below which was generated by chatGPT. I have so far only tested to the point of opening one file and doing the conversion.

The problem is that this conversion will take a long time, but if I do it in the avidemux GUI it takes a few seconds using the MP4 muxer option on the left side of the interface.

Does anyone know what would need to be changed in this bat file in order to get the fast conversion?

@echo off
setlocal enabledelayedexpansion

set "AVIDEMUX_PATH=C:\Program Files\Avidemux 2.8 VC++ 64bits\avidemux.exe"
set "SOURCE_FOLDER=F:\Video"

for /r "%SOURCE_FOLDER%" %%f in (*.mkv) do (
    set "input_file=%%~f"
    set "output_file=!input_file:.mkv=.mp4!"
    "%AVIDEMUX_PATH%" --force-alt-h264 --video-codec X264 --audio-codec AAC --output-format MP4v2 --load "!input_file!" --save "!output_file!" --quit
)

echo Conversion completed.
pause

eumagga0x2a

I cannot comment on cmd.exe part of the script, but "--force-alt-h264" is invalid since far over a decade (maybe since 2.6, need to check) and the MP4v2 muxer doesn't exist in VC++ builds and is deprecated elsewhere (it may be revived in the future as libmp4v2 seems to have raised from the dead).

The difference in speed originates from you using copy mode in Avidemux GUI while the script (which also uses the graphical Avidemux application) re-encodes both audio into AAC and especially video into H.264 with default options for the x264 encoder.

lahatte

So how can I change this to copy mode without re-encoding with the bat script? What parameters for avidemux will do that?

Thanks.

eumagga0x2a

First of all, drop "--force-alt-h264" (it is simply invalid). If video and audio tracks in the source are compatible with MP4, then replace "X264" and "AAC" with Copy and "MP4v2" with MP4. You should put the --load option together with its parameter at the first place in the command line after the name of the Avidemux executable (after the variable which expands to the name of the Avidemux executable).

You also might want to replace avidemux.exe with avidemux_cli.exe.

lahatte

Quote from: eumagga0x2a on September 18, 2023, 11:05:05 AMFirst of all, drop "--force-alt-h264" (it is simply invalid). If video and audio tracks in the source are compatible with MP4, then replace "X264" and "AAC" with Copy and "MP4v2" with MP4. You should put the --load option together with its parameter at the first place in the command line after the name of the Avidemux executable (after the variable which expands to the name of the Avidemux executable).

You also might want to replace avidemux.exe with avidemux_cli.exe.

Thanks very much for that. All of your suggestions did the trick except moving the 'load' part to be a parameter on the avidemux command line.


eumagga0x2a

Quote from: lahatte on September 20, 2023, 02:28:21 AMAll of your suggestions did the trick except moving the 'load' part to be a parameter on the avidemux command line.

"%AVIDEMUX_PATH%" --load "!input_file!" --video-codec Copy --audio-codec Copy --output-format MP4 --save "!output_file!" --quit
The reason why the --load option together with its argument must be at the first place in the command line is that Avidemux, depending on its configuration, will reset encoder and output configuration to (custom) default values upon loading a video, thus overriding the options passed to it before --load.

Please note that --output-format <muxer name> invokes this muxer with its default configuration (the same applies to encoders etc.). To pass tailored configuration, you need to put it into a tinyPy script (project script) and pass the path to this script as argument to option --run.