Extract videos from YouTube and make GIF animations for LGTM from them

(^ Ω ^) Youtube video is here, right?

https://www.youtube.com/watch?v=h3jFRvXFwWA)

Do this ... ( ^ω^) ≡⊃⊂≡

This way

( ^ω^)

things to do

--Download youtube on pytube --Use ffmpeg to convert video to image frame by frame --Use ImageMagick's composite command to overlay LGTM on the converted image --Animated images using ImageMagick's convert command --Recolored GIF animation to reduce size --Upload to lgtm.in

Download youtube on pytube

What is pytube?

Python library for youtube

Installation

$ pip install pytube

Video download

Run according to 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

#Get video information
>>> 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>]

#download
>>> video = yt.get('mp4', '720p')
>>> video.download('/tmp')

Downloading: 'The flirting of the seal and the keeper's uncle was too cute.mp4' (Bytes: 2862856)

ll /tmp
-rw-r--r--  1 kasei_san  wheel  2862856  7 20 15:04 The seal and the keeper's uncle were too cute.mp4

Downloaded

Convert video to image frame by frame using ffmpeg

Installation

$ brew install ffmpeg

Convert video to image frame by frame

$ mkdir pngs
$ ffmpeg -i 01.mp4 -an -r 10 -vf crop=406:406:0:157 pngs/%04d.png

option

-i: Specify input file -an: No audio output -r: Frame rate (how many frames are extracted per second) -vf crop: Trimming output result (output size x: y: trimming start position x: y) -s: Resize (output size): 640x480 etc.

The original video is vertically long, so I trimmed it

Output result

A large number of images are generated in the pngs / directory

Overlay LGTM on the converted image using ImageMagick's composite command

Installation

$ brew install imagemagick

LGTM for synthesis

Prepare a transparent png image

Image composition

Extract some images, not all videos

$ for i in {0200..0220}; do; composite -gravity center -compose over lgtm.png pngs/${i}.png 
$ tmp/${i}.png; done

Synthesis result

Animate an image with ImageMagick's convert command

$ convert -delay 10 -layers optimize tmp/*.png lgtm.gif

-delay x: Display one image for x seconds for 100 minutes -layers optimize: should optimize the image size

Output result

Color reduced GIF animation to reduce size

$ convert lgtm.gif -coalesce -colors 50 lgtm_color50.gif
$ convert lgtm.gif -coalesce -colors 25 lgtm_color25.gif

Size comparison

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

Appearance comparison

Unprocessed

50 colors

25 colors

I think it's better to reduce the color appropriately like the original video

Upload to lgtm.in

You need to publish it somewhere in order to upload it Since tumblr is easy, upload it once and then submit it.

TODO

I want to write a script that does it all at once

reference

-[Unknown modern magic ffmpeg options](http://yosilove.blog.shinobi.jp/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3% 83% A0 / ffmpeg% E3% 81% AE% E3% 82% AA% E3% 83% 97% E3% 82% B7% E3% 83% A7% E3% 83% B3) -Animated GIF conversion of video with ffmpeg and ImageMagick -Automatically search and download YouTube videos with Python --Qiita

Recommended Posts

Extract videos from YouTube and make GIF animations for LGTM from them
LGTM --Compose LGTM images with videos and photos and output GIF animation