A Script to Remux MKV Videos Into MP4 Videos

by Phil Dufault on March 11, 2008 · 46 comments

in Gentoo,Linux,Ubuntu

I’ve written a script that dumps the audio and video from an mkv file, encodes the audio into a two channel AAC stream, and remuxes the audio/video into a MP4 wrapper so it’s playable on the Xbox 360. The script is Linux-only, obviously, written in bash using mplayer, normalize-audio, mencoder, MP4Box, mkvinfo, and mkvextract as tools.

It works pretty well:
phil@phil:~/videos$ time reMuxToMP4 test.mkv
[x] reMuxToMP4 script, v0.5.2, written by Phil Dufault
[x] Contact him at: http://www.dufault.info
[x] Source filename: test.mkv
[x] Destination filename: test.mp4
[x] Video stream found, 624 x 320 @ 23.976 fps, using ffh264
[x] Audio stream found, 2 audio channels, using mad
[x] Muxed in mkv format, source file is 38MB
[x] Dumping the audio...success
[x] Normalizing the audio...success
[x] Encoding the audio to low-complexity AAC...success
[x] Extracting audio from MP4 container (silly neroAacEnc)...success
[x] Extracting the video from the mkv...success
[x] Changing the h264 video stream profile from 5.1 to 4.1...success
[x] Deleting the old video source...success
[x] Moving new video stream to proper filename...success
[x] Muxing video stream at 23.976 fps...success
[x] Muxing the audio stream in...success
[o] All done!

Here’s a link to the source:
remuxtomp4

EDIT: I’ve updated the source to use mp4creator.  Please let me know if this is working with your 360′s.

EDIT: v0.5.3r1 released, contains patches found in comments.

{ 46 comments… read them below or add one }

raz October 13, 2008 at 7:06 pm

Thanks! I love the script (it’s so very convenient). It doesn’t work with subtitles, but for all my other videos it’s perfect! I’m going to try and see if I can remux the subtitles into it on my own, and if I can I’ll post my results.

raz October 13, 2008 at 7:22 pm

Thanks! This script is great (it’s so very convenient)! It doesn’t do subtitles, but I’ll try to patch it and if successful I’ll post my results.

Phil Dufault November 28, 2008 at 4:13 pm

I got this email from someone:
Hi,
I found your remuxtomp4.sh script out on the net and I just wanted to say thanks for writing it. Its beats the pants off any other script I’ve used and actually works on everything! I was sadly using GOTSent under wine before I found your script and was starting to write my own script, but you saved me a ton of time and did a much better job then I would have.

Thanks again!

Feels pretty good! Thanks for anyone out there using my script.

Sam December 3, 2008 at 8:03 am

Hi Phil, so far your script seems to be working… I’ll post back tomorrow once I’ve tested it on my 360 but it looks promising so far…! All the other scripts I’ve tried boned my videos >.<

Jeremy December 4, 2008 at 1:18 pm

Great Script! I’m trying my best to use it, but MP4Box in Ubuntu 8.10 is broken. I’ve even tried compiling from source (gpac 0.4.4, 0.4.5, and CVS!) I’ve modified the script to use mp4creator instead, but have been unsuccessfull. The script executes fine and the file works great on my PC, but no joy on the Xbox360. Any help would be greatly appreciated. I modified the Audio dump and recode function line for dumping with MP4Box to
[code]mp4creator --extract 1 "$DEST.m4a" "$DEST""_track1.aac" >>"$log" 2>&1 &[/code]
and in the mux function I replaced
[code]MP4Box -new "$DEST" -add "$VIDEO_FILE" -add "$AUDIO_FILE" -fps "$FPS" -nosys >>"$log" 2>&1 &[/code]
with the following two lines
[code]mp4creator --create "$VIDEO_FILE" -rate="$FPS" "$DEST" >>"$log" 2>&1 &&
mp4creator --create "$AUDIO_FILE" -rate="$FPS" "$DEST" >>"$log" 2>&1 &
[/code]
Please email me if you come up with a solution.

Jeremy December 5, 2008 at 10:51 am

Upon further testing it seems to be working fine and ushare was just being abit finicky. Now if only Microsoft and Sony would let us just use AC3 5.1 and DTS instead of having to convert to 2CH AAC LC, that would be amazing! :)

Telexen December 21, 2008 at 1:10 am

