Originally Posted by atanas
I try it again and yes it works, but when i click Create Mp3 File it load about 30-40 sec and after that show progress bar and load 3 times about 30% 90% and finish. How to correct it to show progress bar of all operating (download flv + convert flv to mp3) and progress bar to show every percent, not only 3 times.
I'm not experiencing the "choppy" progress bar for the FLV file download. But you did bring to my attention something that I hadn't noticed before: The progress bar is only measuring the FLV file download and "not" the progress of the FLV-to-MP3 file conversion as well. So I did some research into the issue (more than I bargained for!), and I discovered a fix for this.
The solution is anything but simple. I added several more files and an additional directory into the mix. The new file structure (as installed in XAMPP) is as follows:
In a nutshell, after the FLV download is complete
, gauging file conversion progress entails the following:
1) Using cURL to connect to another PHP file (exec_ffmpeg.php) whose only job is to execute the FFMPEG process. The cURL connection times out after 10 milliseconds, forcing YouTubeToMp3Converter::GenerateMP3() to "hang up" on exec_ffmpeg.php -- allowing the FFMPEG command to essentially run in parallel to the main program (by severing their connection).
2) Using the FFMPEG command (in exec_ffmpeg.php) to create a log file of the current running FFMPEG process (the file conversion). The log file is a temporary file (stored in the /logs directory), and it's instrumental to figuring conversion progress.
3) Using AJAX (jQuery) to repeatedly access a PHP file (ffmpeg_progress.php) that subsequently polls the log file to find out what percent of the conversion is completed. (The log file contains information that enables this calculation.) We then use this changing percentage to construct the conversion progress bar.
A few things worth noting:
1) Initially, I was against creating/manipulating log files for every conversion because it seemed like a lot of overhead (if you were to scale the application up to fit a larger user base and heavy usage patterns). But I could not find any other way to gauge conversion progress. So take it or leave it, I guess...
2) There is very little in the way of validation here, and you will definitely need to add that for a production-ready application. Of specific note: The exec() call in exec_ffmpeg.php takes a POST variable directly as its parameter. Please
, if you are using this for more than your own personal use (as I am), sanitize the command before you place it in exec()
. You may even want to password-protect this code in some way (i.e., run exec() only if some unique token is present) to prevent abuse. Additional possibilities include some application of escapeshellcmd
. When I get some more time, I may implement one or a combination of these methods myself.
3) Again, when I have more time, I would like to refactor some of the code and clean things up a bit (per my tastes).
So that's pretty much the long and short of things. I definitely value any feedback that you can provide regarding bugs, suggestions, criticisms, and improvements to the code/functionality.
Download the revised files below: