在目录中查找mp4文件,并在php中转换为mp3后将其发送到其他目录?

编程入门 行业动态 更新时间:2024-10-22 20:24:46
本文介绍了在目录中查找mp4文件,并在php中转换为mp3后将其发送到其他目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个名为 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后将其发送到其他目录?

本文发布于:2023-11-24 21:10:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   将其   发送到   转换为   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!