Sha256: 906b12c4cacf99879b7017636f06f28d7f0ce82ad448c2c45dd2260d3ce2d755

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

require 'streamio-ffmpeg'
require 'fileutils'

module WGif
  class Video
    attr_accessor :name, :clip, :logger

    def initialize name, filepath
      @name = name
      @clip = FFMPEG::Movie.new(filepath)
      FileUtils.mkdir_p "/tmp/wgif/"
      @logger = Logger.new("/tmp/wgif/#{name}.log")
      FFMPEG.logger = @logger
    end

    def trim start_timestamp, duration
      options = {
        audio_codec: "copy",
        video_codec: "copy",
        custom: "-ss #{start_timestamp} -t 00:00:#{'%06.3f' % duration}"
      }
      trimmed = transcode(@clip, "/tmp/wgif/#{@name}-clip.mov", options)
      WGif::Video.new "#{@name}-clip", "/tmp/wgif/#{@name}-clip.mov"
    end

    def to_frames(options={})
      make_frame_dir
      if options[:frames]
        framerate = options[:frames] / @clip.duration
      else
        framerate = 24
      end
      transcode(@clip, "/tmp/wgif/frames/\%2d.png", "-vf fps=#{framerate}")
      open_frame_dir
    end

    private

    def make_frame_dir
      FileUtils.rm Dir.glob("/tmp/wgif/frames/*.png")
      FileUtils.mkdir_p "/tmp/wgif/frames"
    end

    def open_frame_dir
      Dir.glob("/tmp/wgif/frames/*.png")
    end

    def transcode(clip, file, options)
      begin
        clip.transcode(file, options)
      rescue FFMPEG::Error => error
        raise WGif::ClipEncodingException unless error.message.include? "no output file created"
        raise WGif::ClipEncodingException if error.message.include? "Invalid data found when processing input"
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wgif-0.2.0 lib/wgif/video.rb
wgif-0.0.1 lib/wgif/video.rb
wgif-0.0.1.pre lib/wgif/video.rb