class Rabbit::Element::Video
Attributes
Public Class Methods
Source
# File lib/rabbit/element/video.rb, line 38 def initialize(filename, props) @filename = filename @thumbnail = nil super() @properties = Properties.new(props) width, height = detect_size @original_width = width @original_height = height %w(relative_width relative_height relative_margin_top relative_margin_bottom relative_margin_left relative_margin_right relative_padding_top relative_padding_bottom relative_padding_left relative_padding_right ).each do |name| instance_variable_set("@#{name}", @properties.get_relative_size(name, @filename)) end resize(width, height) end
Calls superclass method
Rabbit::Element::Base::new
Public Instance Methods
Source
# File lib/rabbit/element/video.rb, line 93 def as_large_as_possible? @properties.get_boolean("as_large_as_possible", false) end
Source
# File lib/rabbit/element/video.rb, line 63 def compile(canvas, x, y, w, h) super adjust_size(canvas, @x, @y, @w, @h) end
Calls superclass method
Rabbit::Element::Base#compile
Also aliased as: _compile
Source
# File lib/rabbit/element/video.rb, line 59 def compile_for_horizontal_centering(canvas, x, y, w, h) _compile(canvas, x, y, w, h) end
Source
# File lib/rabbit/element/video.rb, line 103 def draw_element(canvas, x, y, w, h, simulation) unless simulation @thumbnail.draw(canvas, x, y) if @thumbnail end [x, y + height, w, h - height] end
Source
# File lib/rabbit/element/video.rb, line 89 def height @height.to_i + @padding_top + @padding_bottom end
Source
# File lib/rabbit/element/video.rb, line 97 def keep_ratio? @properties.get_boolean("keep_ratio", true) and @original_width and @original_height end
Source
# File lib/rabbit/element/video.rb, line 79 def scene_snapshot_element(widget, snapshot, canvas, x, y, w, h) y += @height h -= @height [x, y, w, h] end
Source
# File lib/rabbit/element/video.rb, line 68 def setup_scene_element(canvas, scene_widget, x, y, w, h) x, y, w, h = super video = Gtk::Video.new(@filename) scene_widget.put(video, x, y, @width || w, @height || h) y += @height h -= @height [x, y, w, h] end
Calls superclass method
Rabbit::Element::Base#setup_scene_element
Source
# File lib/rabbit/element/video.rb, line 110 def text "video: #{File.basename(@filename)}" end
Source
# File lib/rabbit/element/video.rb, line 85 def width @width.to_i + @padding_left + @padding_right end
Private Instance Methods
Source
# File lib/rabbit/element/video.rb, line 199 def adjust_margin(w, h) @margin_top = @relative_margin_top&.resolve(h) || @margin_top @margin_bottom = @relative_margin_bottom&.resolve(h) || @margin_bottom @margin_left = @relative_margin_left&.resolve(w) || @margin_left @margin_right = @relative_margin_right&.resolve(w) || @margin_right end
Source
# File lib/rabbit/element/video.rb, line 210 def adjust_padding(w, h) @padding_top = @relative_padding_top&.resolve(h) || @padding_top @padding_bottom = @relative_padding_bottom&.resolve(h) || @padding_bottom @padding_left = @relative_padding_left&.resolve(w) || @padding_left @padding_right = @relative_padding_right&.resolve(w) || @padding_right end
Source
# File lib/rabbit/element/video.rb, line 221 def adjust_size(canvas, x, y, w, h) base_w = w base_h = h adjust_margin(base_w, base_h) adjust_padding(base_w, base_h) base_h = base_h - @padding_top - @padding_bottom if as_large_as_possible? iw = base_w ih = base_h else iw = @relative_width&.resolve(base_w) ih = @relative_height&.resolve(base_h) end resize(iw, ih) end
Source
# File lib/rabbit/element/video.rb, line 119 def detect_size width = @properties.get_size("width", @filename) height = @properties.get_size("height", @filename) if (width.nil? or height.nil?) and Gtk.const_defined?(:MediaFile) w, h = detect_size_media_file width ||= w height ||= h end if (width.nil? or height.nil?) and defined?(::Gst) w, h = detect_size_gstreamer width ||= w height ||= h end [width, height] end
Source
# File lib/rabbit/element/video.rb, line 153 def detect_size_gstreamer pipeline = Gst::Pipeline.new filesrc = Gst::ElementFactory.make("filesrc") filesrc.location = @filename decodebin = Gst::ElementFactory.make("decodebin") return [nil, nil] if decodebin.nil? videoconvert = Gst::ElementFactory.make("videoconvert") return [nil, nil] if videoconvert.nil? jpegenc = Gst::ElementFactory.make("jpegenc") return [nil, nil] if jpegenc.nil? filesink = Gst::ElementFactory.make("filesink") @thumbnail_file = Tempfile.new(["rabbit-video", ".jpeg"]) filesink.location = @thumbnail_file.path pipeline << filesrc << decodebin << videoconvert << jpegenc << filesink filesrc >> decodebin decodebin.signal_connect(:pad_added) do |_, pad| sink_pad = videoconvert.get_static_pad("sink") pad.link(sink_pad) end videoconvert >> jpegenc >> filesink loop = GLib::MainLoop.new bus = pipeline.bus bus.add_watch do |bus, message| case message.type when Gst::MessageType::EOS loop.quit when Gst::MessageType::ERROR loop.quit end true end pipeline.play begin loop.run ensure pipeline.stop end @thumbnail = ImageLoader.new(@thumbnail_file.path) [@thumbnail.width, @thumbnail.height] end
Source
# File lib/rabbit/element/video.rb, line 135 def detect_size_media_file gio_file = Gio::File.open(path: @filename) media_file = Gtk::MediaFile.new(gio_file) # We want to mute here to avoid playing audio but if we # mute, media_file isn't prepared... # media_file.muted = true media_file.play while GLib::MainContext.default.iteration(true) do end width = media_file.intrinsic_width width = nil if width.zero? height = media_file.intrinsic_height height = nil if height.zero? media_file.pause media_file.unref [width, height] end
Source
# File lib/rabbit/element/video.rb, line 237 def resize(w, h) return if w.nil? and h.nil? if keep_ratio? if w and h.nil? h = (@original_height * w.to_f / @original_width).ceil elsif w.nil? and h w = (@original_width * h.to_f / @original_height).ceil end else w ||= width h ||= height end w = w.ceil if w h = h.ceil if h if w and w > 0 and h and h > 0 and [w, h] != [width, height] @width = w @height = h @thumbnail&.resize(@width, @height) end end