Originally posted by atanas
View Post
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:
C:\xampp\htdocs\VideoToMp3Converiter\logs
C:\xampp\htdocs\VideoToMp3Converiter\mp3
C:\xampp\htdocs\VideoToMp3Converter\videos
C:\xampp\htdocs\VideoToMp3Converter\config.class.php
C:\xampp\htdocs\VideoToMp3Converter\exec_ffmpeg.php
C:\xampp\htdocs\VideoToMp3Converter\ffmpeg.exe
C:\xampp\htdocs\VideoToMp3Converter\ffmpeg_progress.php
C:\xampp\htdocs\VideoToMp3Converter\index.php
C:\xampp\htdocs\VideoToMp3Converter\YouTubeToMp3Converter.class.php
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 and/or escapeshellarg. 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:
Comment