在php目录中查找特定文件类型,并在转换后将其发送到其他目录 [英] look for a specific filetype in a directory in php and send it to a different directory after conversion
问题描述
我有一个目录,其中有一个 mp4文件(也包括其他文件),我想将其转换为 mp3 ,然后将其发送到其他目录.我已使用以下命令行命令将其隐藏到mp3中,并且工作正常.
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文件位于 in_folder 中.我想使用ffmpeg将mp4文件转换为mp3 ,然后将其发送到 out_folder .
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)列出目录中的所有文件,包括mp4file.
print_r($files1) lists all the file in a directory including mp4file.
问题陈述:
我想知道我需要编写什么php代码,以便它仅在目录内查找 mp4文件,然后将其发送到不同目录(例如out_folder)转换成mp3.
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.
我想要的图片表示:
推荐答案
以为这就是您想要的:
<?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');
}
}
?>
文档路径信息
通过转换进行
我认为您可以像这样直接导出mp3
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
}
}
文档系统
或者执行第一个循环来转换mp4,然后执行第二个循环来复制mp3
or do a first loop too convert mp4, and a second loop to copy 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'])
以检查文件的扩展名不区分大小写.
Edit 1 :
correction strtolower($parts['extension'])
to check the extension of the file none case-sensitive.
或这样:
strtolower(pathinfo("/path/file.mP4", PATHINFO_EXTENSION)) == ".mp4"
无需使用preg_match
和regexp
,因为pathinfo
是执行此功能的预制函数,并且除非您使用诸如.tar.gz
之类的双扩展名,否则它都可以正常工作.
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
编辑2 :使用rename
而不是copy
移动mp3.
Edit 2 : Use rename
instead of copy
to move mp3.
这篇关于在php目录中查找特定文件类型,并在转换后将其发送到其他目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!