エンコードプラグイン Filter::ffmpeg

ソースコードを公開していなかったので。
でも、登録するやつは少し変更するかも。もうちょい試して、実績がほしい。
Pragger を使っているなら、Pragger 用のプラグインに書き直してもいいかも。

require 'kconv'

module FFmpeg
  class Helper
    def initialize(file)
      @profile = {
        :ipod_touch => {
          :f        => 'mp4',
          :bufsize  => '2000k',
          :maxrate  => '768k',
          :s        => '320x240',
          :b        => '512k',
          :bt       => '512k',
          :vcodec   => 'libx264',
          :acodec   => 'libfaac',
          :ac       => '2',
          :ab       => '128k',
        },
        :audio => {
          :ac      => '2',
          :ab      => '128k',
          :ar      => '44100',
        },
      }

      @file            = file
      @option          = {}
      @extended_option = ''
    end

    def set_profile(name)
      if @profile.key?(name)
        @option.merge!(@profile[name])
      else
        $stderr.puts "not found profile; #{name}"
      end
    end

    def set_option(opt)
      @option.merge!(opt)
    end

    def extended_option(s)
      @extended_option = s
    end

    def execute(infile, outfile)
      argument = []

      argument << %Q{#{@file} -y -i "#{infile}" }

      @option.each {|k, v|
        argument << "-#{k} #{v} "
      }

      argument << %Q{ #{@extended_option} "#{outfile}" 2>&1}

      command = argument.join('')

      puts "#{command}"

      io = IO.popen("#{command}", "r");
      io.each("\r") {|line|
        puts line
      }
      io.close

      return $?
    end
  end
end

def ffmpeg(context, config, &block)
# return true  unless config.key?(:key) && config.key?(:regexp) && config.key?(:to)

  # :input => エンコードするファイル名を格納しているキー
  # :filename => エンコード後のファイル名
  # :format => エンコードフォーマット
  # :extra_options => ffmpeg に指定する追加のコマンドオプション類
  # :dir => エンコードしたファイルの出力ディレクトリ
  # :encoding => ファイル名のエンコード指定

  time = %Q{"#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"}
  input_key = config[:input].to_sym

  ffmpeg = FFmpeg::Helper.new(config[:command])
  ffmpeg.set_profile(:ipod_touch)
  ffmpeg.set_option(:timestamp => time)
  ffmpeg.set_option(:f => config[:format])
  ffmpeg.extended_option(config[:extra_options])

  context.feed.entry.each {|item|
    filename = config[:filename].gsub(/%(.+?)%/) {|s|
      if item.key?($1.to_sym)
        item[$1.to_sym]
      else
        s
      end
    }

    if item.key?(:title)
      ffmpeg.set_option(:title => %Q{"#{item[:title].send('to' + config[:encoding])}"})
    end

    dest = File.expand_path(config[:dir] + '/' + filename)

    ffmpeg.execute(item[input_key], dest.send('to' + config[:encoding]))

    item[input_key] = dest.toutf8
  }
end