TechInfo:SONYハンディCAMのMTSファイルをmp4へ変換する(ffmpeg) by M1 Mac

SONYのハンディカムで動画を録画すると、拡張子がMTS形式の動画ファイルが生成されます。

通常の利用ではmp4形式のほうがコンパクトで扱いやすいので、ffmpegを利用して動画を変換してみました。

ffpmegの導入(macOSの場合)と動画の情報
$ brew install ffmpeg
$ ls -lh ./IMG_20220123_095830.MTS
-rwx------  1 user  staff    23M Jan 23 11:48 ./IMG_20220123_095830.MTS

$ ffprobe ./IMG_20220123_095830.MTS
ffprobe version 4.4 Copyright (c) 2007-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.4_1 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, mpegts, from './IMG_20220123_095830.MTS':
  Duration: 00:00:15.52, start: 1.033367, bitrate: 12465 kb/s
  Program 1
  Stream #0:0[0x1011]: Video: h264 (High) (HDMV / 0x564D4448), yuv420p(top first), 1440x1080 [SAR 4:3 DAR 16:9], 29.97 fps, 59.94 tbr, 90k tbn, 59.94 tbc
  Stream #0:1[0x1100]: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, 5.1(side), fltp, 448 kb/s
  Stream #0:2[0x1200]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090), 1920x1080
