views:

143

answers:

1

Hi Guys,

I'm using SWFUpload and Paperclip on Rails 2.3.5 to upload images and videos. How can I store the capture date of images and duration of videos?

The following works correctly in irb:

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

But when I try to use Paperclip's 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

I get the current date instead of the capture date. How can I fix this? Also, how can I get the video duration and store it with the model?

Thank you.

A: 

It turns out that SWFUpload provides access to the file properties before uploading in the handlers.js file. So, to get the capture date:

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

Now, you can receive it in the controller:

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

To get the video duration, we have used Paperclip's before_post_process to run an FFmpeg command:

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
Hatem