Created
October 9, 2012 17:17
-
-
Save pioz/3860147 to your computer and use it in GitHub Desktop.
Simple video player in Ruby
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
# Written by Pioz. | |
# | |
# Deps: | |
# sudo apt-get install libgtk2.0-dev libgstreamer0.10-dev gstreamer0.10-plugins-base | |
# gem install gtk2 gstreamer | |
require 'gtk2' | |
require 'gst' | |
if ARGV.size != 1 | |
puts "Usage: #{$0} <file>" | |
exit 0 | |
end | |
class VideoWidget < Gtk::DrawingArea | |
def initialize(file) | |
super() | |
@pipeline = Gst::ElementFactory.make('playbin') | |
@pipeline.uri = "file://#{File.absolute_path(file)}" | |
@pipeline.video_sink = Gst::ElementFactory.make('xvimagesink') | |
@pipeline.video_sink.force_aspect_ratio = true | |
@pipeline.audio_sink = Gst::ElementFactory.make('autoaudiosink') | |
@pipeline.signal_connect('notify') do | |
@pipeline.video_sink.xwindow_id = self.window.xid | |
@pipeline.video_sink.expose | |
end | |
@pipeline.ready | |
end | |
def play | |
@pipeline.play | |
end | |
def pause | |
@pipeline.pause | |
end | |
def stop | |
@pipeline.stop | |
end | |
def seek(time) | |
@pipeline.seek(1.0, Gst::Format::TIME, | |
Gst::Seek::FLAG_FLUSH | Gst::Seek::FLAG_KEY_UNIT, | |
Gst::Seek::TYPE_CUR, time * Gst::SECOND, | |
Gst::Seek::TYPE_NONE, -1); | |
end | |
end | |
window = Gtk::Window.new | |
video = VideoWidget.new(ARGV.first) | |
buttonbox = Gtk::HButtonBox.new | |
button = Gtk::Button.new(Gtk::Stock::MEDIA_PLAY) | |
button.signal_connect('clicked') { video.play } | |
buttonbox.add(button) | |
button = Gtk::Button.new(Gtk::Stock::MEDIA_PAUSE) | |
button.signal_connect('clicked') { video.pause } | |
buttonbox.add(button) | |
button = Gtk::Button.new(Gtk::Stock::MEDIA_STOP) | |
button.signal_connect('clicked') { video.stop } | |
buttonbox.add(button) | |
button = Gtk::Button.new(Gtk::Stock::MEDIA_REWIND) | |
button.signal_connect('clicked') { video.seek(-10) } | |
buttonbox.add(button) | |
button = Gtk::Button.new(Gtk::Stock::MEDIA_FORWARD) | |
button.signal_connect('clicked') { video.seek(10) } | |
buttonbox.add(button) | |
hbox = Gtk::HBox.new | |
hbox.pack_start(buttonbox, false) | |
vbox = Gtk::VBox.new | |
vbox.pack_start(video) | |
vbox.pack_start(hbox, false) | |
window.add(vbox) | |
window.signal_connect('destroy') do | |
video.stop | |
Gtk.main_quit | |
end | |
window.set_default_size(640, 480) | |
window.window_position = Gtk::Window::POS_CENTER | |
window.show_all | |
Gtk.main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment