Create 2 mp3-files during upload?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Guldstrand
Forum Newbie
Posts: 20
Joined: Mon Jul 26, 2004 11:16 pm
Location: Sweden

Create 2 mp3-files during upload?

Post by Guldstrand »

I have the following code:

Code: Select all

function convert_audio($source, $format, $ffmpeg, $bitrate, $channel, $freq) { 
 
$cmd = "-acodec libmp3lame -ab $bitrate -ac $channel -ar $freq";
 
$org  = $source;
$wrong = array("å", "ä", "ö", "Å", "Ä", "Ö", "!", " ");
$right   = array("a", "a", "o", "A", "A", "O", "_", "_");
 
$new = str_replace($wrong, $right, $org);
 
 
  exec("$ffmpeg -i $new $cmd {$new}.$format"); 
 
   
     if (file_exists("{$new}.$format")) {  
      
      return array('audio' => "{$new}.$format");
   
   } else {
   
      return false;
      
   }
} 
 
 
///// SKICKA/LADDA UPP ///// 
if (isset($_POST['submit'])) {
 
$audio  = $_FILES['audio']['name'];
$wrong = array("å", "ä", "ö", "Å", "Ä", "Ö", "!", " ");
$right   = array("a", "a", "o", "A", "A", "O", "_", "_");
 
$new = str_replace($wrong, $right, $audio);
 
$path = "ljud/$new";
 
 
   if(move_uploaded_file($_FILES['audio']['tmp_name'], $path)){
 
 
      $type = "mp3";            // format att konvertera till
      $ffmpeg = "ffmpeg";       // Sökväg till mappen där ffmpeg ligger
      $b = "128k";              // Kvalite på ljudet: 96 kb/s
      $c = "1";                 // Kanaler (1=mono, 2=stereo) 
      $f = "16000";                 // Sample rate
 
      
      $convert = convert_audio($path, $type, $ffmpeg, $b, $c, $f); 
 
         // Lyckades konverteringen?
         if ($convert) {
         
            $msg = "<b>Ljudfilen ( ". basename($convert['audio']) ." ) konverterades utan problem!</b><br />
              Bitrate: $b | Kanaler: $c | Sample rate: $f<br /><br />";
         
         } else {
         
            $msg = "Något gick fel!"; 
         }
}
   
   echo $msg;  
} 
///////////////////////////
The code above, converts/encodes all audio formats to .mp3 and changes bitrate, sample rate and so on.

Now i want to convert/encode 2 .mp3-files (of the same file), during upload.
One with low bitrate (demo/exampel), and another with high bitrate.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Create 2 mp3-files during upload?

Post by requinix »

You already have the code to do what you want. Now make it do that twice.

Give it a shot yourself and if you can't figure it out, post your code and where you're having difficulties.
User avatar
Guldstrand
Forum Newbie
Posts: 20
Joined: Mon Jul 26, 2004 11:16 pm
Location: Sweden

Re: Create 2 mp3-files during upload?

Post by Guldstrand »

tasairis wrote:You already have the code to do what you want. Now make it do that twice.

Give it a shot yourself and if you can't figure it out, post your code and where you're having difficulties.
Well.. it´s just that.
I´m not so sure how to proceed. :oops:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Create 2 mp3-files during upload?

Post by requinix »

convert_audio is a function. You can call it with whatever arguments you want.

Code: Select all

$type = "mp3";            // format att konvertera till
$ffmpeg = "ffmpeg";       // Sökväg till mappen där ffmpeg ligger
$b = "128k";              // Kvalite på ljudet: 96 kb/s
$c = "1";                 // Kanaler (1=mono, 2=stereo)
$f = "16000";             // Sample rate
 
$convert = convert_audio($path, $type, $ffmpeg, $b, $c, $f);
$path is the path to the file to convert. It creates a copy named $path.mp3. The comments explain what each variable is for.

Use that function on a second copy of the file to create the demo version.
User avatar
Guldstrand
Forum Newbie
Posts: 20
Joined: Mon Jul 26, 2004 11:16 pm
Location: Sweden

Re: Create 2 mp3-files during upload?

Post by Guldstrand »

I changed it to this:

Code: Select all

if (isset($_POST['submit'])) {
 
$audio  = $_FILES['audio']['name'];
$wrong = array("å", "ä", "ö", "Å", "Ä", "Ö", "!", " ");
$right   = array("a", "a", "o", "A", "A", "O", "_", "_");
 
$new = str_replace($wrong, $right, $audio); 
$path = "ljud/$new"; 
 
 
   if(move_uploaded_file($_FILES['audio']['tmp_name'], $path)){ 
 
      ///// LÅG ///// 
      $type = "wav";            // format att konvertera till
      $ffmpeg = "ffmpeg";       // Sökväg till mappen där ffmpeg ligger
      $b = "64k";               // Kvalite på ljudet: 96 kb/s
      $c = "1";                 // Kanaler (1=mono, 2=stereo) 
      $f = "16000";                 // Sample rate
      
      $convert_low = convert_audio($path, $type, $ffmpeg, $b, $c, $f); 
      /////////////// 
 
      ///// HÖG ///// 
      $type = "mp3";            // format att konvertera till
      $ffmpeg = "ffmpeg";       // Sökväg till mappen där ffmpeg ligger
      $b = "192k";              // Kvalite på ljudet: 96 kb/s
      $c = "2";                 // Kanaler (1=mono, 2=stereo) 
      $f = "44100";                 // Sample rate
      
      $convert_high = convert_audio($path, $type, $ffmpeg, $b, $c, $f); 
      ///////////////
} 
}
Now i can see that two files are created, one .mp3 with high bitrate and one .wav with low bitrate.
So far so good.

But a third .mp3 is also created, with low bitrate.
Why is that?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Create 2 mp3-files during upload?

Post by papa »

No point of converting to a low bit wav imo.
User avatar
Guldstrand
Forum Newbie
Posts: 20
Joined: Mon Jul 26, 2004 11:16 pm
Location: Sweden

Re: Create 2 mp3-files during upload?

Post by Guldstrand »

papa wrote:No point of converting to a low bit wav imo.
Maybe so, but if i change to "mp3", only one version of the file is created/converted.

I have this:

Code: Select all

function convert_audio($source, $format, $ffmpeg, $bitrate, $channel, $freq) { 
 
$cmd = "-acodec libmp3lame -ab $bitrate -ac $channel -ar $freq";
 
$org  = $source;
$wrong = array("å", "ä", "ö", "Å", "Ä", "Ö", "!", " ");
$right   = array("a", "a", "o", "A", "A", "O", "_", "_");
 
$new = str_replace($wrong, $right, $org); 
 
  exec("$ffmpeg -i $new $cmd {$new}.$format"); 
 
   
     if (file_exists("{$new}.$format")) {  
      
      return array('audio' => "{$new}.$format");
   
   } else {
   
      return false;
      
   }
} 
 
$audio  = $_FILES['audio']['name'];
$wrong = array("å", "ä", "ö", "Å", "Ä", "Ö", "!", " ");
$right   = array("a", "a", "o", "A", "A", "O", "", "_");
 
$new = str_replace($wrong, $right, $audio); 
$path = "ljud/$new"; 
 
   if(move_uploaded_file($_FILES['audio']['tmp_name'], $path)){ 
 
      ///// LÅG ///// 
      $type = "wav";            // format att konvertera till
      $ffmpeg = "ffmpeg";       // Sökväg till mappen där ffmpeg ligger
      $b = "64k";               // Kvalite på ljudet: 96 kb/s
      $c = "1";                 // Kanaler (1=mono, 2=stereo) 
      $f = "16000";                 // Sample rate
      
      $convert_low = convert_audio($path, $type, $ffmpeg, $b, $c, $f); 
      /////////////// 
 
      ///// HÖG ///// 
      $type = "mp3";            // format att konvertera till
      $ffmpeg = "ffmpeg";       // Sökväg till mappen där ffmpeg ligger
      $b = "192k";              // Kvalite på ljudet: 96 kb/s
      $c = "2";                 // Kanaler (1=mono, 2=stereo) 
      $f = "44100";                 // Sample rate
      
      $convert_high = convert_audio($path, $type, $ffmpeg, $b, $c, $f); 
      ///////////////
 
unlink($path); 
}  
}
Post Reply