how to compress video in php, write code in php

<?php

// Set the path to the input video file and the output video file
$input_video = '/path/to/input.mp4';
$output_video = '/path/to/output.mp4';

// Use shell_exec() to run the ffmpeg command to compress the video
$command = "ffmpeg -i $input_video -vf scale=640:-1 -c:v libx264 -crf 20 $output_video";
$output = shell_exec($command);

// Check the output for any errors
if (strpos($output, 'Error') !== false) {
    echo "An error occurred: $output";
} else {
    echo "Video was compressed successfully!";
}

?>

This code uses the shell_exec() function to run the ffmpeg command-line utility to compress the video. The -vf scale=640:-1 option specifies the width of the output video (640 pixels) and the height is calculated automatically to maintain the aspect ratio of the input video. The -crf option sets the quality of the output video, with lower values resulting in higher quality but larger file sizes.

You will need to have ffmpeg installed on your server in order to use this code. You can find more information about ffmpeg and its options at the following link:

ffmpeg.org/documentation.html