I have a directory in which there is a mp4 file (including other files as well) which I want to convert into mp3 and then send it to different directory. I have used the following command line command to covert into mp3 and its working perfectly fine.
ffmpeg -i 36031P.mp4 -map 0:2 -ac 1 floor_english.mp3
mp4 file is inside in_folder. Using ffmpeg, I want to convert mp4 file into mp3 and send it to out_folder.
<?php
$dir = 'in_folder';
$files1 = scandir($dir);
print_r($files1); /* It lists all the files in a directory including mp4 file*/
?>
print_r($files1) lists all the file in a directory including mp4file.
Problem Statement:
I am wondering what php code I need to write so that it looks for only mp4 file inside the directory and send it to different directory (let say out_folder) after converting into mp3.
The pictorial representation of what I want:
In think it is what you want :
<?php
$dir = 'in_folder';
$files1 = scandir($dir);
print_r($files1); /* It lists all the files in a directory including mp4 file*/
$destination = 'your new destination';
foreach($files1 as $f)
{
$parts = pathinfo($f);
if ($parts['extension'] = 'mp3';
{
// copy($f, $destination. DS . $parts['filename']. '.' . $parts['extension']);
rename($f, $destination. DS . $parts['filename']. '.mp3');
}
}
?>
Documentation pathinfo
Edit with convertion :
I think you can directly export your mp3 like this
foreach($files1 as $f)
{
$parts = pathinfo($f);
if ($parts['extension'] = 'mp4';
{
// $result : the last line of the command output on success, and FALSE on failure. Optional.
system('ffmpeg -i '.$f.' -map 0:2 -ac 1 '.$destination.DS. $parts['filename'].'.mp3', $result);
}
// See: https://www.php.net/manual/en/function.system.php
if ($result === false) {
// Do something if failed
// log for example
} else {
// command completed with code : $result
// 0 by convention for exit with success EXIT_SUCCESS
// 1 by convention for exit with error EXIT_ERROR
// https://stackoverflow.com/questions/12199216/how-to-tell-if-ffmpeg-errored-or-not
}
}
Documentation system
or do a first loop too convert mp4, and a second loop to copy mp3
Edit all in one :
foreach($files1 as $f)
{
$parts = pathinfo($f);
switch(strtolower($parts['extension']))
{
case 'mp4' :
// $result : the last line of the command output on success, and FALSE on failure. Optional.
system('ffmpeg -i '.$f.' -map 0:2 -ac 1 '.$destination.DS. $parts['filename'].'.mp3', $result);
// See: https://www.php.net/manual/en/function.system.php
if ($result === false) {
// Do something if failed
// log for example
} else {
// command completed with code : $result
// 0 by convention for exit with success EXIT_SUCCESS
// 1 by convention for exit with error EXIT_ERROR
// https://stackoverflow.com/questions/12199216/how-to-tell-if-ffmpeg-errored-or-not
}
break;
case 'mp3' :
// copy($f, $destination. DS . $parts['filename']. '.' . $parts['extension']);
rename($f, $destination.DS.$parts['filename'].'.mp3');
break;
}
}
Edit 1 :
correction strtolower($parts['extension'])
to check the extension of the file none case-sensitive.
or like this :
strtolower(pathinfo("/path/file.mP4", PATHINFO_EXTENSION)) == ".mp4"
There is no need to use preg_match
and regexp
because pathinfo
is a pre-made function to do the job and it works fine unless you use double named extension like .tar.gz
for example.
regular-expression-to-detect-a-file-extension
Edit 2 : Use rename
instead of copy
to move mp3.