Closed6

ffmpegでmp4, mkv, gifをanimated avifに変換する

こたこた

Windows 11 23H2 の FFmpeg ver7.0 で行う

❯ ffmpeg -version
ffmpeg version 7.0-full_build-www.gyan.dev Copyright (c) 2000-2024 the FFmpeg developers
built with gcc 13.2.0 (Rev5, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libaribcaption --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libxevd --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxeve --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-dxva2 --enable-d3d11va --enable-d3d12va --enable-ffnvcodec --enable-libvpl --enable-nvdec --enable-nvenc --enable-vaapi --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libcodec2 --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
libavutil      59.  8.100 / 59.  8.100
libavcodec     61.  3.100 / 61.  3.100
libavformat    61.  1.100 / 61.  1.100
libavdevice    61.  1.100 / 61.  1.100
libavfilter    10.  1.100 / 10.  1.100
libswscale      8.  1.100 /  8.  1.100
libswresample   5.  1.100 /  5.  1.100
libpostproc    58.  1.100 / 58.  1.100
こたこた

mp4

変換コマンド

ffmpeg -i sample_mp4.mp4 -c:v libsvtav1 -crf 35 sample_mp4.avif

入力ファイルは https://sample-videos.com/video321/mp4/720/big_buck_bunny_720p_1mb.mp4

sample_mp4.avif
出力

-c:v libsvtav1 -crf 35 なしでも変換できるが遅いlibaomを使用するため、高速なsvt-av1を使用したほうが良い。
なおRTX 4000以降だと -c:v av1_nvenc でハードウェアエンコード可。

mediainfo
General
Complete name                            : .\sample_mp4.avif
Format                                   : avis
Codec ID                                 : avis (avis/avif/msf1/iso8/mif1/miaf/MA1B)
File size                                : 775 KiB

Video
ID                                       : 1
Format                                   : av01
Codec ID                                 : av01
Width                                    : 1 280 pixels
Height                                   : 720 pixels
Display aspect ratio                     : 16:9
Bit depth                                : 8 bits
Color range                              : Limited

Image
ID                                       : 1
Format                                   : AV1
Format/Info                              : AOMedia Video 1
Format profile                           : Main@L3.1
Codec ID                                 : av01
Width                                    : 1 280 pixels
Height                                   : 720 pixels
Display aspect ratio                     : 16:9
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Stream size                              : 899 KiB
Color range                              : Limited
Codec configuration box                  : av1C+av1C
こたこた

mkv

元動画ファイルが壊れている可能性を考えて、別のファイルでも試してみる。
変換コマンドは同上。

入力ファイルは https://filesamples.com/samples/video/mkv/sample_960x540.mkv

sample_mkv2.avif
出力

ちゃんと変換できた。
ということはさっきのファイルがなにかおかしい?

mediainfo
❯ mediainfo .\sample_mkv.avif
General
Complete name                            : .\sample_mkv.avif
Format                                   : avis
Codec ID                                 : avis (avis/avif/msf1/iso8/mif1/miaf/MA1B)
File size                                : 689 KiB

Video
ID                                       : 1
Format                                   : av01
Codec ID                                 : av01
Width                                    : 1 280 pixels
Height                                   : 720 pixels
Display aspect ratio                     : 16:9
Bit depth                                : 8 bits
Color range                              : Limited

Image
ID                                       : 1
Format                                   : AV1
Format/Info                              : AOMedia Video 1
Format profile                           : Main@L3.1
Codec ID                                 : av01
Width                                    : 1 280 pixels
Height                                   : 720 pixels
Display aspect ratio                     : 16:9
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Stream size                              : 765 KiB
Color range                              : Limited
Codec configuration box                  : av1C+av1C
こたこた

gif

video-to-gif-sample.gif
入力

変換コマンド

ffmpeg -i sample_gif.gif -c:v libsvtav1 -crf 35 sample_gif.avif

なんか怒られる…

[libsvtav1 @ 000002aeada1edc0] Error setting encoder parameters: bad parameter (0x80001005)
[vost#0:0/libsvtav1 @ 000002aeada1e840] Error while opening encoder - maybe incorrect parameters such as bit_rate, rate, width or height.
[vf#0:0 @ 000002aeada208c0] Error sending frames to consumers: Invalid argument
[vf#0:0 @ 000002aeada208c0] Task finished with error code: -22 (Invalid argument)
[vf#0:0 @ 000002aeada208c0] Terminating thread with return code -22 (Invalid argument)
[vost#0:0/libsvtav1 @ 000002aeada1e840] Could not open encoder before EOF
[vost#0:0/libsvtav1 @ 000002aeada1e840] Task finished with error code: -22 (Invalid argument)
[vost#0:0/libsvtav1 @ 000002aeada1e840] Terminating thread with return code -22 (Invalid argument)
[out#0/mp4 @ 000002aead9f4d00] Nothing was written into output file, because at least one of its streams received no packets.

原因不明なのでとりあえずlibaomでやってみる。

ffmpeg -i sample_gif.gif sample_gif.avif

sample_gif.avif
出力

これもいけてそう。

mediainfo
❯ mediainfo .\sample_gif.avif
General
Complete name                            : .\sample_gif.avif
Format                                   : avis
Codec ID                                 : avis (avis/avif/msf1/iso8/mif1/miaf/MA1A)
File size                                : 240 KiB

Video
ID                                       : 1
Format                                   : av01
Codec ID                                 : av01
Width                                    : 350 pixels
Height                                   : 197 pixels
Display aspect ratio                     : 16:9
Color space                              : RGB
Bit depth                                : 8 bits
Color range                              : Full
Matrix coefficients                      : Identity

Image
ID                                       : 1
Format                                   : AV1
Format/Info                              : AOMedia Video 1
Format profile                           : High@L2.0
Codec ID                                 : av01
Width                                    : 350 pixels
Height                                   : 197 pixels
Color space                              : RGB
Bit depth                                : 8 bits
Stream size                              : 255 KiB
Color primaries                          : BT.709
Transfer characteristics                 : sRGB/sYCC
Matrix coefficients                      : Identity
Codec configuration box                  : av1C+av1C
こたこた

ファイルサイズ比較

それぞれのサンプルファイルをmp4, mkv, gif, webpに変換したものとファイルサイズを行う。

file name file size (mp4) file size (mkv) file size (gif) file size (webp) file size (avif)
sample_mp4.mp4 1,055,736 1,225,529 20,079,484 6,376,218 793,332
sample_mkv.mkv 1,168,257 1,319,066 14,432,306 4,058,942 774,952
sample_gif.gif 158,890 158,547 962,901 282,868 245,684

元ファイルが動画だとgifとwebpは逆にサイズが増えているが、avifはちゃんと減らしているので良さそう。
逆に元ファイルがgifだとwebp, avifより動画ファイルにしてやったほうが良さそう。
gifは相変わらず容量デカい。

このスクラップは28日前にクローズされました