Monday, November 10, 2014

MKV to MP4

The following is the procedure I used, including my first failed attempts. Skip to the bottom for the working version.

If you need to convert some mkv video into mp4 so that you can play it on your apple TV, according to superuser, the best way is to just use ffdshow.

ffmpeg -i input.mkv -c:v copy -c:a copy output.mp4

This just changes the container from MKV to MP4. It doesn't re-encode anything. It just changes the wrapping around the video. This way you don't lose quality. On my windows box, I already had ffmpeg in the following locations.

  • C:\Program Files (x86)\Ffmpeg For Audacity\ffmpeg.exe (201)
  • C:\Program Files (x86)\DVD Flick\bin\ffmpeg.exe (2009)

Those looked old so I downloaded it from ffmpeg.org, but that's just the source. They refer you to zeranoe.com for a windows binary. I chose to Download FFmpeg git-064a237 64-bit Static. In both cases I used WinRAR to extract the compressed packages. I dropped the 64bit package into C:/Program Files

cd /d "c:\temp"
"c:\program files\ffmpeg\bin\ffmpeg.exe" -i "c:\temp\game-of-thrones-S04E01.mkv" -c:v copy -c:a copy "c:\temp\game-of-thrones-S04E01.mp4"

It gave the following output:

ffmpeg version N-67501-g064a237 Copyright (c) 2000-2014 the FFmpeg developers
  built on Nov  9 2014 22:52:31 with gcc 4.9.2 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      54. 11.100 / 54. 11.100
  libavcodec     56. 12.100 / 56. 12.100
  libavformat    56. 12.103 / 56. 12.103
  libavdevice    56.  2.100 / 56.  2.100
  libavfilter     5.  2.103 /  5.  2.103
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, matroska,webm, from 'c:\temp\game-of-thrones-S04E01.mkv':
  Metadata:
    encoder         : libebml v1.3.0 + libmatroska v1.4.1
    creation_time   : 2014-05-20 12:43:23
  Duration: 01:00:09.82, start: 0.000000, bitrate: 810 kb/s
    Stream #0:0: Video: h264 (High), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    Stream #0:1(eng): Audio: ac3, 48000 Hz, stereo, fltp, 384 kb/s (default)
[mp4 @ 000000000547f020] track 1: codec frame size is not set
Output #0, mp4, to 'c:\temp\game-of-thrones-S04E01.mp4':
  Metadata:
    encoder         : Lavf56.12.103
    Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 23.98 fps, 16k tbn, 1k tbc (default)
    Stream #0:1(eng): Audio: ac3 ([165][0][0][0] / 0x00A5), 48000 Hz, stereo, 384 kb/s (default)
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=86549 fps=39518 q=-1.0 Lsize=  359154kB time=01:00:09.82 bitrate= 815.1kbits/s
video:187252kB audio:169210kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.754949%

Open iTunes. Select movies. ALT > File > Add File to library. The file now appears under the "Home Videos" heading. It seems to work but won't play. You can skip along the timeline and view static images, but it won't play on it own. It's stuck in the paused state. I guess slhck's advice was wrong.

Searching for "ffmpeg codec frame size is not set", I found zeranoe forum where brs5tettba solved the same problem by specifying -acodec ac3 instead of -acodec copy, so I tried the following:

cd /d "c:\temp"
"c:\program files\ffmpeg\bin\ffmpeg.exe" -i "c:\temp\game-of-thrones-S04E01.mkv" -vcodec copy -acodec ac3 "c:\temp\game-of-thrones-S04E01.mp4"

It gave the following output:

ffmpeg version N-67501-g064a237 Copyright (c) 2000-2014 the FFmpeg developers
  built on Nov  9 2014 22:52:31 with gcc 4.9.2 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      54. 11.100 / 54. 11.100
  libavcodec     56. 12.100 / 56. 12.100
  libavformat    56. 12.103 / 56. 12.103
  libavdevice    56.  2.100 / 56.  2.100
  libavfilter     5.  2.103 /  5.  2.103
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, matroska,webm, from 'c:\temp\game-of-thrones-S04E01.mkv':
  Metadata:
    encoder         : libebml v1.3.0 + libmatroska v1.4.1
    creation_time   : 2014-05-20 12:43:23
  Duration: 01:00:09.82, start: 0.000000, bitrate: 810 kb/s
    Stream #0:0: Video: h264 (High), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    Stream #0:1(eng): Audio: ac3, 48000 Hz, stereo, fltp, 384 kb/s (default)
Output #0, mp4, to 'c:\temp\game-of-thrones-S04E01.mp4':
  Metadata:
    encoder         : Lavf56.12.103
    Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 23.98 fps, 16k tbn, 1k tbc (default)
    Stream #0:1(eng): Audio: ac3 ([165][0][0][0] / 0x00A5), 48000 Hz, stereo, fltp, 192 kb/s (default)
    Metadata:
      encoder         : Lavc56.12.100 ac3
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (ac3 (native) -> ac3 (native))
Press [q] to stop, [?] for help
frame=86549 fps=5160 q=-1.0 Lsize=  274549kB time=01:00:09.81 bitrate= 623.1kbits/s
video:187252kB audio:84605kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.989896%

No error in the output this time, but it still won't play in iTunes. I tried again with -vcodec h264 instead of -vcodec copy, which obviously did a re-encode because it took thirty minutes instead of thirty seconds.

