我有一个名为 incoming_folder 的目录,其中有一个 mp4文件.
I have a directory called incoming_folder in which there is a mp4 file.
我想通过php代码实现的是扫描 incoming_folder 目录,查找mp4文件,然后将其转换为mp3,然后将其发送到 outgoing_folder .
What I want to achieve through php code is to scan a incoming_folder directory, look for an mp4 file and send it to outgoing_folder after converting it into mp3.
从技术上说, outgoing_folder 应该具有 incoming_folder
Technically outgoing_folder should have mp3 version of mp4 from incoming_folder
以下是我想要的图片:
尝试了以下代码,尽管它扫描了入库文件夹目录,但没有通过ffmpeg进行任何转换.
Tried with the following code although its scanning the incoming_folder directory but no conversion is happening through ffmpeg.
<?php $dir = 'in_folder'; /* Place where mp4 file is present */ $files1 = scandir($dir); print_r($files1); /* It lists all the files in a directory including mp4 file*/ $destination = 'out_folder'; /* Place where mp3 file need to be send after conversion from mp4 */ <?php foreach($files1 as $f) { $parts = pathinfo($f); switch($parts['extension']) { case 'mp4' : system('ffmpeg -i '.$f.' -map 0:2 -ac 1 '.$destination.DS. $parts['filename'].'.mp3', $result); if ($result) { // Do something with result if you want // log for example } break; case 'mp3' : // copy($f, $destination. DS . $parts['filename']. '.' . $parts['extension']); copy($f, $destination.DS.$parts['filename'].'.mp3'); break; } } ?>问题陈述:
我想知道我应该在php代码中进行哪些更改,以便文件转换从 incoming_folder 发生,并且应该转到 outgoing_folder .
I am wondering what changes I should make in the php code so that conversion of file happens from incoming_folder and it should go to outgoing_folder.
推荐答案我看到的主要问题是,您仅将文件名传递给ffmpeg,而不是文件路径.您需要在文件名前面加上$dir.DS.
The main problem I see is that you are passing the file name only to ffmpeg, not the file path. You need to prepend $dir.DS to the file name.
<?php $filePath = $dir . DS . $f; system('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination . DS . $parts['filename'] . '.mp3', $result);解决此问题后,ffmpeg失败,抱怨您的流图参数.我将它们更改为此,并成功运行,YMMV.
After I fixed that, ffmpeg failed, complaining about your stream map parameters. I changed them to this and it worked, YMMV.
<?php system('ffmpeg -i ' . $filePath . ' -acodec libmp3lame -ac 2 -ab 160k -ar 48000 ' . $destination . DS . $parts['filename'] . '.mp3', $result);完整更新的代码
<?php const DS = '/'; $dir = 'in_folder'; /* Place where mp4 file is present */ $files1 = scandir($dir); print_r($files1); /* It lists all the files in a directory including mp4 file*/ $destination = 'out_folder'; /* Place where mp3 file need to be send after conversion from mp4 */ foreach ($files1 as $f) { $parts = pathinfo($f); switch ($parts['extension']) { case 'mp4' : $filePath = $dir . DS . $f; system('ffmpeg -i ' . $filePath . ' -acodec libmp3lame -ac 2 -ab 160k -ar 48000 ' . $destination . DS . $parts['filename'] . '.mp3', $result); if ($result) { // Do something with result if you want // log for example } break; case 'mp3' : // copy($f, $destination. DS . $parts['filename']. '.' . $parts['extension']); copy($f, $destination . DS . $parts['filename'] . '.mp3'); break; } }更多推荐
在目录中查找mp4文件,并在php中转换为mp3后将其发送到其他目录?
发布评论