Avidemux Forum

Avidemux => Windows => Topic started by: Fabshin on August 23, 2019, 04:00:54 PM

Title: mkv to mp4 .bat
Post by: Fabshin on August 23, 2019, 04:00:54 PM
i want to create a .bat file to mux all files in a folder to mp4 using the mp4v2 muxer.
i only could get it to work with the mp4 muxer.

this is what i used:

set avidemux="C:\Program Files\Avidemux 2.7 - 64 bits\avidemux.exe"
set videocodec=Copy
set audiocodec=Copy
set outfmt=MP4
for %%f in (*.mkv) do %avidemux% --load "%%f" -video-codec %videocodec% --audio-codec %audiocodec% --output-format %outfmt% --save "%%~nf.mp4" --quit


Title: Re: mkv to mp4 .bat
Post by: eumagga0x2a on August 23, 2019, 04:54:17 PM
Quote from: Fabshin on August 23, 2019, 04:00:54 PM
i want to create a .bat file to mux all files in a folder to mp4 using the mp4v2 muxer.

Don't use MP4v2, it is obsolete (a long dead project), deprecated and not present in VC++ builds, your script is correct in the way that it uses the MP4 (a very current libavformat) muxer. The MP4 muxer has now all the features which could have been a reason to prefer libmp4v2 over libavformat in earlier days.
Title: Re: mkv to mp4 .bat
Post by: Fabshin on August 23, 2019, 05:01:54 PM
my problem using the mp4 muxer is, that in the muxed videos, the audio stutters.

and thx for the quick reply
Title: Re: mkv to mp4 .bat
Post by: eumagga0x2a on August 23, 2019, 05:13:21 PM
Is there more than a single audio track? Could you please provide a sample mkv (as short as possible to be able to reproduce the issue) via WeTransfer (no email address required!), Mega, Dropbox or Google Drive?
Title: Re: mkv to mp4 .bat
Post by: eumagga0x2a on August 23, 2019, 05:30:02 PM
If uploading a sample is impossible due to the nature of the video, the output of MediaInfo tool might help to understand what is going on.
Title: Re: mkv to mp4 .bat
Post by: Fabshin on August 23, 2019, 08:12:51 PM
all of the mkvs have 3 audio tracks. but the stutter stays even if i remove the 2nd and 3rd track in avidemux before remuxing.
https://drive.google.com/file/d/1CzV-QSSM2GZHOI0371C35XJJ0W1Ygkoa/view?usp=sharing
Title: Re: mkv to mp4 .bat
Post by: eumagga0x2a on August 23, 2019, 08:29:57 PM
WFM, no stuttering when saving in copy mode to mp4 using the MP4 muxer.

edit: what equipment or software would play the resulting .mp4 file with stutter? Does enabling or disabling of moving the index to the start of the file affect the problem?
Title: Re: mkv to mp4 .bat
Post by: Fabshin on August 23, 2019, 08:37:01 PM
davinci resolve, and the stutter stays after reencode for youtube.
no stutter with mp4v2 for some reason.
Title: Re: mkv to mp4 .bat
Post by: eumagga0x2a on August 23, 2019, 08:41:37 PM
Does moving the moov atom (the index) to the start of the file have any effect?

Looks like a davinci problem so far. Does using ffmpeg instead of Avidemux produce mp4 files with better compatibility?

