ruby/sdlで、無理やりyoutube 5

前回の奴の焼き直しだ。
fork,IO.pipe を使ってない。
ruby/IOの周辺は難しいですね。

ruby1.9でSDL::Surface.loadFromIO(io)を使えなかったので
sudo apt-get install libsdl-image1.2-dev
sudo gem1.9.1 install rubysdl
これで使えた。

<追記>
ruby/sdlで、無理やりyoutube 6 で、何とか恰好がついてます。
http://gaziya.exblog.jp/15773949/
こちらです。

#!usr/bin/env ruby

require 'sdl'
require "stringio"

url = "http://www.youtube.com/watch?v=Bsk2ioK0Lrg"
system("youtube-dl #{url} -o /tmp/dl.flv")
flv = `cat /tmp/dl.flv`
info = `ffmpeg -i /tmp/dl.flv 2>&1`
duration = ""
if /Duration: (\d{2}):(\d{2}):(\d{2}).(\d{1})/ =~ info
value = (($1.to_i * 60 + $2.to_i) * 60 + $3.to_i) * 10 + $4.to_i
duration = value * 0.1
end

SDL.init(SDL::INIT_VIDEO|SDL::INIT_AUDIO)
SDL::Mixer.open
system("ffmpeg -i /tmp/dl.flv -f wav - | lame - /tmp/out.mp3")
@bgm = SDL::Mixer::Music.load("/tmp/out.mp3")
IO.popen("ffmpeg -ss 0 -vframes 1 -i - -f image2 - 2> /dev/null", "r+") do |io|
begin
io.write(flv)
rescue
ensure
io.close_write
StringIO.open(io.read){|s| @surface = SDL::Surface.loadFromIO(s)}
end
end
screen = SDL::Screen.open(@surface.w, @surface.h, 32, 0)
SDL::Mixer.play_music(@bgm, 1)
ticks = 0.1
s_ticks = SDL.get_ticks * 0.001
while ticks < duration
while e=SDL::Event.poll
exit if e.kind_of?(SDL::Event::Quit) || (e.kind_of?(SDL::Event::KeyDown) && e.sym == SDL::Key::ESCAPE)
end
IO.popen("ffmpeg -ss #{ticks} -vframes 1 -i - -f image2 - 2> /dev/null", "r+") do |io|
begin
io.write(flv)
rescue
ensure
io.close_write
StringIO.open(io.read){|s| @surface = SDL::Surface.loadFromIO(s)}
end
end
screen.put(@surface, 0, 0)
screen.flip
ticks = SDL.get_ticks * 0.001 - s_ticks
end
[PR]
by gaziya | 2011-06-11 15:58