Author Topic: mkv to mp4 .bat  (Read 678 times)

Fabshin

  • Newbie
  • *
  • Posts: 6
mkv to mp4 .bat
« 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



eumagga0x2a

  • Moderator
  • Hero Member
  • *****
  • Posts: 3405
Re: mkv to mp4 .bat
« Reply #1 on: August 23, 2019, 04:54:17 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.

Fabshin

  • Newbie
  • *
  • Posts: 6
Re: mkv to mp4 .bat
« Reply #2 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

eumagga0x2a

  • Moderator
  • Hero Member
  • *****
  • Posts: 3405
Re: mkv to mp4 .bat
« Reply #3 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?

eumagga0x2a

  • Moderator
  • Hero Member
  • *****
  • Posts: 3405
Re: mkv to mp4 .bat
« Reply #4 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.

Fabshin

  • Newbie
  • *
  • Posts: 6
Re: mkv to mp4 .bat
« Reply #5 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

eumagga0x2a

  • Moderator
  • Hero Member
  • *****
  • Posts: 3405
Re: mkv to mp4 .bat
« Reply #6 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?
« Last Edit: August 23, 2019, 08:34:58 PM by eumagga0x2a »

Fabshin

  • Newbie
  • *
  • Posts: 6
Re: mkv to mp4 .bat
« Reply #7 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.

eumagga0x2a

  • Moderator
  • Hero Member
  • *****
  • Posts: 3405
Re: mkv to mp4 .bat
« Reply #8 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?

Code: [Select]
ffmpeg -i 01.mkv -c copy out.mp4

Fabshin

  • Newbie
  • *
  • Posts: 6
Re: mkv to mp4 .bat
« Reply #9 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.

eumagga0x2a

  • Moderator
  • Hero Member
  • *****
  • Posts: 3405
Re: mkv to mp4 .bat
« Reply #10 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).

eumagga0x2a

  • Moderator
  • Hero Member
  • *****
  • Posts: 3405
Re: mkv to mp4 .bat
« Reply #11 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):

Code: [Select]
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:

Code: [Select]
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

eumagga0x2a

  • Moderator
  • Hero Member
  • *****
  • Posts: 3405
Re: mkv to mp4 .bat
« Reply #12 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.
« Last Edit: August 24, 2019, 11:40:27 AM by eumagga0x2a »

Fabshin

  • Newbie
  • *
  • Posts: 6
Re: mkv to mp4 .bat
« Reply #13 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?

eumagga0x2a

  • Moderator
  • Hero Member
  • *****
  • Posts: 3405
Re: mkv to mp4 .bat
« Reply #14 on: August 24, 2019, 01:12:35 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.

Quote
is 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.