为什么exec()不能在foreach循环中工作?

时间:2017-12-07 06:48:25

标签: php loops foreach shell-exec php-ffmpeg

如果我试图仅使用一个视频文件静态运行PHP exec(),那么请参阅测试目的,然后完美地压缩视频(请参见第一行)。

稍后当我在循环中动态压缩时,exec函数不起作用。请告诉我它为什么不在循环中工作?

// echo shell_exec("/usr/local/bin/ffmpeg -i /home4/machine/public_html/riyaz/check_video/video_1494453415.mp4 -strict -2 -crf 20 /home4/machine/public_html/riyaz/zcompress/video_1494453415.mp4");

@include_once("start.php");

$directory_path = FILEPATH.'/';

$video_mp4 = glob("/home4/machine/public_html/riyaz/check_video/*.mp4");

/*video size will show in mb*/
foreach ($video_mp4 as $video_mp4_list){

    $video_size = filesize($video_mp4_list);
    $video_size_in_mb = round(($video_size/1048576), 2);

    $get_file_name = explode('/',$video_mp4_list);
    $get_file_name_for_destination = $get_file_name[6];

    $getSourceFileNamePath = '/home4/machine/public_html/riyaz/check_video/'.$get_file_name_for_destination;
    $getDestFileNamePath = '/home4/machine/public_html/riyaz/zcompress/'.$get_file_name_for_destination;

    if ($video_size_in_mb >= 1000 ){

        echo exec("/usr/local/bin/ffmpeg -i ". $getSourceFileNamePath ." -strict -2 -crf 20 ".$getDestFileNamePath);
    }
}

1 个答案:

答案 0 :(得分:-1)

shell_exec()会返回完整输出:PHP shell_exec()但是 exec()只返回outptut中可能为空的最后一行。所以你必须提供第二个$ output和第三个参数$ return_var来获取更多有用的数据:PHP exec()

if ($video_size_in_mb >= 1000 ){
  $command = "/usr/local/bin/ffmpeg -y -i ". $getSourceFileNamePath ." -strict -2 -crf 20 ".$getDestFileNamePath . ' 2>&1';
  echo PHP_EOL ."Executing command: ". $command;
  if(file_exists($getDestFileNamePath)) echo "File already exists!";
  $output = null;
  $return_var = null;
  exec($command, $output, $return_var);
  print_r($command);
  print_r($return_var);
  print_r($output);
  echo PHP_EOL;
} else {
  echo "video too small to proceed';
}

php shell_exec() vs exec()

编辑:问题是目标文件存在且ffmpeg退出而没有任何操作。一种解决方案是使用属性-y覆盖输出文件5.4 Main options -y (global) Overwrite output files without asking.