[FFmpeg-user] [Windows] Running ffmpeg.exe programatically - possible to monitor progress?

Tom Evans tevans.uk at googlemail.com
Fri Feb 10 17:49:31 CET 2012


On Fri, Feb 10, 2012 at 1:45 PM, John Dexter <jdxsolutions at gmail.com> wrote:
> Thanks Mark & Alex. More specifically to ffmpeg, I was more wondering
> what to _look for_ in the console output, and if there are cmd-line
> arguments I should look up which can make the output more easily
> computer-readable? The default output I'm getting is not too friendly
> in this regard.
>
> Mark: with your approach (which is the general idea I had in mind), I
> noted ffmpeg.exe doesn't just write new lines but seems to update the
> same console line as it is progressing. Does that come through to
> stderr as normal each time the line changes?
>

I thought I'd chime in with my python code that does the same thing.
Leaving the boring bits of the class behind gives this:

DURATION_RE = re.compile(r'Duration:
(?P<hours>\d+):(?P<mins>\d+):(?P<secs>\d+)\.(?P<msecs>\d+),')

FRAME_RE = re.compile(r'frame=\s*(?P<frames>\d+)\s+fps=\s*(?P<fps>\d+)\s+q=.*\s+size=.*\s+time=\s*(?P<hours>\d+):(?P<mins>\d+):(?P<secs>\d+)\.(?P<msecs>\d+)')

  def process(self):
    pffmpeg = Popen(self.args, stdout=PIPE, stderr=STDOUT,
                        universal_newlines=True, close_fds=True)
    while True:
      line = pffmpeg.stdout.readline()
      if not line:
        break
      if not self.duration:
        m = DURATION_RE.search(line)
        if m:
          self.duration = self.__hr_mins_secs_to_secs(m.groupdict())
        elif self.debug:
          print u'ffmpeg pre-duration: %s' % (line,)
        continue
      m = FRAME_RE.search(line)
      if m:
        data = m.groupdict()
        frame = int(data['frames'])
        fps = int(data['fps'])
        time = self.__hr_mins_secs_to_secs(data)
        self.update_cache(frame, fps, time)
      elif self.debug:
        print u'ffmpeg post-duration: %s' % (line,)
    return True

So the first thing to note is that I tell python to use universal
newlines - ie it treats each of these as an end of line character
sequence '\r', '\n', '\r\n'. This makes parsing the status lines
trivial.

The second thing to note is the regexps used. The first looks for a
'Duration: xxxx' line, and calculates the total time of the media. The
second is the status update itself, which is parsed to determine the
percentage through the file I am. I then store this in a cache, where
it is then used by a web front end to display progress to the user.

Hope that was helpful!

Cheers

Tom


More information about the ffmpeg-user mailing list