Python版Processingをやっていると作ったものを誰かに見せたり、SNSでアップしたくなりますよね。
Processingから動画を書き出す方法を確認しました。
まず、円が左右に言ったり来たりする動画を作りました。
code↓
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x=0 | |
d=-1 | |
def setup(): | |
size(500,500) | |
def draw(): | |
background(200) | |
global x,d | |
ellipse(x,height/2,30,30) | |
x=x+10*d | |
if x<0 or x>width: | |
d=d*-1 | |
saveFrame("frames/####.tif") | |
if frameCount>=300: | |
exit() |
code↑
その後、動画を書き出します。
動画の書き出し
書き出すのは、上のcodeの15~17行目の「saveFrame("frames/####.tif")」の部分を入れます。
「if frameCount>=300:」の数字の部分を変更すると動画の長さを調整できます。この場合、300なので5秒の動画が作られます。
実行すると、Processing(pyde)のファイルがあるフォルダに「frames」にたくさんのtifファイルが作成されます。
ムービーメーカー
それを、ツールの中の「ムービーメーカー」で動画を書き出します。
しかし、書き出されるファイルの形式が「mov(QuickTime)」なんです。
Windowsでは確認することさえできません。。
ffmpeg-python
movファイルを変更したいので、ffmpeg-pythonを使います。
ffmpegというツールが有名ですが、そのpython版です。
movファイルと同じところに以下のpyファイルを作成します。
code↓
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ffmpeg | |
stream = ffmpeg.input('./0919a.mov') | |
stream = ffmpeg.output(stream, './0924a_output.mp4') | |
ffmpeg.run(stream) |
code↑
実行するとmp4のファイルが作られます。
Youtubeの画質が荒いのが気になるので調査してみます。