ffmpeg -i 01.mkv -c copy out.mp4
Title: Re: mkv to mp4 .bat
Post by: Fabshin on August 24, 2019, 10:09:02 AM
where do i put these commands, sorry i'm a coding noob :v
took me days to get it semi working.
Title: Re: mkv to mp4 .bat
Post by: eumagga0x2a on August 24, 2019, 10:22:25 AM
In the cmd.exe window assuming ffmpeg.exe is present and its location is listed in the list of directories contained in the user environment variable PATH (or you "cd /d" to the folder where ffmpeg.exe is).
Title: Re: mkv to mp4 .bat
Post by: eumagga0x2a on August 24, 2019, 10:33:42 AM
VLC dumps the structure of the mp4 container beautifully. For the output of the MP4 muxer in Avidemux from saving the 01.mkv in copy mode (I've set the language of the first audio track to "English" and disabled other audio tracks):

mp4 stream debug: dumping root Box "root"
mp4 stream debug: |   + ftyp size 32 offset 0
mp4 stream debug: |   + moov size 53396 offset 32
mp4 stream debug: |   |   + mvhd size 108 offset 40
mp4 stream debug: |   |   + trak size 36349 offset 148
mp4 stream debug: |   |   |   + tkhd size 92 offset 156
mp4 stream debug: |   |   |   + edts size 48 offset 248
mp4 stream debug: |   |   |   |   + elst size 40 offset 256
mp4 stream debug: |   |   |   + mdia size 36201 offset 296
mp4 stream debug: |   |   |   |   + mdhd size 32 offset 304
mp4 stream debug: |   |   |   |   + hdlr size 45 offset 336
mp4 stream debug: |   |   |   |   + minf size 36116 offset 381
mp4 stream debug: |   |   |   |   |   + vmhd size 20 offset 389
mp4 stream debug: |   |   |   |   |   + dinf size 36 offset 409
mp4 stream debug: |   |   |   |   |   |   + dref size 28 offset 417
mp4 stream debug: |   |   |   |   |   |   |   + url  size 12 offset 433
mp4 stream debug: |   |   |   |   |   + stbl size 36052 offset 445
mp4 stream debug: |   |   |   |   |   |   + stsd size 184 offset 453
mp4 stream debug: |   |   |   |   |   |   |   + avc1 size 168 offset 469
mp4 stream debug: |   |   |   |   |   |   |   |   + avcC size 66 offset 555
mp4 stream debug: |   |   |   |   |   |   |   |   + pasp size 16 offset 621
mp4 stream debug: |   |   |   |   |   |   + stts size 1056 offset 637
mp4 stream debug: |   |   |   |   |   |   + stss size 48 offset 1693
mp4 stream debug: |   |   |   |   |   |   + ctts size 12840 offset 1741
mp4 stream debug: |   |   |   |   |   |   + stsc size 10864 offset 14581
mp4 stream debug: |   |   |   |   |   |   + stsz size 6432 offset 25445
mp4 stream debug: |   |   |   |   |   |   + stco size 4620 offset 31877
mp4 stream debug: |   |   + trak size 16833 offset 36497
mp4 stream debug: |   |   |   + tkhd size 92 offset 36505
mp4 stream debug: |   |   |   + edts size 36 offset 36597
mp4 stream debug: |   |   |   |   + elst size 28 offset 36605
mp4 stream debug: |   |   |   + mdia size 16697 offset 36633
mp4 stream debug: |   |   |   |   + mdhd size 32 offset 36641
mp4 stream debug: |   |   |   |   + hdlr size 45 offset 36673
mp4 stream debug: |   |   |   |   + minf size 16612 offset 36718
mp4 stream debug: |   |   |   |   |   + smhd size 16 offset 36726
mp4 stream debug: |   |   |   |   |   + dinf size 36 offset 36742
mp4 stream debug: |   |   |   |   |   |   + dref size 28 offset 36750
mp4 stream debug: |   |   |   |   |   |   |   + url  size 12 offset 36766
mp4 stream debug: |   |   |   |   |   + stbl size 16552 offset 36778
mp4 stream debug: |   |   |   |   |   |   + stsd size 106 offset 36786
mp4 stream debug: |   |   |   |   |   |   |   + mp4a size 90 offset 36802
mp4 stream debug: |   |   |   |   |   |   |   |   + esds size 54 offset 36838
mp4 stream debug: |   |   |   |   |   |   + stts size 7112 offset 36892
mp4 stream debug: |   |   |   |   |   |   + stsc size 28 offset 44004
mp4 stream debug: |   |   |   |   |   |   + stsz size 4624 offset 44032
mp4 stream debug: |   |   |   |   |   |   + stco size 4620 offset 48656
mp4 stream debug: |   |   |   |   |   |   + sgpd size 26 offset 53276
mp4 stream debug: |   |   |   |   |   |   + sbgp size 28 offset 53302
mp4 stream debug: |   |   + udta size 98 offset 53330
mp4 stream debug: |   |   |   + meta size 90 offset 53338
mp4 stream debug: |   |   |   |   + hdlr size 33 offset 53350
mp4 stream debug: |   |   |   |   + ilst size 45 offset 53383
mp4 stream debug: |   |   |   |   |   + ctoo size 37 offset 53391
mp4 stream debug: |   |   |   |   |   |   + data size 29 offset 53399
mp4 stream debug: |   + free size 8 offset 53428
mp4 stream debug: |   + mdat size 64802401 offset 53436
mp4 demux debug: ISO Media (isom) version 512.
mp4 demux debug: found 2 tracks
mp4 demux warning: elst box found
mp4 demux debug:    - [0] duration=17ms media time=-1ms) rate=1.0
mp4 demux debug:    - [1] duration=26716ms media time=17ms) rate=1.0
mp4 demux debug: track[Id 0x1] read 1151 chunk
mp4 demux warning: STTS table of 130 entries
mp4 demux warning: CTTS table of 1603 entries
mp4 demux debug: track[Id 0x1] read 1603 samples length:26s
main input debug: selecting program id=0
mp4 demux debug: adding track[Id 0x1] video (enable) language undef
mp4 demux warning: elst box found
mp4 demux debug:    - [0] duration=26715ms media time=0ms) rate=1.0
mp4 demux debug: track[Id 0x2] read 1151 chunk
mp4 demux warning: STTS table of 887 entries
mp4 demux debug: track[Id 0x2] read 1151 samples length:26s
mp4 demux debug: adding track[Id 0x2] audio (enable) language eng