Hello. I’ve been reading through your thread on this subject on the Ubuntu forums from earlier this year, and I’ve tried this script (as downloaded from the link above) on several videos (MKV x264). All seems to go well, but I don’t have any audio in the video files when it’s done. What could have gone wrong?

Phil Dufault December 21, 2008 at 1:22 am

Should be working fine for you — email me directly with the midentify output, and I’ll see if I can help.

Zigiless December 26, 2008 at 6:30 am

Found a minor typo on line 109
if [ -f ${DEST} ];
your forgot to put quatation marks around ${DEST}
Other then that, great script!
I’m just trying to figure out why I don’t get any audio in the output file.

Phil Dufault December 27, 2008 at 11:10 am

Thanks, fixed. :)

f4rrest January 7, 2009 at 11:25 pm

I had to add this back into the script and install bbe in order to change the h264 level info (you had mentioned here http://ubuntuforums.org/showthread.php?t=684780&page=5 that you were going to do this but it was not in the code you posted here on your blog):

ncecho ” [x] Changing the 5.1 bit to 4.1…”;
bbe -e “r 7 \41″ –output=”$VIDEO_FILE.new” “$VIDEO_FILE” >>”$log” 2>&1 &
pid=$!;progress $pid
ncecho ” [x] Deleting the 5.1 video source…”;
rm -f “$VIDEO_FILE” >>”$log” 2>&1 &
pid=$!;progress $pid
ncecho ” [x] Renaming the new video ext to .h264…”;
mv “$VIDEO_FILE.new” “$VIDEO_FILE” >>”$log” 2>&1 &
pid=$!;progress $pid

Without the above addition, the mkv file I was trying to convert for xbox 360 would just display a blank screen.

ruckerz January 11, 2009 at 3:24 pm

Thanks much for the script! Works perfectly for making mkv -> MP4. Since I’m ultimately importing my movies into itunes, I mod’d the script to allow for 6 channel AAC audio as well as mkfifo so I don’t have to take up too much disk space .

ruckerz January 11, 2009 at 3:26 pm

Oh, also the fact that NeroAacEnc has trouble with accessing large files (>4GB) I had to use mkfifo for my six channel tracks.

Phil Dufault January 12, 2009 at 12:07 pm

Ah, what device are you using for the 6 channel playback?

ruckerz January 12, 2009 at 8:44 pm

I have an HTPC hooked up to my TV, so I plan to play in iTunes, will work on it more this weekend.

Zigiless January 13, 2009 at 6:31 am

Okay, I found my audio extraction fails at the dump-audio–to-wav-file-with-mplayer stage. When I copied that command from the script and tried it on its own(mplayer test.mkv -vo null -vc null -ao “pcm:fast:file=test.wav” -channels 2)
I get a lot of “Too many buffered pts” and then –
“Too many audio packets in the buffer: (4096 in 10485760 bytes).
Maybe you are playing a non-interleaved stream/file or the codec failed?”
I then end up with a 64kb wav file.
This happens with all of my mkv files.
Any suggestions?

Phil Dufault January 13, 2009 at 9:52 am

Hmm, what is the next step in your audio processing? We might be able to skip the dump to the .wav. What final audio codec are you using?

Iain January 20, 2009 at 12:35 am

I got a crash, any idea why?

*snip*

Phil Dufault January 25, 2009 at 7:10 pm

Sorry, not sure — might want to recompile your gpac install.

Iain January 26, 2009 at 10:38 pm

Some ideas for things to add to your script:

Here’s a script that incorporates splitting the file into 3.5GiB videos to avoid MP4Box’s segfaulting and the fact that FAT32 doesn’t support files > 4GiB:

Here’s a script that uses mp4creator instead of MP4Box, because the version of MP4Box in Intrepid segfaults: . Also, it uses mplayer to normalize the audio, eliminating a step and a dependency.

Here’s a PPA that has a newer version of MP4Box (in package gpac) which might not segfault–I haven’t tried it:

Personally, I like your script, Phil, because a) I don’t understand Perl, and b) yours is better commented and smarter than the other shell script.

Iain January 29, 2009 at 1:00 am

I keep trying to post the links, but my comment gets deleted. Oh well, search gpac’s launchpad entry for them.

Zigiless February 27, 2009 at 8:10 am

