问题
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:
回答1:
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.
回答2:
I feel this could all be a bit more robust and shorter.
<?php
// the folder to get MP4's from, recursively
$src_dir = 'folder/a';
// the destination to put MP3's in.
$dst_dir = 'folder/b';
// An iterator ('reader') that takes all files in src
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($src_dir)
);
// A filter, that only leaves entries ending with .mp4, case-insensitive.
$videos = new RegexIterator($files, '/^.+\.mp4$/i', RecursiveRegexIterator::MATCH));
// Then, for each element in videos, convert to MP3
foreach ($videos as $video) {
$src = $video->getPathname();
// The destination is the basename without 'mp4', with 'mp3' appended.
$dst = $dst_dir.'/'.$video->getBaseName($video->getExtension()).'mp3';
system("ffmpeg -i $src -map 0:2 -ac 1 $dst");
}
You should take care that you don't allow 'user specified' names for files, but use your own (random) filenames. Avoid executing unvalidated user input at all cost!
回答3:
This will do it. I tested the solution and it works. I had to make one parameter change for the ffmpeg command. On my machine, it was throwing an error for the mapping. I added a question mark after the mapping to ignore the error.
The code assumes the following folder structure:
<?php
error_reporting(E_ALL);
$in_folder = sprintf("%s/mp4", __DIR__);
$out_folder = sprintf("%s/mp3", __DIR__);
if(!file_exists($in_folder)){
mkdir($in_folder);
}
if(!file_exists($out_folder)){
mkdir($out_folder);
}
$items = scandir($in_folder);
foreach($items as $item){
if(preg_match('/^.*\.mp4$/', $item)){
$file_name = str_replace(".mp4", "", $item);
$in_file = sprintf("%s/%s.mp4", $in_folder, $file_name);
$out_file = sprintf("%s/%s.mp4", $out_folder, $file_name);
if(file_exists($out_file)){
unlink($out_file);
}
$cmd = sprintf("ffmpeg -i %s -map 0:2? -ac 1 %s", $in_file, $out_file);
print "$cmd\n";
exec($cmd);
}
}
?>
来源:https://stackoverflow.com/questions/56094984/look-for-a-specific-filetype-in-a-directory-in-php-and-send-it-to-a-different-di