Increasingly, video is the main content of the Internet. As a web application developer, you must be able to integrate video in your PHP projects. The benefits of integrating video into web applications are numerous. For one, it helps create a more dynamic and memorable experience for the user. It also makes the content more engaging and interactive which results in better user engagement.

How can PHP scripts manipulate video and audio?

PHP is a general-purpose, server-side programming language aimed to web development. It is not specifically built for audio/visual media creation or edition, but it can be integrated with other tools, APIs, SDKs and other applications to do the trick.

The most common alternatives you have for video and audio editing from PHP are (in order of complexity):

What to consider when editing videos with PHP?

As a developer, you need to decide what your best approach is depending on your own skills, the performance you need and your budget.

Difficulty

Depending on your knowledge of video technology, your sysops skills and the ability to admin your server you can choose one or another. For example, if you don't know anything about video technology and you don't have sysops skills, the cloud-based solutions are the best choice for you. If you have sysops skills and administration access to your servers you can try the self-hosted solutions.

Performance

Matching the performance of the cloud-based solutions on your own servers can be challenging and definitely more expensive. If you are taking the video processing or editing seriously in your project, you should probably go with a video cloud solution. If your are just learning and investigating, go with a DIY self-hosted environment.

Cost

When you need to process video on your web server, it's possible that you can’t do it with your current hosting solution, and you need something with a faster CPU and/or more RAM. If you are building a B2C application that must run several video-editing processes in parallel, the scalability and cost of such infrastructure can be prohibitive. Even it’s true that cloud-based solutions have paid plans for big volumes, they usually have generous free entry plans that can cover your initial requirements, and as your requirements grow you can use a pay-as-you-go plan to control your costs.

Cloud-based video libraries for PHP

When you choose to use a cloud-based video solution, you just need to install a PHP library or SDK and all the editing is done remotely in the cloud. It can be used in any PHP-hosting, with no admin rights and no Sysop skills.

Let’s see an example using JSON2Video PHP SDK:

1. Install the SDK:

composer require json2video/json2video-php-sdk

2. Simple "Hello world" example

This example creates a 10 seconds video with a "Hello world" text

As you can see it's really straight forward, and you can probably create your own videos in just a few minutes. You can check other examples for trimming, adding watermarks, cropping, creating slideshows or adding voice-over in the tutorial.

JSON2Video has a free plan with 1,000 API calls per month. Get your API Key and test it out.

Self-hosted video libraries for PHP

There are multiple video libraries for PHP that mainly all do the same: they encapsulate the execution of a third-party command-line tool like FFMPEG.

What is FFMPEG?

FFMPEG is a powerful set of tools that can convert audio and video formats. It can also combine videos into one file, extract audio tracks, or compress the data in a video. FFMPEG is open-source and free to use, and it's so powerful that is used by most of the professional video products out there. But such power comes with a step learning curve, cryptic parameters and some frustration by newbies.

What these libraries try to do is to simplify the calls to FFMPEG and save you the learning curve of understanding the FFMPEG cryptic parameters.

Top PHP video libraries:

All these libraries require you to install FFMPEG locally in your server, something that could be easier or harder depending on your back-end setup and your privileges accessing/configuring the server.

Self-hosted command-line tools

As said, the top video editing tool is FFMPEG, and it's the one you should use if you want to set up your own video processing platform. FFMPEG is open source, so you can compile it yourself or you can directly download the binaries you need for Linux, Windows or MacOS.

FFMPEG is a command-line tool you can run from the console or execute it from your PHP script, following this format:

ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...

How to run FFMPEG from a PHP script?

To run FFMPEG from your PHP script you must use the PHP function exec:

<?php
    exec(string $command, array &$output = null, int &$result_code = null): string|false

Where:

Example:

<?php
    exec('ffmpeg -i input.avi -b:v 64k -bufsize 64k output.avi');

Final thoughts on editing videos using PHP scripts

Although all necessary technology is openly available, the learning curve and cost to have your own high performing video processing infrastructure is challenging, and can require several weeks and expensive back-end servers.

On the other hand, cloud-based solutions like JSON2Video provide a instantly available, highly-scalable and easy to use platform that covers all your needs for creating any PHP video application.

Published on January 6th, 2022

Author
David Bosch
David Bosch David is an experienced engineer, now collaborating with JSON2Video.