认为这是你想要的:
<?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
使用转换编辑:
我觉得你可以像这样直接导出你的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
或者做第一个循环也转换 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;
}
}
编辑 1 :
更正strtolower($parts['extension'])检查文件扩展名不区分大小写。
或者像这样:
strtolower(pathinfo("/path/file.mP4", PATHINFO_EXTENSION)) == ".mp4"
没有必要使用preg_match 和regexp,因为pathinfo 是完成这项工作的预制函数,除非您使用像.tar.gz 这样的双命名扩展名,否则它可以正常工作。
regular-expression-to-detect-a-file-extension
编辑 2 :使用 rename 而不是 copy 移动 mp3。