I’ve recently been going through a number of old MKV home video files and wanted a quicker way to convert them from MKV to M4V so iTunes could stream them properly to my Apple TV.
I’ve been using Handbrake but the problem with Handbrake is that it will re-process all streams including video and run it through compression again – leading to a loss in quality but more importantly, it takes a long time.
By using FFMPEG, I’ve managed to put together a script that will quickly process the container but leave the source video alone and create the two audio tracks necessary for good Apple TV playback on home cinema equipment.
Using FFMPEG to convert MKV to M4V
Being able to quickly convert MKV to M4V for iTunes has so far proved very useful.
The following process has worked so far for a variety of MKV files that are H.264 and either AAC, AC3 or DTS audio.
I have created a similar batch file for converting FLAC audio files into MP3.
I wanted to achieve the following:
- Quickly convert MKV to M4V
- Ensure the audio tracks are iTunes / Apple TV compatible
- Be able to process a number of files at a time
- Work quickly
I use the following folder structure:
c:\source (The folder I put all my MKV files into)
c:\source\done (The folder the original MKV is moved into after processing)
c:\output (The folder that the complete M4V files are written to)
c:\ffmpeg\bin (The location that FFMPEG is installed in)
It seems that in later versions of FFMPEG a bug has been introduced that ends up marking both Audio tracks as default / enabled. This completely confuses the Apple TV and will prevent the AC3 Audio stream from being detected or played by Home Cinema equipment. After researching I found that by using an earlier version of FFMPEG (in my case v1.2.4 from 2013) the batch file works perfectly again.
The batch file looks like this:
for %%a in ("c:\source\*.mkv") do ( c:\ffmpeg\bin\ffmpeg -i "%%a" -strict experimental -map 0:v -map 0:a:0 -map 0:a:0 -c:v copy -c:a:0 aac -b:a:0 160k -ac:a:0 2 -c:a:1 ac3 -b:a:1 640k -ac:a:1 6 "c:\output\%%~na.m4v"
move "%%a" c:\source\done
There are some key elements to the batch file which are worth discussing a little further.
- Give the batch file a name like convert.bat and save it, you can then run it by double clicking on it or by calling it from a command prompt
- The whole thing is built inside a for do loop, meaning it will run once for every .MKV file it finds in c:\source
- By using the -map feature, we are able to take the primary audio from the source MKV file and create both a stereo AAC track at 160k and a 5.1 AC3 track at 640k which keeps both iTunes and the Apple TV happy
- Once the file has been converted, it is moved from c:\source to c:\source\done
Running these files through Handbrake has been taking about an hour to process, where as by using this batch file – the process takes around 5 minutes and will do every MKV file in the c:\source folder and write it out to c:\output ready for inclusion to iTunes.
Even though I’ve used this for home video files, there is no reason that this won’t work for any MKV that has h.264 video and either AAC or AC3 audio. Ensure you read Ripping Blu-Ray Movies into iTunes for Apple TV for further information on how to get the output file into iTunes and looking good.
FFMPEG can be downloaded from the FFMPEG website.
I’m still learning FFMPEG but already it’s clear it’s a very powerful tool. I suspect this script can be improved further and I welcome your feedback either in the comments.