LoginSignup
14
4

More than 5 years have passed since last update.

ruby + cairo + ffmpeg で砂嵐動画を作成する

Last updated at Posted at 2018-07-30

適当なダミー動画が欲しい時にこれでサクッと作成できます。

out.gif

cairo のインストール

$ gem install cairo

Rubyコード

suna.rb
require "cairo"

format = Cairo::FORMAT_ARGB32
# キャンバスの大きさ
width = 400
height = 300

surface = Cairo::ImageSurface.new format, width, height
context = Cairo::Context.new surface

# 1ピクセルの大きさ
cell_height = 2
cell_width = 2

# 解像度
num_cols = width / cell_width
num_rows = height / cell_height

66.times do |n|
  current_col = 0
  current_row = 0

  num_rows.times do |i|
    num_cols.times do |j|
      # ピクセルの座標
      y = i * cell_height
      x = j * cell_width
      # ランダムな色
      r = rand(100) / 100.0
      g = rand(100) / 100.0
      b = rand(100) / 100.0
      context.set_source_rgb r, g, b
      # ピクセルを描画
      context.rectangle x, y, cell_width, cell_height
      context.fill
    end
  end
  # ファイルに書き込み
  surface.write_to_png "out/suna#{format "%03d", n}.png"
end

連番画像を生成

$ ruby suna.rb

ffmpeg のインストール (Homebrew の場合)

$ brew install ffmpeg

ffmpeg で連番画像から動画を作成

$ ffmpeg -r 15 -i ./out/suna%03d.png -r 15 -an -vcodec libx264 -pix_fmt yuv420p out.mp4 
14
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
4