views:

85

answers:

2

I have this ffmpeg video encoder using Rails and I needed to report ffmpeg progress to the user. How can that be done provided that I am using Linux?

A: 

Previously asked and answered.

http://stackoverflow.com/questions/747982/can-ffmpeg-show-a-progress-bar

Edit: The main gist of that other question is that you want to run ffmpeg in another thread and then monitor the log files to parse out the progress.

Long ago Zencoder (disclosure: I'm a part of it) released a gem called RVideo. It's a low level Rails gem that helps you deal with video. We no longer actively develop that gem, having moved on to other tools, but in the spirit of OSS others have done some great work with it.

From one of the forks, which I have not personally used but seems to be the most active, take a look at the ffmpeg.rb file and look for the execute_with_progress method. If you trace that through the source, you should get an idea of how it's handled in RVideo and maybe get some hints for your own code.

Please ask specific questions if you run into trouble.

jdl
NONE of the solutions listed there worked for me. I also need something ver specific to Rails.
Omar
@Omar: Check my edit above. Hopefully that's more helpful than the other thread.
jdl
A: 

This is the solution I reached:

def execute_ffmpeg(cmd, progress)
  logger.debug "Running command #{cmd}"
  command = "#{cmd} 2>&1"
  progress = nil
  frames = nil
  fps = 25
  ffmpeg = IO.popen(command)
  ffmpeg.each("\r") do |line|
    if frames.nil? && line =~ /Duration:(\s.?(\d*):(\d*):(\d*\.\d*))/
      duration = $2.to_s + ":" + $3.to_s + ":" + $4.to_s
      frames = ($4.to_i + ($3.to_i * 60) + ($2.to_i * 60 * 60)) * fps
    end
    if line =~ /frame=(\s.?(\d*))/
      progress = $1.to_i / frames.to_i
      print "Progress: #{progress}"
    end
  end
end
Omar