【问题标题】:look for a mp4 file inside a directory and send it to different directory after converting to mp3 in php?在目录中查找一个mp4文件,并在php中转换为mp3后将其发送到不同的目录?
【发布时间】:2019-05-13 18:02:29
【问题描述】:

我有一个名为 incoming_folder 的目录,其中有一个 mp4 文件。

我想通过php代码实现的是扫描一个incoming_folder目录,找到一个mp4文件,转换成mp3后发送到outgoing_folder

技术上 outgoing_folder 应该有 mp3 版本的 mp4 来自 incoming_folder

这是我想要的图形表示:

尝试使用以下代码,尽管它扫描了incoming_folder 目录,但没有通过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

【问题讨论】:

    标签: php audio video ffmpeg


    【解决方案1】:

    我看到的主要问题是您仅将文件名传递给 ffmpeg,而不是文件路径。您需要在文件名前面加上$dir.DS

    <?php
    $filePath = $dir . DS . $f;
    system('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination . DS . $parts['filename'] . '.mp3', $result);
    

    修复后,ffmpeg 失败,抱怨您的流图参数。我把它们改成了这个,它起作用了,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;
        }
    }
    

    【讨论】:

    • 这是我唯一需要进行更改的地方?
    • 是的,只需传入文件路径,而不仅仅是简单的文件名。然后确保你的 ffmpeg 标志是正确的。
    • 这些是我所做的更改; case 'mp4' : $filePath = $dir. DS .$f; echo $filePath; system('ffmpeg -i '.$filePath.' -map 0:2 -ac 1 '.$destination.DS. $parts['filename'].'.mp3', $result);
    • 是的,这将允许 ffmpeg 找到输入文件。
    • 我确实 echo $filePath;它在网页incoming_folderDS36031P.mp4上显示以下o/p
    猜你喜欢
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多