为什么 exec() 在 foreach 循环中不起作用?

Why exec() is not working in foreach loop?

请看,出于测试目的,如果我尝试仅使用一个视频文件静态地 运行 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);
    }
}

shell_exec() returns 完整输出:PHP shell_exec() 然而
exec() returns 只有输出的最后一行可能是空的。所以你必须提供第二个 $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.