The script sometimes fails for me on certain files with AAC audio. I found I had to add a few steps to the script:
1)Extract the audio using mkvextract
2)Convert it to wav with the “faad” program
Here are the changes if anyone is interested (add to the audio function instead of the mplayer command):

AUDIOTRACKNUM=$(mkvinfo “$SOURCE” | grep -B2 “Track type: audio”|head -n1|awk -F”: ” ‘{print $2}’)

mkvextract tracks “$SOURCE” “$AUDIOTRACKNUM”:”$DEST.audio”

if [[ $AUDIO_CODEC = "faad" ]];
then
faad -d -o “$DEST.wav” “$DEST.audio”
else
mplayer “$DEST.audio” -vo null -vc null -ao “pcm:fast:file=$DEST.wav” -channels 2 -af volnorm
fi

Thomas Greenwood May 3, 2009 at 4:22 pm

I have found you can remove the bbe part (and the subsequent file manipulation) with the following:

echo -n “)” | dd of=”$VIDEO_FILE” bs=1 seek=7 conv=notrunc >>”$log” 2>&1 &

Seems to create the file correctly but I have not tested on the 360 yet.

Phil Dufault May 3, 2009 at 8:59 pm

Seems interesting, let me know if it’s working for you.

RoB June 4, 2009 at 11:15 am

Hey, great work on the script.

I was just wondering why its set to normalize the audio by default. Is that necessary for some reason? Doesn’t normalizing the audio lose some quality and/or experience?

Just wondering, thanks for the great work.

Phil Dufault June 9, 2009 at 10:13 am

I was having some problems back then with unnormalised audio where one movie would be loud, the other soft, and was getting tired of reaching for the remote.

Ignatius July 4, 2009 at 8:06 am

This works perfectly for my 720p .mkv files that I want to play on my xbox 360. Because I stream from ushare I have to rename the .mp4 files to .mov files, otherwise they won’t play.

However 1080p files turn out flakey, don’t know what’s causing this? I would like to be able to convert 1080p mkv’s as well, can this feature be addressed in the future?

Thanks for the script, I really appreciate it!

Phil Dufault July 16, 2009 at 4:29 pm

Not sure for the reason they’re flakey, can you describe how they’re flakey or how it’s not working?

Bill September 21, 2009 at 5:50 pm

Phil,
Great script. Just wanted to say thanks.

~Bill

Dave Conniff October 31, 2009 at 4:54 am

Hey, I’m not sure if your still looking at this site at all, but I just wanted to report that this script ( which has been a wonderful and useful tool for a long time now ), isn’t working properly every since I upgraded my ubuntu to 9.10….I think there is something wrong with the version of mplayer in the repos or something, because the part of the script where mplayer is supposed to dump out the audio isn’t working, and the result of the script now gives you a perfect mp4 that doesn’t have any audio. The error in the log is thie line repeating over and over:

Starting playback…
Too many buffered pts
Too many buffered pts
Too many buffered pts
….

Just thought I would mention it….I haven’t given much thought into a workaround, although I am sure there must be one

kpd001 November 2, 2009 at 8:23 pm

I also get the same problem with this script in 9.10. It would be great to have this fixed as it is a very useful tool, that I have yet to find a quick, easy alternative for.

Phil Dufault November 3, 2009 at 1:19 pm

Sorry all — I’m no longer using this script anymore, and am not maintaining it further either. If someone wants to fork the script, let me know and I’ll update my posts here.

Dave Connifff November 15, 2009 at 8:17 pm

Hey I had found a solution for the problem (which just used mkvextract for the audio extraction instead of mplayer), but it actually just failed for the first time (hence me checking this forum again)….if i can get it fixed again I may be intrested in forking this, just because it really is the only solution for this issue that easily and consistently works(ed) well…however let me figure out what is going wrong now before I go any further, thanks for writing it to begin with though

Dave Connifff November 15, 2009 at 10:55 pm

as it turned out my old fix was overkill, I’m guessing a parameter used with mplayer has become deprecated, but all you have to do is on line 131 where you see -vc null it should be changed -vc dummy
so there you go

Dave Connifff November 16, 2009 at 12:05 am

for some reason the xbox won’t play the file after that fix, but I have my old fix, and now I just use ffmpeg instead of mplayer, since mplayer is obviously the problem

Thomas December 5, 2009 at 11:44 am

