认为这就是你想要的:
<?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');
}
}
?>
文档pathinfo https://www.php.net/manual/fr/function.pathinfo.php
编辑并转换:
我想你可以像这样直接导出你的mp3
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
}
}
文档system https://www.php.net/manual/fr/function.system.php
或者执行第一个循环来转换 mp4,然后执行第二个循环来复制 mp3
全部编辑:
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:
更正strtolower($parts['extension'])
检查文件的扩展名不区分大小写。
或者像这样:
strtolower(pathinfo("/path/file.mP4", PATHINFO_EXTENSION)) == ".mp4"
没有必要使用preg_match
and regexp
因为pathinfo
是一个预制函数来完成这项工作,除非您使用双命名扩展,否则它可以正常工作.tar.gz
例如。
正则表达式检测文件扩展名 https://stackoverflow.com/questions/5656475/regular-expression-to-detect-a-file-extension
Edit 2 : Use rename
代替copy
移动 mp3。