سؤال

قمت بإنشاء فئة قضبان مع مرفق فيديو، وأريد أن أعرف كيفية الحصول على طول الفيديو الذي تم تحميله على طلبي. كيف يمكنني تحقيق ذلك ؟

هل كانت مفيدة؟

المحلول

يستخدم ffmpeg و ال RVideo جوهرة، وهو مجمع روبي رقيق من حوله. هناك الكثير من الشوك 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

نصائح أخرى

لم أحصل على rvideo يعمل بشكل كامل، لم يتم تحديث GEM منذ أربع سنوات. ومع ذلك، فإن هذا يعمل:

  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

اضطررت للقيام بذلك مؤخرا، وكان هذا نهائي:

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، لذلك إذا كان لديك ذلك تثبيت، فمن المحتمل أن تذهب.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top