You are right Zigiless, even with me the script failed a few times using the AAC codec.

kpd001 January 8, 2010 at 4:31 pm

If anyone is still trying to get this script to work in 9.10 you need to go to line 131 and replace:

mplayer “$SOURCE” -vo null -vc null -ao “pcm:fast:file=$DEST.wav” -channels 2 >>”$log” 2>&1 &

with:

mplayer “$SOURCE” -vo null -vc null -nocorrect-pts -ao “pcm:fast:file=$DEST.wav” -channels 2 >>”$log” 2>&1 &

ird January 31, 2010 at 9:54 am

Just wanted to say thanks for making this script. It saves me a lot of time doing all this by hand.

Awesome job :)

ird January 31, 2010 at 10:21 am

I actually spoke too soon.

dugger@1i ~/Dexter.Season.4.720p.x264.Complete $ mkv2mp4 dexter.s04e01.proper.720p.hdtv.x264-orenji.mkv
[x] reMuxToMP4 script, v0.5.3, written by Phil Dufault
[x] Contact him at: http://www.dufault.info
[x] Source filename: dexter.s04e01.proper.720p.hdtv.x264-orenji.mkv
[x] Destination filename: dexter.s04e01.proper.720p.hdtv.x264-orenji.mp4
[x] Video stream found, 1280 x 720 @ 23.976 fps, using ffh264
[x] Audio stream found, 6 audio channels, using a52
[x] Muxed in mkv format, source file is 1493MB
[x] Dumping the audio…success
[x] Normalizing the audio…success
[x] Encoding the audio to low-complexity AAC…failed
[i] Showing the last 5 lines from the logfile (/home/dugger/Dexter.Season.4.720p.x264.Complete/log.11:14:39-2010-01-31.txt)…
*************************************************************

ERROR: could not parse WAV file
10579′s retcode: 1
failed

Any ideas?

Phil Dufault February 3, 2010 at 3:53 pm

Patched in latest version. v0.5.3-r1

Dan Walker February 23, 2010 at 5:17 pm

Sorry, after actually reading the rest of the thread I suspect that people have been reporting different audio problems, and other scripts are also experiencing similar issues. My assuming that your work is pieced together from stuff off the internet (in the same way that all of mine is!) is a bit of a logic leap, apologies for that.

So… I think I just found a simple typo on the modified mplayer line – no dollar on the SOURCE variable.

Cheers!

Dot2Kode March 3, 2010 at 8:24 pm

I got it working just fine…Very nice script…I’m using Ubuntu Karmic and the packages I grabbed work perfect with your script. I did add the mkvtoolnix repo’ so I it would grab the latest version’s. http://www.bunkus.org/videotools/mkvtoolnix/downloads.html
then I just grabbed the other’s without adding any other repo’s downloaded the nero program and ran a 720p mkv TV download through your script and it worked perfectly. Many thanks for putting this script together!

p.s. I use ushare to stream to my 360 and is all I had to do was rename the .mp4 to .avi and ushare streamed it just fine like that.

Dot2Kode March 4, 2010 at 4:57 am

I do have one question…Would it be possible to remux the file but instead of dropping the audio down to 2 channel’s you leave it as it is? (5.1) I have my xbox running to my receiver and .avi files that are encoded with 5.1 play in 5.1 through uShare. Is this possible with remuxing them to .mp4?

Gota say it again many many thanks for putting this together and sharing!

Phil Dufault March 8, 2010 at 2:59 pm

At the time I wrote the script, the published Microsoft document[1] specified it’ll only play 2.1 when you’re decoding h264. Looks like this hasn’t been fixed, it’s a little crippling. This was something I was going to write about earlier, I was very annoyed at the time, as I’m running an optical cable to my 5.1 receiver as well, but I wanted a solution to play HD content on my Xbox 360 quickly so I went with two channel audio.

Taylor July 15, 2010 at 7:17 pm

Hey man, the script generates the mp4 video but I have no audio. You said you don’t support this anymore but you were still responding to people’s comments. Any idea what could be up? Thanks.

Phil Dufault July 15, 2010 at 11:44 pm

Hi Taylor! You’ll have to provide the runtime log, information about your distribution and mplayer builds, and anything else that you think is relevant so I can try and debug things. Thanks

Leave a Comment

Previous post:

Next post: