This function is giving me an error but I’ve copied it from the video-diet program and I don’t really understand it so I can’t simplify it. Could someone who understands it, explain it step by step?

def convert_video_progress_bar(self, manager, cmd):
    name = self.path.rsplit(os.path.sep, 1)[-1]
    proc = expect.spawn(cmd, encoding='utf-8')
    pbar = None
    try:
        proc.expect(pattern_duration)
        total = sum(map(lambda x: float(
            x[1])*60**x[0], enumerate(reversed(proc.match.groups()[0].strip().split(':')))))
        cont = 0
        pbar = manager.counter(
            total=100,
            desc=name,
            unit='%',
            bar_format=BAR_FMT,
            counter_format=COUNTER_FMT,
            leave=False
        )
        while True:
            proc.expect(pattern_progress)
            progress = sum(map(lambda x: float(
                x[1])*60**x[0], enumerate(reversed(proc.match.groups()[0].strip().split(':')))))
            percent = progress/total*100
            pbar.update(percent-cont)
            cont = percent
    except expect.EOF:
        traceback.print_exc()
    finally:
        if pbar is not None:
            pbar.close()
    proc.expect(expect.EOF)
    res = proc.before
    res += proc.read()
    exitstatus = proc.wait()
    if exitstatus:
        raise ffmpeg.Error('ffmpeg', '', res)
@thann@heapoverflow.ml
mod
link
fedilink
3
edit-2
20d

It would be helpful to link the full source like this

It looks like you’re using an older version of the code, so its hard to tell whats happening exactly. can you link it please?

The code is pretty much entirely just using the pexpect library, so you’d need to familiarize yourself with that first. Read the API Overview section of the docs.

Also, a good way of figuring out how code works, is to print() out the variables youre curious about.

def convert_video_progress_bar(self, manager, cmd):
    name = self.path.rsplit(os.path.sep, 1)[-1]
    # Run ffmpeg
    proc = expect.spawn(cmd, encoding='utf-8')
    pbar = None
    try:
        # extract the total number of frames 
        proc.expect(pattern_duration)
        total = sum(map(lambda x: float(
            x[1])*60**x[0], enumerate(reversed(proc.match.groups()[0].strip().split(':')))))
        cont = 0
        # keeps track of the progress to show it to the user
        pbar = manager.counter(
            total=100,
            desc=name,
            unit='%',
            bar_format=BAR_FMT,
            counter_format=COUNTER_FMT,
            leave=False
        )
        # infinite loop
        while True:
            # look for a progress indicator, and extract the frame count
            proc.expect(pattern_progress)
            progress = sum(map(lambda x: float(
                x[1])*60**x[0], enumerate(reversed(proc.match.groups()[0].strip().split(':')))))
            # compute the percentage complete
            percent = progress/total*100
            # update the counter, to show the user
            pbar.update(percent-cont)
            cont = percent
    # because of the infinite loop, 
    # `proc.expect` will throw an error once ffmpeg has completed.
    # catch that error, and clean things up
    except expect.EOF:
        traceback.print_exc()
    finally:
        if pbar is not None:
            pbar.close()
    proc.expect(expect.EOF)
    res = proc.before
    res += proc.read()
    # check to see if ffmpeg threw and error
    exitstatus = proc.wait()
    if exitstatus:
        raise ffmpeg.Error('ffmpeg', '', res)

Ask specific questions about how to code something in python

Python docs (tutorial)
Intro to programming - University of Helsinki

General python discussion on lemmy.ml

Create Post From:
lemmy.ml

  • 0 users online
  • 1 user / day
  • 2 users / week
  • 3 users / month
  • 7 users / 6 months
  • 18 subscribers
  • 7 Posts
  • 11 Comments
  • Modlog