The same for the MP4v2 muxer with streaming optimization enabled:

mp4 stream debug: dumping root Box "root"
mp4 stream debug: |   + ftyp size 24 offset 0
mp4 stream debug: |   + moov size 28454 offset 24
mp4 stream debug: |   |   + mvhd size 108 offset 32
mp4 stream debug: |   |   + iods size 24 offset 140
mp4 stream debug: |   |   + trak size 23029 offset 164
mp4 stream debug: |   |   |   + tkhd size 92 offset 172
mp4 stream debug: |   |   |   + mdia size 22929 offset 264
mp4 stream debug: |   |   |   |   + mdhd size 32 offset 272
mp4 stream debug: |   |   |   |   + hdlr size 33 offset 304
mp4 stream debug: |   |   |   |   + minf size 22856 offset 337
mp4 stream debug: |   |   |   |   |   + vmhd size 20 offset 345
mp4 stream debug: |   |   |   |   |   + dinf size 36 offset 365
mp4 stream debug: |   |   |   |   |   |   + dref size 28 offset 373
mp4 stream debug: |   |   |   |   |   |   |   + url  size 12 offset 389
mp4 stream debug: |   |   |   |   |   + stbl size 22792 offset 401
mp4 stream debug: |   |   |   |   |   |   + stsd size 168 offset 409
mp4 stream debug: |   |   |   |   |   |   |   + avc1 size 152 offset 425
mp4 stream debug: |   |   |   |   |   |   |   |   + avcC size 66 offset 511
mp4 stream debug: |   |   |   |   |   |   + stts size 32 offset 577
mp4 stream debug: |   |   |   |   |   |   + stsz size 6432 offset 609
mp4 stream debug: |   |   |   |   |   |   + stsc size 40 offset 7041
mp4 stream debug: |   |   |   |   |   |   + co64 size 3224 offset 7081
mp4 stream debug: |   |   |   |   |   |   + ctts size 12840 offset 10305
mp4 stream debug: |   |   |   |   |   |   + stss size 48 offset 23145
mp4 stream debug: |   |   + trak size 5285 offset 23193
mp4 stream debug: |   |   |   + tkhd size 92 offset 23201
mp4 stream debug: |   |   |   + mdia size 5163 offset 23293
mp4 stream debug: |   |   |   |   + mdhd size 32 offset 23301
mp4 stream debug: |   |   |   |   + hdlr size 33 offset 23333
mp4 stream debug: |   |   |   |   + minf size 5090 offset 23366
mp4 stream debug: |   |   |   |   |   + smhd size 16 offset 23374
mp4 stream debug: |   |   |   |   |   + dinf size 36 offset 23390
mp4 stream debug: |   |   |   |   |   |   + dref size 28 offset 23398
mp4 stream debug: |   |   |   |   |   |   |   + url  size 12 offset 23414
mp4 stream debug: |   |   |   |   |   + stbl size 5030 offset 23426
mp4 stream debug: |   |   |   |   |   |   + stsd size 106 offset 23434
mp4 stream debug: |   |   |   |   |   |   |   + mp4a size 90 offset 23450
mp4 stream debug: |   |   |   |   |   |   |   |   + esds size 54 offset 23486
mp4 stream debug: |   |   |   |   |   |   + stts size 24 offset 23540
mp4 stream debug: |   |   |   |   |   |   + stsz size 4620 offset 23564
mp4 stream debug: |   |   |   |   |   |   + stsc size 40 offset 28184
mp4 stream debug: |   |   |   |   |   |   + co64 size 232 offset 28224
mp4 stream debug: |   |   |   + udta size 22 offset 28456
mp4 stream debug: |   |   |   |   + name size 14 offset 28464
mp4 stream debug: |   + mdat size 64801969 offset 28478
mp4 demux debug: unrecognized major media specification (mp42).
mp4 demux debug: found 2 tracks
mp4 demux debug: track[Id 0x1] read 401 chunk
mp4 demux warning: STTS table of 2 entries
mp4 demux warning: CTTS table of 1603 entries
mp4 demux debug: track[Id 0x1] read 1603 samples length:26s
main input debug: selecting program id=0
mp4 demux debug: adding track[Id 0x1] video (enable) language undef
mp4 demux debug: track[Id 0x2] read 27 chunk
mp4 demux warning: STTS table of 1 entries
mp4 demux debug: track[Id 0x2] read 1150 samples length:26s
mp4 demux debug: adding track[Id 0x2] audio (enable) language eng
Title: Re: mkv to mp4 .bat
Post by: eumagga0x2a on August 24, 2019, 11:13:06 AM
I compared also the result from saving the sample in copy mode with ffmpeg, and the structure is as expected very similar to the output of Avidemux (no wonder, both use the libavformat library, a part of ffmpeg). The first obvious difference is a different strategy in splitting data into chunks (very small chunks = good interleaving with libavformat, big chunks in the output of libmp4v2). No idea whether this matters.

