ffplayの再生時間をratpoisonのmessage barに表示する

ffmpeg附属のmovie playerのffplayは再生時間が画面に出てこない。
一方、-statsオプションをつけると標準出力に再生時間が出力される。

その標準出力をパイプで読み込んでratpoisonのmessage barに表示しちゃおうというのがこのスクリプト
最近はyoutubeなどflvを見ることが多いので重宝している。

パイプ、マルチスレッドプログラミングの簡単な使用例としてもどうぞ。

#!/usr/bin/env ruby
#  rp-ffplay - ffplay with current time/duration display in ratpoison message bar
#  
#  Copyright (C) 2007 rubikitch <rubikitch@ruby-lang.org>
#  Version: $Id: rp-ffplay,v 1.7 2007/06/20 04:02:10 rubikitch Exp $

#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#    This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
require 'ratpoison'
require 'tempfile'

FFPLAY="/usr/local/bin/ffplay"

def main(argv)
  args = argv.map{|a|"'#{a.gsub("'",%q('\\\\''))}'"}*' '  # '
  err = Tempfile.new "rp-ffplay"
  out = IO.popen("#{FFPLAY} -stats #{args} 2> #{err.path} ")
  out.sync = true

  cur = nil
  duration = nil

  Thread.start do 
    until duration
      sleep 0.99
      duration = $1 if err.read =~ /Duration: (.+?), /m
    end
  end

  Thread.start do
    loop do 
      sleep 1
      Ratpoison.echo "%s/%s" % [ to_hms(cur.to_f), duration ]
    end
  end

  while line = out.gets("\cm")
    if line =~ /([\d\.]+) A-V/
      cur = $1
    end
  end
  Ratpoison.echo " "
end

def to_hms(sec)
  int = sec.to_i
  h = int / 3600
  m = int % 3600 / 60
  s = sec - 3600*h - 60*m
  "%02d:%02d:%04.1f" % [ h, m, s ]
end

=begin test_to_hms
assert_equal "00:01:02.0", to_hms(62)
assert_equal "00:01:02.2", to_hms(62.2)
assert_equal "02:01:02.2", to_hms(7262.2)
=end


main(ARGV) if __FILE__==$0