2009-12-08 143 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的安装,所以如果你有安装你可能好走。

相关问题