2017年8月20日日曜日

GIFアニメにLGTMロゴを合成する

フレームレート(Delay)を調べる

$ identify -verbose animation.gif | grep "Delay"
 Delay: 20x100

ここで調べたDelay値を再合成時のdelayパラメータに指定する
(この場合は、100分の20秒毎にアニメーション、フレームレート換算すると5fps相当)

GIFファイルをフレーム単位に分割する

$ convert -coalesce animation.gif part%d.png
or 
$ convert +adjoin -coalesce animation.gif conv/%02d.gif
  • +adjoin: 画像分割するためのオプション(無くても分割できるかもしれない)
  • -coalesce: フレーム差分のみの画像が出力されるのを抑制するために指定

LGTMを合成する

LGTMの透過画像を準備しておくこと
$ for i in `seq -w 0 10`; do 
  composite -gravity center -compose over lgtm_320.png  conv/${i}.png tmp/${i}.png ; 
done
  • -gravity center: 位置を指定
  • -compose over image1.png image2.png : image2.pngの上にimage1.pngを重ね合せる

画像からGIFアニメを作成

$ convert -delay 10 -layers optimize tmp/*.png lgtm.gif
  • -delay: 1枚の画像を100分のx秒間表示する. ffmpegコマンド実行時に指定したフレームレートから計算して指定
  • -layers optimize: 最適化オプション.

おまけ: トリミングしたい

convert -crop 285x285+5+15! -resize 300 pngs/*.png  tmp/%d.png
基準座標から5x15pxの位置から285x285pxで画像を切り出し、300x300pxに拡大する

mp4からLGTMを作成する

動画の前処理をしてサイズを落とす

$ ffmpeg -ss 3200 -t 100 -i input.mp4 -an output.mp4
  • ss: 開始位置(sec)
  • t: トリミング期間(sec)
  • an: 音声を出力しない
  • -i: 入力ファイル

動画ファイルからpng画像に変換する

$ ffmpeg -i output.mp4 -an -r 10 -vf crop=406:406:0:157 pngs/%04d.png
 or 
$ ffmpeg -i output.mp4 -an -r 10 -s 320x240 pngs/%04d.png
  • -r : フレームレート(1秒間に何フレーム抽出するか)
  • -vf crop : 動画のフィルタリング
  • -vf crop : 出力結果のトリミング(出力サイズx:y:トリミング開始位置x:y)
  • -s : リサイズ(出力サイズ) : 640x480 等

LGTMを合成する

LGTMの透過画像を準備しておくこと
$ for i in {101..200}; do 
  composite -gravity center -compose over lgtm_320.png  pngs/${i}.png tmp/${i}.png ; 
done
  • -gravity center: 位置を指定
  • -compose over image1.png image2.png : image2.pngの上にimage1.pngを重ね合せる

画像からGIFアニメを作成

$ convert -delay 10 -layers optimize tmp/*.png lgtm.gif
  • -delay: 1枚の画像を100分のx秒間表示する. ffmpegコマンド実行時に指定したフレームレートから計算して指定
  • -layers optimize: 最適化オプション.

(オプション)サイズを調整する

$ convert lgtm.gif -coalesce -colors 64 -geometry 320 opt.gif 
  • -coalesce -colors: 減色処理
  • -geometry: 画像の縦横サイズを変更する. 縦横比は維持されるため長辺のサイズを指定してやる