By the way, a brief search for "davinci resolve audio stuttering" finds quite a lot of complaints, some reporting the issue to be solved by chaning the sampling frequency from 44100 Hz to 48000. You might try to re-encode audio with the appropriate "resample" audio filter enabled.
Title: Re: mkv to mp4 .bat
Post by: Fabshin on August 24, 2019, 12:58:34 PM
i recorded a clip 48 khz and no stutter... now i feel like an moron :3

thank you for helping

is there anything i could change for the better in the script i posted?
Title: Re: mkv to mp4 .bat
Post by: eumagga0x2a on August 24, 2019, 01:12:35 PM
Quote from: Fabshin on August 24, 2019, 12:58:34 PM
i recorded a clip 48 khz and no stutter... now i feel like an moron :3

This is rather a clear pointer to a bug in davinci resolve.

Quoteis there anything i could change for the better in the script i posted?

I never felt a need to learn Windows cmd.exe scripting, so while the script looks good for me (especially the order of Avidemux options in the call is correct, which is very important), I can't tell for sure whether your script is able to handle spaces and similar pitfalls in file names. Please ask user "dosdan" for competent advice.

You should also be aware that the MP4 muxer supports less codecs than the Matroska demuxer, so the remuxing may fail if the source mkv contains a video or audio stream compressed with an unusual codec or contains 24 bits audio.
Title: Re: mkv to mp4 .bat
Post by: dosdan on August 26, 2019, 12:37:41 AM
Using ADM for the batching of simple Copy |  Copy stuff like this is really kludgy.  It's more straight-forward to do it in FFMPEG. This is a very powerful program that can handle multiple input and output files. However, here we'll be using single input and output files.  Assuming you either have ffmpeg.exe in the current directory or in your Environmental PATH list of directories, and using your supplied MKV file:

