dongshi4773 2019-06-11 18:59
浏览 136

如何在php中使用系统命令?

I am working on a PHP code as shown below in which conversion of mp4 into mp3 is happening at Line B.

I have added if block after system command to print Conversion Completed on the webpage once the conversion is complete but it doesn't seem to work.

Php code:

if (isset($_POST['id']))
{
    for($i=0; $i <count($mp4_files); $i++) {
        if($i == $_POST['id']) {
            $f = $mp4_files[$i];
            $parts = pathinfo($f); 
            switch ($parts['extension'])
            {  
            case 'mp4' :
                $filePath = $src_dir . DS . $f;

                print_r($f);  // Line A        

                system('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3', $result);  // Line B

                if($result)
                {
                    echo "Conversion Completed";
                }

            }
        }
    }
} 

Problem Statement:

I am wondering what changes I should make in the PHP code above so that once the conversion is complete; on the webpage, it should print Conversion Completed.

  • 写回答

1条回答 默认 最新

  • dongyuan2652 2019-06-11 19:08
    关注

    You can use shell_exec to get a return value and then put that in an if statement like this

    $output = shell_exec('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3');
    if ($output) {
       echo "Conversion Completed!";
       // or redirect here
    }
    

    Also, make sure to sanitize your inputs as they are exposed to a CLI interface.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog