Guest User

Untitled

a guest
Apr 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. class Video < ActiveRecord::Base
  2. has_attached_file :clip
  3. has_attached_file :screenshot, :styles => { :small => '96x96#'}
  4.  
  5. before_save :make_screenshot
  6.  
  7. def make_screenshot
  8. return if clip.queued_for_write.empty?
  9. video_file = clip.queued_for_write[:original].path
  10. filename = temp_file_name
  11. cmd = "ffmpeg -i #{video_file} -an -ss 00:00:10 -an -r 1 -vframes 1 -y -f mjpeg #{filename} >/dev/null 2>&1"
  12. Rails.logger.info "Running cmd: #{cmd}"
  13. system cmd
  14. self.screenshot = File.open(filename)
  15. end
  16.  
  17. private
  18.  
  19. def temp_file_name
  20. tempdir = Dir::tmpdir || '/tmp'
  21. t = Time.now.strftime("%Y%m%d")
  22. path = "clip#{t}-#{$$}-#{rand(0x100000000).to_s(36)}.jpg"
  23. File.join(tempdir, path)
  24. end
  25. end
Add Comment
Please, Sign In to add comment