cd /d "c:\temp"
"c:\program files\ffmpeg\bin\ffmpeg.exe" -i "c:\temp\game-of-thrones-S04E01.mkv" -vcodec h264 -acodec ac3 "c:\temp\game-of-thrones-S04E01.mp4"

It gave the following output:

ffmpeg version N-67501-g064a237 Copyright (c) 2000-2014 the FFmpeg developers
  built on Nov  9 2014 22:52:31 with gcc 4.9.2 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      54. 11.100 / 54. 11.100
  libavcodec     56. 12.100 / 56. 12.100
  libavformat    56. 12.103 / 56. 12.103
  libavdevice    56.  2.100 / 56.  2.100
  libavfilter     5.  2.103 /  5.  2.103
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, matroska,webm, from 'c:\temp\game-of-thrones-S04E01.mkv':
  Metadata:
    encoder         : libebml v1.3.0 + libmatroska v1.4.1
    creation_time   : 2014-05-20 12:43:23
  Duration: 01:00:09.82, start: 0.000000, bitrate: 810 kb/s
    Stream #0:0: Video: h264 (High), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    Stream #0:1(eng): Audio: ac3, 48000 Hz, stereo, fltp, 384 kb/s (default)
[libx264 @ 0000000002ba4540] using SAR=1/1
[libx264 @ 0000000002ba4540] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0000000002ba4540] profile High, level 3.1
[libx264 @ 0000000002ba4540] 264 - core 142 r2479 dd79a61 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=23 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'c:\temp\game-of-thrones-S04E01.mp4':
  Metadata:
    encoder         : Lavf56.12.103
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 23.98 fps, 24k tbn, 23.98 tbc (default)
    Metadata:
      encoder         : Lavc56.12.100 libx264
    Stream #0:1(eng): Audio: ac3 ([165][0][0][0] / 0x00A5), 48000 Hz, stereo, fltp, 192 kb/s (default)
    Metadata:
      encoder         : Lavc56.12.100 ac3
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (ac3 (native) -> ac3 (native))
Press [q] to stop, [?] for help
frame=86549 fps= 71 q=-1.0 Lsize=  399086kB time=01:00:09.81 bitrate= 905.7kbits/s
video:312203kB audio:84605kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.573770%
[libx264 @ 0000000002ba4540] frame I:1239  Avg QP:15.59  size: 39306
[libx264 @ 0000000002ba4540] frame P:27894 Avg QP:19.65  size:  7009
[libx264 @ 0000000002ba4540] frame B:57416 Avg QP:23.28  size:  1315
[libx264 @ 0000000002ba4540] consecutive B-frames:  7.0% 11.9%  5.8% 75.4%
[libx264 @ 0000000002ba4540] mb I  I16..4: 19.3% 69.2% 11.5%
[libx264 @ 0000000002ba4540] mb P  I16..4:  2.9%  7.8%  0.2%  P16..4: 34.8%  4.8%  1.8%  0.0%  0.0%    skip:47.7%
[libx264 @ 0000000002ba4540] mb B  I16..4:  0.1%  0.2%  0.0%  B16..8: 21.2%  0.7%  0.1%  direct: 0.2%  skip:77.6%  L0:40.5% L1:57.9% BI: 1.6%
[libx264 @ 0000000002ba4540] 8x8 transform intra:70.8% inter:89.0%
[libx264 @ 0000000002ba4540] coded y,uvDC,uvAC intra: 36.1% 44.3% 7.5% inter: 3.2% 3.6% 0.0%
[libx264 @ 0000000002ba4540] i16 v,h,dc,p: 33% 24% 11% 32%
[libx264 @ 0000000002ba4540] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 30% 18% 28%  4%  4%  5%  4%  4%  4%
[libx264 @ 0000000002ba4540] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 32% 20% 11%  5%  8%  8%  6%  6%  4%
[libx264 @ 0000000002ba4540] i8c dc,h,v,p: 60% 17% 20%  3%
[libx264 @ 0000000002ba4540] Weighted P-Frames: Y:1.6% UV:1.0%
[libx264 @ 0000000002ba4540] ref P L0: 66.0% 12.0% 16.7%  5.3%  0.1%
[libx264 @ 0000000002ba4540] ref B L0: 87.1% 11.4%  1.4%
[libx264 @ 0000000002ba4540] ref B L1: 97.8%  2.2%
[libx264 @ 0000000002ba4540] kb/s:708.50

But it still won't play in iTunes.

Next I tried handbrake which I've used before and which notably, slhck suggested. I ran this on my MAC. All I did was select the MKV file and the defualt output format was MP4 with the H.264 codec and framerate "same as source". This conversion took about thirty minutes. And it worked.

{ "loggedin": false, "owner": false, "avatar": "", "render": "nothing", "trackingID": "UA-36983794-1", "description": "", "page": { "blogIds": [ 517 ] }, "domain": "holtstrom.com", "base": "\/michael", "url": "https:\/\/holtstrom.com\/michael\/", "frameworkFiles": "https:\/\/holtstrom.com\/michael\/_framework\/_files.4\/", "commonFiles": "https:\/\/holtstrom.com\/michael\/_common\/_files.3\/", "mediaFiles": "https:\/\/holtstrom.com\/michael\/media\/_files.3\/", "tmdbUrl": "http:\/\/www.themoviedb.org\/", "tmdbPoster": "http:\/\/image.tmdb.org\/t\/p\/w342" }