ffmpeg -i 01.mkv -hide_banner -c copy -y test.mp4

Note: when operating at the CMD prompt in Windows, you can type one or more starting letters of a filename and then press the Tab key one or more times to find matches. This saves time typing filenames. You can also use the Up Arrow key to recycle through previous command limes, which you can reuse directly, or edit first.

Here, "-i" is followed by the input filename.
"-hide_banner" hides a lot of FFMEG compilation info that can otherwise make the output display very wordy.
"-y" is useful when repeating this or similar commands where the output file already exists and being asked "Do you want to overwrite it?"
"-c copy" is to copy (i.e. not re-encode) the major video & audio stream.
"test.mp4"  specifies both the output filename and the output container format (MP4).

Result:
Input #0, matroska,webm, from '01.mkv':
  Metadata:
    ENCODER         : Lavf57.84.100
  Duration: 00:00:26.75, start: 0.000000, bitrate: 19553 kb/s
    Stream #0:0: Video: h264 (High), yuv420p(tv, bt470bg/bt709/bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 60 fps, 60 tbr, 1k tbn, 120 tbc (default)
    Metadata:
      DURATION        : 00:00:26.750000000
    Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp (default)
    Metadata:
      title           : Track1
      DURATION        : 00:00:26.703000000
    Stream #0:2: Audio: aac (LC), 44100 Hz, stereo, fltp (default)
    Metadata:
      title           : Track2
      DURATION        : 00:00:26.703000000
    Stream #0:3: Audio: aac (LC), 44100 Hz, stereo, fltp (default)
    Metadata:
      title           : Track3
      DURATION        : 00:00:26.680000000
Output #0, mp4, to 'test.mp4':
  Metadata:
    encoder         : Lavf58.30.100
    Stream #0:0: Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt470bg/bt709/bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 60 fps, 60 tbr, 16k tbn, 1k tbc (default)
    Metadata:
      DURATION        : 00:00:26.750000000
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp (default)
    Metadata:
      title           : Track1
      DURATION        : 00:00:26.703000000
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 1603 fps=0.0 q=-1.0 Lsize=   63332kB time=00:00:26.70 bitrate=19429.3kbits/s speed= 207x
video:62759kB audio:525kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.077037%


FFMPEG has found 4 streams in the input container (MKV). Like many programs, it counts from zero. So the 4 streams are:
0:0 Video (h264)
0:1 Audio (AAC)
0:2 Audio (AAC)
0:3 Audio (AAC)

Here the "0:1"  means the 2nd stream in the 1st (zeroth) input file.

Note that in the output file there are only 2 streams:
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)


This is because FFMPEG has automatically selected the audio & video streams which it thinks are the most significant. The logic it uses is:
In the absence of any map options for a particular output file, ffmpeg inspects the output format to check which type of streams can be included in it, viz. video, audio and/or subtitles. For each acceptable stream type, ffmpeg will pick one stream, when available, from among all the inputs.

