: O. Yuanying

iPhone用に動画を公開す(2)

前回のエントリで書いたAppleScriptがうまく動かない。

動画の数が3つ以上になるとエラーダイアログが出てエンコードされないぽい。repeat がうまくいってないのかな?よくわからん。

ってことで怪しそうなところを Ruby Cocoa で書いて、Ruby から AppleScript を呼び出す形式にしてみた。


#!/usr/bin/env ruby -wKU
require 'optparse'
require 'osx/cocoa'

output_dir = '~/Movies'

opt = OptionParser.new
opt.on("-o", "--output-directory OUTPUT_DIRECTORY") { |v| output_dir = v }
opt.parse!

ARGV.each do |file|
  new_file = File.join(output_dir, File.basename(file, '.*'))
  script = OSX::NSAppleScript.alloc.initWithSource(%{
  tell application "QuickTime Player"                                                                                                                                                                                   
    launch
    activate
    stop every document
    close every document saving no   
    open "#{file}"
    set file_name to name of document 1
    set new_file to "#{new_file}" & ".m4v"

    with timeout of (1 * 60 * 60) seconds
      export document 1 to new_file as iPhone
    end timeout

    close document 1 saving no
  end tell
  })
  errinfo = OSX::OCObject.new
  result = script.executeAndReturnError(errinfo)
  p result
end

こんな感じで使う。

$ ruby mp4.rb -o /Volumes/yuanying/Sites/files/data/ /Volumes/yuanying/movie/攻殻機動隊/*

-o で出力先ディレクトリ指定。

コマンドラインから実行めんどい。少し様子を見てみよう。

ffmpeg を使う

ffmpegでエンコード版。


#!/usr/bin/env ruby -wKU
require 'optparse'

output_dir  = '~/Movies'
size        = '480x270'

opt = OptionParser.new
opt.on("-o", "--output-directory OUTPUT_DIRECTORY") { |v| output_dir = v }
opt.on("-s", "--size SIZE") { |v| size = v }
opt.parse!

ARGV.each do |file|
  new_file = File.join(output_dir, File.basename(file, '.*'))
  `ffmpeg -i #{file} -vcodec libx264 -b 768k -s #{size} -r 29.97 -acodec libfaac -ac 2 -ar 44100 -ab 160k #{new_file}.mp4`
end

こっちの方がよさげ。