2009-12-08 151 views

回答

2

使用ffmpegRVideo寶石,這是一個薄薄的Ruby包裝。 RVideo項目有很多分支,我個人使用http://github.com/greatseth/rvideo,因爲它支持從視頻捕獲幀並將它們保存爲圖像。當這一切都建立了,你可以這樣做:

# For Paperclip 2 
video_attributes = RVideo::Inspector.new(:file => self.upload.to_file.path, :ffmpeg_binary => "/usr/local/bin/ffmpeg") 
video_attributes.duration # duration in milliseconds 

# For Paperclip 3 
video_attributes = RVideo::Inspector.new(:file => self.upload.queued_for_write[:original].path) 
video_attributes.duration # duration in milliseconds 
+0

偉大的作品,thx本。 – giosakti 2009-12-08 05:21:34

5

我沒有得到Rvideo工作充分,創業板還未四年更新一次。然而,這個工程:

before_post_process :get_video_duration 

    def get_video_duration 
    result = `ffmpeg -i #{self.video.to_file.path} 2>&1` 
    r = result.match("Duration: ([0-9]+):([0-9]+):([0-9]+).([0-9]+)") 
    if r 
     self.duration = r[1].to_i*3600+r[2].to_i*60+r[3].to_i 
    end 
    end 
+0

這有效,但你可能需要使用staged_pa​​th的視頻,如果你還沒有存儲過:'ffmpeg -i#{self.video.staged_pa​​th} 2>&1',這對我有效 – joseramonc 2016-05-06 17:55:20

1

我不得不近日做到這一點,這是我的方法:

before_post_process do 
    file = data.queued_for_write[:original].path 
    self.duration = Paperclip.run("ffprobe", '-i %s -show_entries format=duration -v quiet -of csv="p=0"' % file).to_f 
end 

ffprobe由ffmpeg的安裝,所以如果你有安裝你可能好走。

相關問題