Web Analytics Made Easy -
StatCounter YouTube-to-MP3 conversion - PHP class and script - CodingForum

Announcement

Collapse
No announcement yet.

YouTube-to-MP3 conversion - PHP class and script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Originally posted by atanas View Post
    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:

    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:
    Attached Files
    Last edited by chump2877; Feb 6th, 2012, 09:14 AM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like our YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Instructional videos and tutorials are also available on YouTube, Dailymotion, and Vimeo]
    Explore all products and services, view demos, review documentation, check prices, and more!
    ♪♪ …Need Web Hosting For Our YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

    Comment


    • prejudice dear excuse my English, I'm testing the script in a linux server with ffmpeg installed and running, I put in config.class.php parameters: / / Constants
      = const _TEMPVIDDIR 'videos /';
      _SONGFILEDIR const = 'mp3 /';
      _FFMPEG const = '/ usr / local / bin / ffmpeg';
      _LOGSDIR const = 'logs /';
      inserting in input the url of youtube nothing happens I see the preview image tells me the title, the Progressed bar is paralyzed, nothing happens something wrong?
      Tanks

      Comment


      • Okay i installed in my VPS server:

        Ubuntu 10.04 Lucid Lynx (32Bit)
        Newest version of this youtube files (2-6-12)

        Did as this guide told me to

        sudo apt-get install apache2
        sudo apt-get install php5
        sudo apt-get install php5-ffmpeg
        (restart system)
        sudo apt-get install curl libcurl3 libcurl3-dev php5-curl php5-mcrypt
        /etc/php5/apache2/php.ini chmod 777 it and enable curl by adding line extension=curl.so
        (restart system or apache)
        $ sudo /etc/init.d/apache2 restart
        download file and put in www
        chmod 777 mp3
        chmod 777 videos
        get the lame mp3 encoder
        sudo apt-get install ffmpeg libavcodec-extra-52
        changed const _FFMPEG = 'ffmpeg.exe'; path ffmpeg.exe to /usr/bin/ffmpeg


        My result: http://91.224.92.69/

        Php info: http://91.224.92.69/test.php

        File is downloaded to my VPS as flv, but it is not converted (looked at mp3 directory, its empty)

        /var/log/apache2 logs show:

        PHP Code:
        [Mon Feb 06 17:06:59 2012] [error] [client 212.12.201.191File does not exist: /var/www/favicon.ico
        [Mon Feb 06 17:07:09 2012] [error] [client 212.12.201.191PHP Fatal error:  Class 'Config' not found in /var/www/ffmpeg_progress.php on line 14refererhttp://91.224.92.69/index.php
        [Mon Feb 06 17:08:24 2012] [error] [client 212.12.201.191File does not exist: /var/www/favicon.ico
        [Mon Feb 06 17:08:26 2012] [error] [client 212.12.201.191PHP Warning:  Division by zero in /var/www/YouTubeToMp3Converter.class.php on line 103refererhttp://91.224.92.69/index.php
        [Mon Feb 06 17:08:27 2012] [error] [client 212.12.201.191File does not exist: /var/www/favicon.ico
        [Mon Feb 06 17:08:47 2012] [error] [client 84.32.224.63PHP Notice:  Undefined indexsubmit in /var/www/index.php on line 142
        [Mon Feb 06 17:08:47 2012] [error] [client 84.32.224.63File does not exist: /var/www/favicon.ico
        [Mon Feb 06 17:08:47 2012] [error] [client 84.32.224.63File does not exist: /var/www/favicon.ico
        [Mon Feb 06 17:08:47 2012] [error] [client 84.32.224.63File does not exist: /var/www/favicon.ico
        [Mon Feb 06 17:09:03 2012] [error] [client 84.32.224.63PHP Warning:  Division by zero in /var/www/YouTubeToMp3Converter.class.php on line 103refererhttp://91.224.92.69/index.php
        [Mon Feb 06 17:09:03 2012] [error] [client 84.32.224.63PHP Warning:  Division by zero in /var/www/YouTubeToMp3Converter.class.php on line 103refererhttp://91.224.92.69/index.php
        [Mon Feb 06 17:09:03 2012] [error] [client 84.32.224.63PHP Warning:  Division by zero in /var/www/YouTubeToMp3Converter.class.php on line 103refererhttp://91.224.92.69/index.php
        [Mon Feb 06 17:09:03 2012] [error] [client 84.32.224.63PHP Warning:  Division by zero in /var/www/YouTubeToMp3Converter.class.php on line 103refererhttp://91.224.92.69/index.php
        [Mon Feb 06 17:09:03 2012] [error] [client 84.32.224.63PHP Warning:  Division by zero in /var/www/YouTubeToMp3Converter.class.php on line 103refererhttp://91.224.92.69/index.php
        [Mon Feb 06 17:09:04 2012] [error] [client 84.32.224.63PHP Warning:  Division by zero in /var/www/YouTubeToMp3Converter.class.php on line 103refererhttp://91.224.92.69/index.php
        [Mon Feb 06 17:11:37 2012] [error] [client 212.12.201.191File does not exist: /var/www/favicon.ico
        [Mon Feb 06 17:11:39 2012] [error] [client 212.12.201.191PHP Warning:  Division by zero in /var/www/YouTubeToMp3Converter.class.php on line 103refererhttp://91.224.92.69/index.php
        [Mon Feb 06 17:11:39 2012] [error] [client 212.12.201.191File does not exist: /var/www/favicon.ico
        [Mon Feb 06 17:16:00 2012] [error] [client 212.12.201.191PHP Notice:  Undefined indexsubmit in /var/www/index.php on line 142
        [Mon Feb 06 17:16:01 2012] [error] [client 212.12.201.191File does not exist: /var/www/favicon.ico
        [Mon Feb 06 17:18:00 2012] [error] [client 212.12.201.191File does not exist: /var/www/favicon.ico
        [Mon Feb 06 17:18:10 2012] [error] [client 212.12.201.191PHP Notice:  Undefined indexsubmit in /var/www/index.php on line 142
        [Mon Feb 06 17:18:10 2012] [error] [client 212.12.201.191File does not exist: /var/www/favicon.ico 
        Last edited by zemaitis; Feb 6th, 2012, 12:19 PM.

        Comment


        • Thanks for the responses...I'll look into the issues that you are having here later this evening and get back to you...
          Regards, R.J.

          ---------------------------------------------------------

          Help spread the word! Like our YouTube-to-Mp3 Conversion Script on Facebook !! :-)
          [Instructional videos and tutorials are also available on YouTube, Dailymotion, and Vimeo]
          Explore all products and services, view demos, review documentation, check prices, and more!
          ♪♪ …Need Web Hosting For Our YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

          Comment


          • And oh i had to reinstall my OS on VPS so there aren't any result pages left, but what i can tell:

            The script downloaded flv files sucessfully, but didn't converted them (mp3's directory was empty)

            Also talking about phpinfo (it was reading all the configs, curl was on as fopen, also ffmpeg.ini files were readed to)

            Comment


            • Originally posted by zemaitis View Post
              And oh i had to reinstall my OS on VPS so there aren't any result pages left, but what i can tell:

              The script downloaded flv files sucessfully, but didn't converted them (mp3's directory was empty)

              Also talking about phpinfo (it was reading all the configs, curl was on as fopen, also ffmpeg.ini files were readed to)
              A few things to try:

              1) Is your path to FFMPEG correct? Try '/usr/local/bin/ffmpeg'?

              2) Are you using at least PHP 5.3?

              3) Try chmod'ing the 'logs/' directory to 0777 as well?

              4) You had the following error message in your server log: PHP Fatal error: Class 'Config' not found in /var/www/ffmpeg_progress.php on line 14. Make sure that this line of code:

              PHP Code:
              include 'config.class.php'
              ...appears at the top of ffmpeg_progress.php.

              5) Comment out this line in index.php, like so:

              PHP Code:
                  // Execution settings
                  
              ini_set('max_execution_time',0);
                  
              //ini_set('display_errors',0); 
              To see what PHP errors (if any) that you are getting?


              I have the code running great in my local XAMPP development environment, so everything should work if set up correctly.
              Regards, R.J.

              ---------------------------------------------------------

              Help spread the word! Like our YouTube-to-Mp3 Conversion Script on Facebook !! :-)
              [Instructional videos and tutorials are also available on YouTube, Dailymotion, and Vimeo]
              Explore all products and services, view demos, review documentation, check prices, and more!
              ♪♪ …Need Web Hosting For Our YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

              Comment


              • Hi guys, I went back and retested the code, and it's not working right for me either...it was working last night, so I'm sure I just need to tweak something somewhere...I'll get back to you with a fix when I get the chance...

                Edit: And now it's working for me again. This clearly seems to be a hit-or-miss type of thing, so, again, I'll get back to you later with a fix.
                Last edited by chump2877; Feb 6th, 2012, 09:46 PM.
                Regards, R.J.

                ---------------------------------------------------------

                Help spread the word! Like our YouTube-to-Mp3 Conversion Script on Facebook !! :-)
                [Instructional videos and tutorials are also available on YouTube, Dailymotion, and Vimeo]
                Explore all products and services, view demos, review documentation, check prices, and more!
                ♪♪ …Need Web Hosting For Our YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

                Comment


                • Back again! I'm having a hard time making the code break again after playing with it some more.

                  So, for those having a problem with the most recent distribution of files (2-6-12.zip), please do the following for me:

                  1) Change the jQuery $.ajax() call in updateConversionProgress() in index.php to read:

                  Code:
                  			$.ajax({
                  				type : "POST",
                  				url : "ffmpeg_progress.php",
                  				data : "uniqueId=<?php echo $converter->GetUniqueID(); ?>&logLength=" + conversionLogLength + "&mp3File=" + encodeURI(songFile),
                  				success : function(retVal, status, xhr) {
                  					var retVals = retVal.split('|');
                  					if (retVals[3] == 2)
                  					{
                  						progress.style.width = progress.innerHTML = parseInt(retVals[1]) + '%';
                  						if (parseInt(retVals[1]) < 100)
                  						{
                  							conversionLogLength = parseInt(retVals[0]);
                  							setTimeout(function(){updateConversionProgress(songFile);}, 10);
                  						}
                  						else
                  						{
                  							$("#preview").css("display", "none");
                  							var convertSuccessMsg = (retVals[2] == 1) ? '<p>Success!</p><p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?mp3=' + encodeURI(songFile) + '">Download your MP3 file</a>.</p>' : '<p>Error generating MP3 file!</p>';
                  							$("#conversionSuccess").html(convertSuccessMsg);
                  							$("#conversionForm").css("display", "block");
                  						}
                  					}
                  					else
                  					{
                  						setTimeout(function(){updateConversionProgress(songFile);}, 1);
                  					}
                  				},
                  				error : function(xhr, status, ex) {
                  					setTimeout(function(){updateConversionProgress(songFile);}, 1);
                  				}
                  			});
                  All I did was add an error handler for a failed AJAX request (which retries the request). I wonder if that will fix some (or all?) of your problems?

                  2) If you are still having issues, please report the AJAX response text using either Firebug in Firefox or Dragonfly in Opera (or any other way that you accomplish the task).

                  If there aren't any (PHP) errors in the response, the AJAX response text will generally look like the following (for example):

                  Code:
                  2577|95|2|2
                  ...or

                  Code:
                  0|100|1|2
                  The first number indicates FFMPEG log file length, the second # indicates conversion progress (on a scale of 1-100), the third indicates conversion success, and the fourth indicates whether an error occurred.

                  I have periodically been getting file permission errors in the AJAX response text (which results in videos and log files not being deleted after the conversion process has finished). I'm not sure why because I think my permissions are set correctly. Please let me know if you experience the same.
                  Last edited by chump2877; Feb 7th, 2012, 12:24 AM.
                  Regards, R.J.

                  ---------------------------------------------------------

                  Help spread the word! Like our YouTube-to-Mp3 Conversion Script on Facebook !! :-)
                  [Instructional videos and tutorials are also available on YouTube, Dailymotion, and Vimeo]
                  Explore all products and services, view demos, review documentation, check prices, and more!
                  ♪♪ …Need Web Hosting For Our YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

                  Comment


                  • Another thing to try is to increase the number of milliseconds in this line in YouTubeToMp3Converter::GenerateMP3() in YouTubeToMp3Converter.class.php:

                    PHP Code:
                    curl_setopt($chCURLOPT_TIMEOUT_MS10); 
                    ...to...

                    PHP Code:
                    curl_setopt($chCURLOPT_TIMEOUT_MS100); 
                    ...or even higher, perhaps...personally, I bumped it up to 100, and so far so good...Reasoning behind this: I noticed that sometimes the log file isn't created by FFMPEG, so I'm guessing that cURL hasn't had a chance to send everything to exec_ffmpeg.php before the CURL connection is severed by CURLOPT_TIMEOUT_MS?

                    Also CURLOPT_TIMEOUT_MS, per php.net, was added in cURL 7.16.2 and available since PHP 5.2.3. So it will only work for those versions and higher. If you don't have those versions, try CURLOPT_TIMEOUT instead (for example):

                    PHP Code:
                    curl_setopt($chCURLOPT_TIMEOUT1); 
                    Can someone let me know if any of these proposed fixes are helping?? Thanks.
                    Regards, R.J.

                    ---------------------------------------------------------

                    Help spread the word! Like our YouTube-to-Mp3 Conversion Script on Facebook !! :-)
                    [Instructional videos and tutorials are also available on YouTube, Dailymotion, and Vimeo]
                    Explore all products and services, view demos, review documentation, check prices, and more!
                    ♪♪ …Need Web Hosting For Our YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

                    Comment


                    • Same vps setting of zemaitis, testing it on my vps, and let you know
                      Last edited by neoanderson; Feb 8th, 2012, 08:57 PM.

                      Comment


                      • Ok launched my system for testing again, address

                        http://91.224.92.69/

                        PHP INFO http://91.224.92.69/test.php

                        Downloads, but doesn't compile the video to mp3. I have to mention that I'm doing everything with root account.

                        Setting in config.class.php :

                        const _FFMPEG = '/usr/bin/ffmpeg';


                        Looks like i wil need to get a windows VPS to run this script...

                        Comment


                        • It would be really nice if somebody would give a tutorial how to install this on Ubuntu or Debian.

                          Comment


                          • Um i have one request.

                            Can u make a custom prefix, setted by variable, for the outputed mp3 files? For example, John is downloading a song: Three Days Grace - Never too late, and the output is Three_days_grace_-_never_too_late.mp3, but i want that he would get the file named Three_days_grace-_-never_too_late_www.mysite.com.mp3

                            Comment


                            • When I get a chance, I'll load Ubuntu into a virtual machine and come up with some instructions for installing the application in that environment. I know it has worked for others using Linux and ffmpeg-php.

                              I will also attach another updated copy of the distribution files with my recent fixes.

                              FWIW, the code seems to consistently work for me now using XAMPP on Windows.

                              Please remember that I work on this script in my spare time. I make no money from this. (Did I mention that your donations are welcome??) A lot of people have shown an interest in this application, and I will do my best to maintain this code as well as implement new functionality requests as time permits.

                              Can u make a custom prefix, setted by variable, for the outputed mp3 files? For example, John is downloading a song: Three Days Grace - Never too late, and the output is Three_days_grace_-_never_too_late.mp3, but i want that he would get the file named Three_days_grace-_-never_too_late_www.mysite.com.mp3
                              You should be able to change the downloaded file name in YouTubeToMp3Converter:ownloadMP3() via the following line of code:

                              PHP Code:
                              header('Content-Disposition: attachment; filename="'.urldecode($file).'"'); 
                              AFAIK, the name given to the downloadable file attachment does not have to match the actual name of the MP3 file on the server. So you could modify the above code as follows, per your example:

                              PHP Code:
                              $siteName 'www.mysite.com';
                              $fname preg_replace('/^((.+?)(\.mp3))$/'"$2_".$siteName."$3"urldecode($file));
                              header('Content-Disposition: attachment; filename="'.$fname.'"'); 
                              That code is untested, but you get the general idea. You could even make $siteName a class field or constant in either YouTubeToMp3Converter class or Config class -- depending on what you are doing.
                              Regards, R.J.

                              ---------------------------------------------------------

                              Help spread the word! Like our YouTube-to-Mp3 Conversion Script on Facebook !! :-)
                              [Instructional videos and tutorials are also available on YouTube, Dailymotion, and Vimeo]
                              Explore all products and services, view demos, review documentation, check prices, and more!
                              ♪♪ …Need Web Hosting For Our YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

                              Comment


                              • Okay, waiting forward for a guide for ubuntu

                                Also i have a problem, if file in youtube is called „ŽAS - Life is good“ the output name is „as_-life_is_good“ without he letter Ž. It would be okay if it would be replaced to Z.

                                P.S. i'm using older version of ur downloader
                                Last edited by zemaitis; Feb 11th, 2012, 09:00 AM.

                                Comment

                                Working...
                                X