question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

progress event support

See original GitHub issue

Hi,

I was working on a transcoding platform and I found your module to wrapping ffmpeg. I’m interested to make improvements that would be useful to everyone involved in this niche.

I’m planning to replace Popen.communicate call to achieve the goals:

  • Make ffmpy efficient in memory usage
  • Give it progress reporting support

Popen.communicate() is inefficient because of stdout and stderr buffering. Let’s see some cases:

Use case 1:

you want to send transcoded data over network as fast as available. If communicate() is used, you must to accumulate the entire transcoded movie on a memory buffer to send it.

Use case 2:

you want to know the transcoding state (output size, time, bitrate) in real time. If communicate() is used, you can’t do it because this method is consuming the stderr pipe

I have a experimental branch with a on_progress callback implemented here: https://github.com/astroza/ffmpy/tree/progress_support

An usage example is:

def on_progress(state):
        print state.time

ff = ffmpy.FFmpeg(
        global_options="-hide_banner -nostdin -y -stats",
        inputs={local_input_path: None},
        outputs={local_output_path: None},
        on_progress=on_progress
)
ff.run(stderr=subprocess.PIPE)

Now I’m working to make it backwards compatible. The branch break your API

UPDATE

https://github.com/astroza/ffmpy/tree/progress_support_v2 keeps backward compatibility

Usage:

def on_progress(state):
        print state.time

ff = ffmpy.FFmpeg(
        global_options="-hide_banner -nostdin -y -stats",
        inputs={local_input_path: None},
        outputs={local_output_path: None}
)
print(ff.run(on_progress=on_progress)[1])

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:8

github_iconTop GitHub Comments

2reactions
astrozacommented, Jun 15, 2020

Hi @Benny739, I have had no response from the repo owner. Nevertheless, you can find an working branch (it’s used on a company) here https://github.com/astroza/ffmpy/tree/progress_support_v2

movie_description = ....

def on_progress(state):
       progress = (float(state.time or state.start_time) / movie_description.duration) * 100
       print(progress)

ff = ffmpy.FFmpeg(
        global_options="-hide_banner -nostdin -y -stats",
        inputs={local_input_path: None},
        outputs={local_output_path: None}
)
print(ff.run(on_progress=on_progress)[1])

state object has all what you need

1reaction
Benny739commented, Jun 17, 2020

I found the bug, it’s caused by piping and not closing stdin and stderr. In the wait function after the while loop I close stdin and stderr now, and the warning is gone.

    def wait(self, on_progress=None, stderr_ring_size=30):
        stderr_ring = []
        is_running = True
        stderr_fileno = self.process.stderr.fileno()
        ff_state = FFstate()
        while is_running:
            latest_update = os.read(stderr_fileno, self.update_size)
            if ff_state.consume(latest_update) and on_progress is not None:
                on_progress(ff_state)
            stderr_ring.append(latest_update.decode())
            if len(stderr_ring) > stderr_ring_size:
                del stderr_ring[0]
            is_running = self.process.poll() is None

        stderr_out = str.join("", stderr_ring)

        self.process.stdin.close()
        self.process.stderr.close()

        if self.process.returncode != 0:
            raise FFRuntimeError(self.cmd, self.process.returncode, stderr_out)

        return stderr_out
Read more comments on GitHub >

github_iconTop Results From Across the Web

ProgressEvent - Web APIs - MDN Web Docs - Mozilla
desktop desktop Chrome Edge ProgressEvent Full support. Chrome1. Toggle history Full support. Edge12. T... ProgressEvent() constructor Full support. Chrome16. Toggle history Full support. Edge14. T...
Read more >
The ProgressEvent - W3Schools
Events that occur when loading external resources, belongs to the ProgressEvent Object. ProgressEvent Properties and Methods. Property/Method, Description.
Read more >
Progress Events - Chrome Developers
There are five types of events that developers can respond to in Native Client: progress, message, view change, focus, and input events (each...
Read more >
XMLHttpRequest API: `progress` event | Can I use... Support ...
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and ... XMLHttpRequest API: progress event.
Read more >
ProgressEvent object schema - CloudFormation Command ...
A ProgressEvent is a JSON object which represents the current operation status of the handler, the current live state of the resource, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found