動画の変換 (書式: ffmpeg -i 入力ファイル 出力ファイル)
$ ffmpeg -i ./IMG_20220123_095830.MTS ./IMG_20220123_095830.mp4
ffmpeg version 4.4 Copyright (c) 2000-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.4_1 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, mpegts, from './IMG_20220123_095830.MTS':
  Duration: 00:00:15.52, start: 1.033367, bitrate: 12465 kb/s
  Program 1
  Stream #0:0[0x1011]: Video: h264 (High) (HDMV / 0x564D4448), yuv420p(top first), 1440x1080 [SAR 4:3 DAR 16:9], 29.97 fps, 59.94 tbr, 90k tbn, 59.94 tbc
  Stream #0:1[0x1100]: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, 5.1(side), fltp, 448 kb/s
  Stream #0:2[0x1200]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090), 1920x1080
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (ac3 (native) -> aac (native))
Press [q] to stop, [?] for help
[aac @ 0x7ffdc201c200] Using a PCE to encode channel layout "5.1(side)"
[libx264 @ 0x7ffdf2845e00] using SAR=4/3
[libx264 @ 0x7ffdf2845e00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2
[libx264 @ 0x7ffdf2845e00] profile High, level 4.0, 4:2:0, 8-bit
[libx264 @ 0x7ffdf2845e00] 264 - core 161 r3049 55d517b - H.264/MPEG-4 AVC codec - Copyleft 2003-2021 - 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=12 lookahead_threads=2 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=25 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 './IMG_20220123_095830.mp4':
  Metadata:
    encoder         : Lavf58.76.100
  Stream #0:0: Video: h264 (avc1 / 0x31637661), yuv420p(top coded first (swapped)), 1440x1080 [SAR 4:3 DAR 16:9], q=2-31, 29.97 fps, 30k tbn
    Metadata:
      encoder         : Lavc58.134.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
  Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 5.1(side), fltp, 394 kb/s
    Metadata:
      encoder         : Lavc58.134.100 aac
frame=    1 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A speed=   frame=   48 fps=0.0 q=0.0 size=       0kB time=00:00:01.04 bitrate=   0.4kbits/sframe=   66 fps= 62 q=29.0 size=     256kB time=00:00:01.49 bitrate=1404.6kbits/frame=   84 fps= 51 q=29.0 size=     768kB time=00:00:02.11 bitrate=2979.1kbits/frame=  102 fps= 45 q=29.0 size=    1024kB time=00:00:02.68 bitrate=3120.9kbits/frame=  121 fps= 44 q=29.0 size=    1536kB time=00:00:03.32 bitrate=3781.0kbits/frame=  136 fps= 40 q=29.0 size=    1792kB time=00:00:03.84 bitrate=3823.0kbits/frame=  148 fps= 38 q=29.0 size=    2304kB time=00:00:04.24 bitrate=4446.0kbits/frame=  160 fps= 36 q=29.0 size=    2816kB time=00:00:04.62 bitrate=4983.2kbits/frame=  172 fps= 34 q=29.0 size=    3328kB time=00:00:05.05 bitrate=5392.3kbits/frame=  187 fps= 34 q=29.0 size=    3840kB time=00:00:05.52 bitrate=5693.4kbits/frame=  199 fps= 33 q=26.0 size=    4352kB time=00:00:05.95 bitrate=5989.9kbits/frame=  212 fps= 32 q=29.0 size=    5120kB time=00:00:06.40 bitrate=6553.7kbits/frame=  227 fps= 31 q=29.0 size=    5632kB time=00:00:06.86 bitrate=6716.5kbits/frame=  238 fps= 30 q=29.0 size=    5888kB time=00:00:07.23 bitrate=6669.6kbits/frame=  249 fps= 30 q=29.0 size=    6400kB time=00:00:07.63 bitrate=6864.9kbits/frame=  264 fps= 30 q=29.0 size=    6912kB time=00:00:08.12 bitrate=6966.5kbits/frame=  276 fps= 29 q=29.0 size=    7168kB time=00:00:08.51 bitrate=6898.6kbits/frame=  294 fps= 29 q=29.0 size=    7936kB time=00:00:09.10 bitrate=7136.9kbits/frame=  306 fps= 28 q=29.0 size=    8448kB time=00:00:09.49 bitrate=7290.0kbits/frame=  322 fps= 28 q=29.0 size=    9216kB time=00:00:10.04 bitrate=7513.7kbits/frame=  336 fps= 28 q=29.0 size=    9728kB time=00:00:10.51 bitrate=7577.2kbits/frame=  349 fps= 28 q=29.0 size=   10240kB time=00:00:10.94 bitrate=7665.1kbits/frame=  366 fps= 28 q=29.0 size=   10752kB time=00:00:11.52 bitrate=7645.9kbits/frame=  381 fps= 28 q=29.0 size=   11264kB time=00:00:12.03 bitrate=7669.1kbits/frame=  396 fps= 28 q=29.0 size=   11776kB time=00:00:12.50 bitrate=7716.7kbits/frame=  407 fps= 28 q=29.0 size=   12288kB time=00:00:12.88 bitrate=7812.3kbits/frame=  419 fps= 28 q=29.0 size=   12800kB time=00:00:13.24 bitrate=7915.0kbits/frame=  432 fps= 28 q=29.0 size=   13312kB time=00:00:13.71 bitrate=7950.0kbits/frame=  446 fps= 28 q=29.0 size=   13824kB time=00:00:14.20 bitrate=7970.6kbits/frame=  457 fps= 28 q=29.0 size=   14336kB time=00:00:14.54 bitrate=8071.9kbits/frame=  465 fps= 25 q=-1.0 Lsize=   17084kB time=00:00:15.50 bitrate=9023.8kbits/s speed=0.826x
video:16317kB audio:749kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.104848%
[libx264 @ 0x7ffdf2845e00] frame I:7     Avg QP:24.13  size: 96276
[libx264 @ 0x7ffdf2845e00] frame P:138   Avg QP:26.39  size: 61031
[libx264 @ 0x7ffdf2845e00] frame B:320   Avg QP:27.54  size: 23786
[libx264 @ 0x7ffdf2845e00] consecutive B-frames:  4.1%  9.0% 10.3% 76.6%
[libx264 @ 0x7ffdf2845e00] mb I  I16..4: 15.7% 54.8% 29.5%
[libx264 @ 0x7ffdf2845e00] mb P  I16..4:  5.8% 16.1%  7.0%  P16..4: 38.0% 15.3%  8.4%  0.0%  0.0%    skip: 9.5%
[libx264 @ 0x7ffdf2845e00] mb B  I16..4:  0.8%  0.8%  0.5%  B16..8: 42.2%  8.8%  2.5%  direct: 7.5%  skip:37.0%  L0:42.5% L1:45.9% BI:11.7%
[libx264 @ 0x7ffdf2845e00] 8x8 transform intra:53.7% inter:76.1%
[libx264 @ 0x7ffdf2845e00] coded y,uvDC,uvAC intra: 67.8% 33.9% 3.4% inter: 31.4% 13.0% 0.4%
[libx264 @ 0x7ffdf2845e00] i16 v,h,dc,p:  4% 72%  4% 21%
[libx264 @ 0x7ffdf2845e00] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 16% 35% 24%  3%  3%  3%  5%  3%  7%
[libx264 @ 0x7ffdf2845e00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu:  5% 80%  7%  1%  1%  1%  2%  1%  2%
[libx264 @ 0x7ffdf2845e00] i8c dc,h,v,p: 66% 21% 11%  2%
[libx264 @ 0x7ffdf2845e00] Weighted P-Frames: Y:18.1% UV:10.9%
[libx264 @ 0x7ffdf2845e00] ref P L0: 57.2% 19.9% 15.9%  6.2%  0.8%
[libx264 @ 0x7ffdf2845e00] ref B L0: 90.1%  8.6%  1.3%
[libx264 @ 0x7ffdf2845e00] ref B L1: 97.2%  2.8%
[libx264 @ 0x7ffdf2845e00] kb/s:8614.77
[aac @ 0x7ffdc201c200] Qavg: 186.215

生成されたファイルのサイズと情報

$ ls -lh ./IMG_20220123_095830.*
-rwx------  1 user  staff    23M Jan 23 11:48 ./IMG_20220123_095830.MTS
-rwx------  1 user  staff    17M Jan 23 14:18 ./IMG_20220123_095830.mp4

$ $ ffprobe ./IMG_20220123_095830.mp4
ffprobe version 4.4 Copyright (c) 2007-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.4_1 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from './IMG_20220123_095830.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.76.100
  Duration: 00:00:15.52, start: 0.000000, bitrate: 9017 kb/s
  Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1440x1080 [SAR 4:3 DAR 16:9], 8615 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, 6 channels, fltp, 395 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]

細かいチューニング無しで、ファイルサイズが73%に圧縮できました。

M1 macを利用しているので、変換時のfpsは29程度と、録画時間とほぼ同じ時間でエンコードできました。

動画のサイズやコーデックを指定することで更に圧縮できます。

動画の変換:サイズ指定(書式: ffmpeg -i 入力ファイル -vf scale=1280:-1 出力ファイル) 1280の場合
$ ffmpeg -i ./IMG_20220123_102735.MTS -vf scale=1024:-1 -pix_fmt yuv420p ./IMG_20220123_102735.1024.mp4

$ ls -lh ./IMG_20220123_102735.*
-rwx------  1 user  staff    15M Jan 23 14:42 ./IMG_20220123_102735.1024.mp4 → 34%
-rwx------  1 user  staff    28M Jan 23  2022 ./IMG_20220123_102735.1280.mp4 → 63%
-rwx------@ 1 user  staff    39M Jan 23 14:27 ./IMG_20220123_102735.1920.mp4 → 88%
-rwx------@ 1 user  staff    44M Jan 23 11:48 ./IMG_20220123_102735.MTS → 100%

この例では 1920 / 1280/ 1024 の3サイズに変更しましたが、少し確認する用途には 1024で十分でした。

超重要! おまけ:QuickTimeで再生できるmp4形式にするには

上記の方法で動画の変換を行ったのですが、なんとQuickTimeで再生できないではありませんか。。。(泣)

たぶんQuickTimeのaudio codecの判定が厳しいのだと思います。

QuickTimeで再生できるようにするオプションは、以下のとおりです。

(audio track(ac3)をコピーすることで無事解決しました)

方法1 h264の場合
$ ffmpeg -i ./input.MTS -vcodec h264 -c:a copy output.h264.acopy.mp4

方法2 yuv420pの場合
$ ffmpeg -i ./input.MTS -pix_fmt yuv420p -c:a copy output.yuv420p.acopy.mp4

上記の検証は macOS Big Sur 11.5.2 にて行いました。