1

How can i get the output file of this FFMPEG code saved to a variable?

  def take_screenshot
    logger.debug "Trying to grab a screenshot from #{self.file}"
    system "ffmpeg -i #{self.file} -ss 00:00:02 -vframes 1 #{Rails.root}/public/uploads/tmp/screenshots/#{File.basename(self.file)}.jpg"
    self.save!
  end

I have tried: self.screenshot = system "ffmpeg -i #{self.file} -ss 00:00:02 -vframes 1 #{Rails.root}/public/uploads/tmp/screenshots/#{File.basename(self.file)}.jpg"

but this doesn't save anything.

thanks in advance!

dodgerogers747
  • 3,325
  • 9
  • 34
  • 55
  • 1
    What do you mean by that? Do you want the contents of the jpeg in a variable? You can capture the stdout of a process in this case, ffmpeg into a variable. The file that ffmpeg writes into is not on stdout. – av501 May 04 '13 at 04:21
  • Hi av501, I would to save the contents of the jpeg which ffmpeg produces to a variable so I can use this with carrierwave to upload to s3. Will stdout do this? – dodgerogers747 May 04 '13 at 06:23
  • 1
    See @slhck's answer below. It is correct. Set it to dump to stdout and then use it. – av501 May 04 '13 at 10:32

1 Answers1

3

ffmpeg usually outputs nothing on stdout and all of its debug messages on stderr. You can make it output the video (or image) to stdout when you pass - as the output file. You'd then also need to suppress stderr.

system "ffmpeg -i #{self.file} -ss 00:00:02 -c:v mjpeg -f mjpeg -vframes 1 - 2>/dev/null"

This will output the raw data of the JPEG-encoded image to stdout. From there you can save the data to a variable and, for example, transfer it somewhere else.

To get stdout from system calls, see here: Getting output of system() calls in ruby – especially popen3 should help you in that case, where you could discard the stderr from within Ruby.

Community
  • 1
  • 1
slhck
  • 36,575
  • 28
  • 148
  • 201