-1

See, for testing purpose if I am trying to run PHP exec() statically with only one video file, then its compressing video perfectly(please see first line line).

Later when I am compressing dynamically in the loop, then exec function is not working. Please tell me why it's not working in the loop?

// 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);
    }
}
Riyaz Khan
  • 23
  • 1
  • 9

1 Answers1

-1

shell_exec() returns the full output: PHP shell_exec() however
exec() returns only the last line from the outptut that might be empty.. So you have to provide second $output and third parameter $return_var to get more useful data: 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()

EDIT: The problem is that the destination file exists and ffmpeg exits without any action. One solution is to use attribute -y to overwrite the output file 5.4 Main options -y (global) Overwrite output files without asking.

ino
  • 2,345
  • 1
  • 15
  • 27
  • Its showing 1Array ( ) 1Array ( ) in output. Blank array showing. – Riyaz Khan Dec 07 '17 at 07:28
  • @Riyaz good, 1 is the $return_var, Array() is the $output which is empty. Search for ffmpeg return code what does mean the `1`. – ino Dec 07 '17 at 07:32
  • From the PHP exec() documentation of return_var: " return_var If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable. " – ino Dec 07 '17 at 07:47
  • ok, let's see what is the `$command`, I have updated my answer with `echo PHP_EOL . "Executing command: ". $command;` . Post here the output, pls – ino Dec 07 '17 at 09:42
  • The only difference is the user name riyaz vs sterlingadmin. Has PHP access to both locations? – ino Dec 07 '17 at 10:54
  • I am really sorry @ino.. I forget to reply. Please check my output. Executing command: /usr/local/bin/ffmpeg -i /home4/machine/public_html/riyaz/check_video/video_1‌​494453415.mp4 -strict -2 -crf 20 /home4/machine/public_html/riyaz/zcompress/video_149‌​4453415.mp4 Executing command: /usr/local/bin/ffmpeg -i /home4/machine/public_html/riyaz/check_video/video_1‌​498257113.mp4 -strict -2 -crf 20 /home4/machine/public_html/riyaz/zcompress/video_149‌​8257113.mp4 – Riyaz Khan Dec 07 '17 at 10:55
  • ok, then the only difference between your example and the actual code is the used function `shell_exec` vs `exec`. Use the same function in foreach as in the example, ie `shell_exec`. – ino Dec 07 '17 at 11:02
  • oh, sorry, only `shell_exec($command);` as the shell_exec does not support more parameters as exec(). – ino Dec 07 '17 at 11:06
  • And the file and output dir exists, right? `var_dump(file_exists($getSourceFileNamePath)); var_dump(file_exists($get_file_name_for_destination));` – ino Dec 07 '17 at 11:16
  • Yes file and directory exists. For $get_file_name_for_destination its showing false. – Riyaz Khan Dec 07 '17 at 11:21
  • It will give false bcz there is no file yet. When video will be comressed then that file should be move there. So as of now there is no video thats why it showing false. – Riyaz Khan Dec 07 '17 at 11:22
  • One more thing we can test is to add ` 2>&1` to the end of the $command in my example, ie change ` $command = "/usr/local/bin/ffmpeg -i ". $getSourceFileNamePath ." -strict -2 -crf 20 ".$getDestFileNamePath .' 2>&1';` and run the code from my example using `exec()` to see what will be in the output now. – ino Dec 07 '17 at 11:37
  • This one also not working bro...string(194) "/usr/local/bin/ffmpeg -i /home4/machine/public_html/sterlingadmin/check_video/video_1494453415.mp4 -strict -2 -crf 20 /home4/machine/public_html/sterlingadmin/zcompress/video_1494453415.mp4 2>&1" string(194) "/usr/local/bin/ffmpeg -i /home4/machine/public_html/sterlingadmin/check_video/video_1498257113.mp4 -strict -2 -crf 20 /home4/machine/public_html/sterlingadmin/zcompress/video_1498257113.mp4 2>&1" – Riyaz Khan Dec 07 '17 at 11:42
  • ok, I have updated the code example in my answer above. Run it an paste the outpt. – ino Dec 07 '17 at 11:47
  • Ok, I see the error. The destination file already exists. I added `-y` as a parameter to ffmpeg command - it should overwrite the file. Or delete the file manually. The error is: '/home4/machine/public_html/sterlingadmin/zcompress/video_1498257113.mp4' already exists. Overwrite ? [y/N] Not overwriting - exiting – ino Dec 07 '17 at 12:03
  • The document you have linked from docs contains printed errors from ffmpeg. Just search the document for string: "already exists". Proposed solution is to add attribute `-y` in the $command. I have already updated the code in my answer above. – ino Dec 07 '17 at 12:18
  • Actually, as I run this file via browser, it creates file at the same time with 0 byte size. – Riyaz Khan Dec 07 '17 at 12:20
  • As we saw overwrite problem, its happened because when I run my file, it fails to compress video(That's what we are talking about), so it create 0 bytes files. So, I need to delete manually. – Riyaz Khan Dec 07 '17 at 12:26
  • Apart from that, I would like to inform you that my video will be compressed when I run file via terminal. If everything is okay then terminal start compressing video. – Riyaz Khan Dec 07 '17 at 12:29