It will select that stream based upon the following criteria:

In the case where several streams of the same type rate equally, the stream with the lowest index is chosen.

But we want all the steams in the output file. The "-map 0" option will do that:

ffmpeg -i 01.mkv -map 0 -hide_banner -c copy -y test.mp4
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
  Stream #0:2 -> #0:2 (copy)
  Stream #0:3 -> #0:3 (copy)


In this example of 1 video & 3 audio streams this is equivalent to specifying any of the following:
-map 0:v -map 0:a
-map 0:v -map 0:a:0 -map 0:a:1 -map 0:a:2
-map 0:v:0 -map 0:a:0 -map 0:a:1 -map 0:a:2


And, since we have only 1 input file, we can drop the "0:" when mapping e.g.
-map v:0 -map a:0 -map a:1 -map a:2

The order that the maps are specified is significant. We could swap the last two audio streams using:
ffmpeg -i 01.mkv -hide_banner -map v -map a:0  -map a:2  -map a:1   -c copy -y -hide_banner test.mkv
or:
ffmpeg -i 01.mkv -hide_banner -map :0 -map :1  -map :3  -map :2   -c copy -y -hide_banner test.mkv
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
  Stream #0:3 -> #0:2 (copy)
  Stream #0:2 -> #0:3 (copy)


Or we could drop tracks and swap their order using:
ffmpeg -i 01.mkv -hide_banner -map :0 -map :3  -map :2 -c copy -y -hide_banner test.mkv
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:3 -> #0:1 (copy)
  Stream #0:2 -> #0:2 (copy)


A batch file to use FFMEG will be presented in my next msg.

Dan.





Title: Re: mkv to mp4 .bat
Post by: dosdan on August 26, 2019, 02:00:21 AM
Convert_MKV_to_MP4 (folder).bat

@echo off
setlocal EnableDelayedExpansion
cls

if exist conversion_error.txt del /q conversion_error.txt

:FILES_LOOP
for %%F  in ("%~1\*.mkv") do (
    echo Converting    "%%~F"    to    "%%~dpFconverted\%%~nF.mp4"
if not exist "%%~dpFconverted\" md "%%~dpFconverted\"
ffmpeg.exe -i "%%~fF" -c copy -y -hide_banner -loglevel error -map 0 "%%~dpFconverted\%%~nF.mp4"
if errorlevel 1 echo error occurred: "%%~F"  >> conversion_error.txt
rem Next line shows the MP4 after it has been created, so with multiple MP4s it acts as a sort of progress indicator.
if exist "%%~dpFconverted\%%~nF.mp4" dir "%%~dpFconverted\%%~nF.mp4" | find "/"
rem Remove REM from the start of the next line if you wish to delete the original file if no error occurred in the conversion.
echo "%%~fF"
rem del "%%~fF"
echo.
shift
)
if not "%1"=="" goto FILES_LOOP
goto FINISHED

:FINISHED
@echo off
if exist conversion_error.txt start /i conversion_error.txt
echo.
echo.
pause



Place a shortcut to this batchfile on your Windows desktop and then drag&drop a folder (not selected files within a folder) onto this icon. All the MKVs within the folder will be converted to MP4s.

Test setup:  a directory called "C:\Filename_test\MKV Test" (note the space in the directory name". This directory contains 3 MKVs:
* 01.mkv (actually a bogus media file. This is a renamed text file to evaluate the error-handling when the batchfile encounters a corrupted (simulated) media file.)
* 0 2.mkv  (a media file with a space in its name).
* 03.mkv  (media file without a space in its name)

Result:
(https://dl.dropbox.com/s/riar7is9jojo1rb/processing%20result.jpg)

The batchfile is designed to continue operating if it encounters errors while processing a folder with thousands of MKV files. Because you may miss this error(s) due to scrolling off the screen, it also pops up a window (conversion_error.txt in the associated program, typically Notepad) at the end, if any errors occurred.

Dan.