Skip to content Skip to sidebar Skip to footer

(PHP) Trim videos easily with FFMPEG

FFMPEG logo

Editing video is not easy and of course you are already used to using some kind of application / software that has functionality Cut or trim video with one click. Most applications or software use ffmpeg to make it easier to trim a video. And here I will explain a little how to do it Cut videos in PHP using ffmpeg.

Sometimes, with an application or software that was not created by itself, there are flaws in the video results, and it is possible that the video has been compressed due to the bit rate and others. Of course, many people don’t like it when the results of the cut videos are of a different quality or worse than the original. But now if you want to cut the video to the same quality as the original, just provide PHP and FFMPEG.

Before doing this, make sure you install ffmpeg on the serverFor those using hosting, you can install through the SSH shell or ask the hosting staff to instruct the server to install. Because ffmpeg is not a native function of PHP, but a binary file (.exe) that can be called in PHP with the help of exec (). And actually not just the exec () function, but a passthru (), shell_exec () like function that is used to execute files via commands.
When the server is ready to use, please note a small code below.

diff($sc);return $diff->format('%H:%I:%S');}$ffmpeg = '/usr/local/bin/ffmpeg';$video = '/home/user/public_html/video.avi';$hasil="/home/user/public_html/output.avi";$dari = '01:00:00';$hingga="01:30:00";$parsing = _str($dari, $hingga);echo exec("$ffmpeg -i $video -ss $dari -t $parsing -vcodec copy -acodec copy $hasil");?>

Explanation

Function _str – Used to determine the interval (difference) between the time before and after in terms of hours: minutes: seconds. We recommend that this function section does not need to be changed.

$ ffmpeg – Change the directory in which ffmpeg is installed. If you’re using Linux, don’t change anything.

$ video – Change according to the directory of the video file to be cut. We recommend using fullpath as in the example above.

$ Output – Change accordingly to the directory in which the cut video is saved. We recommend using fullpath as in the example above.

$ from – change according to the start time of the video you want to cut.

$ up to – change according to the end time of the video you want to cut.

$ parsing – is used to get the time difference between the strings $ to and $ from with the help of the _str function.

executive – To execute commands from ffmpeg.

With the code above, you can cut videos without worrying about the video quality, as the quality that will be produced will be the same as the original video. And on purpose Use time difference because of the order -T in ffmpeg is used to remove the video to be generated. There are so many mistakes in using the command -T as the end of the video to be trimmed, and the result is sure to surprise.

You can also Remove video sound by adding a command -a. The time format in the strings $ from and $ to is hours: minutes: seconds. The above was set for a time difference of 30 minutes.

It’s actually quite easy to trim a video with ffmpeg, just hard for me to explain, but it’s fine as long as it’s understandable. ️
If you have any questions please comment.

Hopefully useful and good luck