( ^ω^) ここに youtube の動画があるじゃろ?
https://www.youtube.com/watch?v=h3jFRvXFwWA)
これをこうして…
( ^ω^)
≡⊃⊂≡
こうじゃ
やること
- pytube で、youtube をダウンロード
- ffmpeg を使って、動画をフレーム毎に画像に変換
- ImageMagick の composite コマンドを使って、変換した画像に LGTM を重ねる
- ImageMagick の convert コマンドを使って、画像をGIFアニメ化
- サイズを減らす為に、GIFアニメを減色
- lgtm.in に アップロード
pytube で、youtube をダウンロード
pytube とは?
Python の youtube 用ライブラリ
インストール
$ pip install pytube
動画のダウンロード
Usage Example に沿って実行
$ python
Python 2.7.6 (default, Apr 9 2014, 11:54:50)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pytube import YouTube
>>> yt = YouTube()
>>> yt.url = "https://www.youtube.com/watch?v=h3jFRvXFwWA"
>>> from pprint import pprint
# video の情報を取得
>>> pprint(yt.videos)
[<Video: MPEG-4 Visual (.3gp) - 144p>,
<Video: MPEG-4 Visual (.3gp) - 240p>,
<Video: Sorenson H.263 (.flv) - 240p>,
<Video: H.264 (.mp4) - 360p>,
<Video: H.264 (.mp4) - 720p>,
<Video: VP8 (.webm) - 360p>]
# ダウンロード
>>> video = yt.get('mp4', '720p')
>>> video.download('/tmp')
Downloading: 'アザラシと飼育員のおじさんのいちゃちゃっぷりが可愛すぎた.mp4' (Bytes: 2862856)
ll /tmp
-rw-r--r-- 1 kasei_san wheel 2862856 7 20 15:04 アザラシと飼育員のおじさんのいちゃちゃっぷりが可愛すぎた.mp4
ダウンロードされた
ffmpeg を使って、動画をフレーム毎に画像に変換
インストール
$ brew install ffmpeg
動画をフレーム毎に画像に変換
$ mkdir pngs
$ ffmpeg -i 01.mp4 -an -r 10 -vf crop=406:406:0:157 pngs/%04d.png
オプション
-i : 入力ファイルを指定
-an : 音声は出力しない
-r : フレームレート(1秒間に何フレーム抽出するか)
-vf crop : 出力結果のトリミング(出力サイズx:y:トリミング開始位置x:y)
-s : リサイズ(出力サイズ) : 640x480 等
元動画が縦長なので、トリミングした
出力結果
ImageMagick の composite コマンドを使って、変換した画像に LGTM を重ねる
インストール
$ brew install imagemagick
合成用のLGTM
透過 png の画像を用意する
画像の合成
動画全部ではなく、一部の画像を抽出する
$ for i in {0200..0220}; do; composite -gravity center -compose over lgtm.png pngs/${i}.png
$ tmp/${i}.png; done
合成結果
ImageMagick の convert コマンドを使って、画像をGIFアニメ化
$ convert -delay 10 -layers optimize tmp/*.png lgtm.gif
-delay x : 1枚の画像を100分のx秒間表示する
-layers optimize : 画像サイズを最適化してくれるはず
出力結果
サイズを減らす為に、GIFアニメを減色
$ convert lgtm.gif -coalesce -colors 50 lgtm_color50.gif
$ convert lgtm.gif -coalesce -colors 25 lgtm_color25.gif
サイズ比較
ll -h lgtm*.gif
-rw-r--r-- 1 kasei_san wheel 1.7M 7 20 15:39 lgtm.gif
-rw-r--r-- 1 kasei_san wheel 1.0M 7 20 15:42 lgtm_color25.gif
-rw-r--r-- 1 kasei_san wheel 1.3M 7 20 15:42 lgtm_color50.gif
見た目比較
元動画の感じで適当に色を減らすと良いと思う
lgtm.in にアップロード
アップロードするために、一度どこかで公開する必要がある
tumblr が手軽なので、そちらに一度upしてから、submit する
TODO
一括でやってくれるスクリプトを書きたい