I'm looking at the library called FFMpeg which I believe is used in symfony2. There is a method inside which looks like this in the example:
$format->on('progress', function ($video, $format, $percentage) {
echo "$percentage % transcoded";
});
In-turn, it displays real-time progress to the browser. I'm looking to very simple example, it works same way as it does or any blog that explains it very clearly. I am eager to learn.
I am not familiar with the library but judging from the code you posted, the second function is a callback in php
not in javascript
, nothing gets rendered to the browser at this time. All you would get at the end is the echo part once the complete function has finished execution.
Let me answer this with a little generalisation as a i have seen fair number of questions on same
Basically if there is a long running function in php
you never call it inside the views
or this can also be said as you never call it directly from javascript XHR as that makes that request blocking. The correct method involves a queue and websockets (Or XHR polling for old browsers)
From your codebase
$format->on('progress', function ($video, $format, $percentage) {
echo "$percentage % transcoded";
});
Rather than calling the echo function that puts out the output into output stream
push it into a redis queue with a channel
. Think of channel
as simple chat rooms.
Make a websocket connection to a server endpoint that POPS the value from the queue and sends it over to client. This endpoint may or maynot be written in PHP as its not a part of API you are writing but more of WebSocket
connection handler.
Now you might have realized that you actually need to start the function somehow. For this you can use some library like celery
or you can also turn to CRON
if you want to process in batches.
The implementation would be a simple PHP view
that adds the job to celery and returns the status of the request if the job was added successfully to be processes.
Just to clear things more i am adding a simple diagram to help with the process