0

Rails 2.3.5 で SWFUpload と Paperclip を使用して、画像とビデオをアップロードしています。画像のキャプチャ日とビデオの長さを保存するにはどうすればよいですか?

以下は irb で正しく動作します。

irb(main):001:0> File.new('hatem.jpg').mtime
=> Tue Mar 09 16:56:38 +0200 2010

しかし、Paperclip の before_post_process を使用しようとすると:

before_post_process :get_file_info
def get_file_info
  puts File.new(self.media.to_file.path).mtime  # =>Wed Apr 14 18:36:22 +0200 2010
end

キャプチャ日の代わりに現在の日付を取得します。どうすればこれを修正できますか? また、ビデオの長さを取得してモデルに保存するにはどうすればよいですか?

ありがとうございました。

4

1 に答える 1

0

SWFUpload は、handlers.js ファイルにアップロードする前に、ファイル プロパティへのアクセスを提供することがわかりました。したがって、キャプチャ日を取得するには:

//handlers.js    
function uploadStart(file) {
    // set the captured_at to the params
    swfu.removePostParam("captured_at");
    swfu.addPostParam("captured_at", file.modificationdate);
    ...
}

これで、コントローラーで受け取ることができます。

class UploadsController < ApplicationController
  def create
    @upload.captured_at = params[:captured_at].try :to_time
    ...
  end
end

ビデオの長さを取得するために、Paperclip の before_post_process を使用して FFmpeg コマンドを実行しました。

class Upload < ActiveRecord::Base
  before_post_process :get_video_duration

  def get_video_duration
    result = `ffmpeg -i #{self.media.to_file.path} 2>&1`
    if result =~ /Duration: ([\d][\d]:[\d][\d]:[\d][\d].[\d]+)/
      self.duration = $1.to_s
    end
    return true
  end
  ...
end
于 2010-04-29T16:47:58.427 に答える