DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
+2 votes

I'm trying to print a progress bar while executing ffmpeg but I'm having trouble getting the total number of frames and the current frame being processed. The error I get is AttributeError: 'NoneType' object has no attribute 'groups'. I've copied the function from this program and for some reason it works there but not here, even though I haven't changed that part.

main.py

pattern_duration = re.compile(
    'duration[ \t\r]?:[ \t\r]?(.+?),[ \t\r]?start', re.IGNORECASE)
pattern_progress = re.compile('time=(.+?)[ \t\r]?bitrate', re.IGNORECASE)

def execute_ffmpeg(self, manager, cmd):
    proc = expect.spawn(cmd, encoding='utf-8')
    self.update_progress_bar(proc, manager)
    self.raise_ffmpeg_error(proc)

def update_progress_bar(self, proc, manager):
    total = self.get_total_frames(proc)
    cont = 0
    pbar = self.initialize_progress_bar(manager)
    try:
        proc.expect(pattern_duration)
        while True:
            progress = self.get_current_frame(proc)
            percent = progress / total * 100
            pbar.update(percent - cont)
            cont = percent
    except expect.EOF:
        pass
    finally:
        if pbar is not None:
            pbar.close()

def raise_ffmpeg_error(self, proc):
    proc.expect(expect.EOF)
    res = proc.before
    res += proc.read()
    exitstatus = proc.wait()
    if exitstatus:
        raise ffmpeg.Error('ffmpeg', '', res)

def initialize_progress_bar(self, manager):
    pbar = None
    pbar = manager.counter(
        total=100,
        desc=self.path.rsplit(os.path.sep, 1)[-1],
        unit='%',
        bar_format=BAR_FMT,
        counter_format=COUNTER_FMT,
        leave=False
    )
    return pbar

def get_total_frames(self, proc):
    return sum(map(lambda x: float(
        x[1])*60**x[0], enumerate(reversed(proc.match.groups()[0].strip().split(':')))))

def get_current_frame(self, proc):
    proc.expect(pattern_progress)
    return sum(map(lambda x: float(
                x[1])*60**x[0], enumerate(reversed(proc.match.groups()[0].strip().split(':')))))
by

1 Answer

+1 vote
 
Best answer

Your entry point is execute_ffmpeg? The flow is then:

execute_ffmpeg
    -> expect.spawn(cmd, encoding='utf-8')
    self.update_progress_bar
        self.get_total_frames
            -> proc.match.groups()

between spawn and match you have to expect until a pattern is matched.

Example:

cmd = "ffmpeg -i video.mkv video.avi"
proc = expect.spawn(cmd, encoding='utf-8')
proc.expect(pattern_duration)
print(get_total_frames(proc))
by
selected by
Contributions licensed under CC0
...