0

我正在使用 SWFUpload 和 Rails 2.3.5 上的 Paperclip 上传图像和视频。如何存储图像的拍摄日期和视频的时长?

以下在 irb 中正常工作:

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

但是当我尝试使用回形针的 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 回答