Download the PHP package tbritz/ffmphp without Composer
On this page you can find all versions of the php package tbritz/ffmphp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tbritz/ffmphp
More information about tbritz/ffmphp
Files in tbritz/ffmphp
Package ffmphp
Short Description Convert media, save previews, and more. All with clean, elegant syntax that won't get in your way.
License MIT
Informations about the package ffmphp
FFmphp
A simple, clean, and elegant way to run FFmpeg from your php applications.
Installation
You will need a working install of FFmpeg (and FFprobe) in your system (https://www.ffmpeg.org/download.html).
This library can be installed via composer:
As long as ffmpeg
and ffprobe
are available from your system's PATH variable, this library should use them automatically. If not, you can configure FFmphp before using it:
These settings will be applied to all FFmphp calls for the remainder of the script execution.
Basic Usage
Video Transcoding
Getting started is easy. Converting a video can be as simple as this command:
FFmpeg will guess the output type based on the extension.
Most of the time, however, you will give save()
the name of an OutputFormat
:
For the rest of the document we will use php's ::class
syntax instead of providing the fully qualified class name of the format. Here is what the previous example looks like when it is rewritten:
Extracting Audio Tracks from Video
Working with audio formats is just as easy:
Saving Thumbnails
Saving a video poster, or thumbnail image, works the same way:
Saving Tiled/Mosaic Images
You guessed it:
Save Multiple Files with One Command
You can add an arbitrary number of outputs with one command. For example:
The above command would run FFmpeg once and create 5 files.
Note: This is only provided as an example. Although the code may be more readable and make it easier to reason about the overall progress (or failures) during conversion, actual memory usage of FFmpeg increases for every output stream. In many cases the number of outputs you can chain on a single command instance will be limited by the available system memory and the resolution of your video streams.
Advanced Usage
Output Formats
The best way to keep your project organized is to create an OutputFormat
class for every type of file that you will save. An output format includes codecs, bitrate, container, resolution, and more. Don't worry, it's actually much simpler than it sounds!
To make it easier to start writing code, these formats are included:
FFmphp\Formats\Video\MP4
FFmphp\Formats\Video\Webm
FFmphp\Formats\Video\HLS
(m3u8)FFmphp\Formats\Audio\MP3
FFmphp\Formats\Image\Poster
(Thumbnail)FFmphp\Formats\Image\TileFiveByFive
FFmphp\Formats\Image\TileFourByThree
You are welcome to use these classes in your own application, however in most cases it is better to use them only as a reference, and instead create your own formats which are tuned to the unique requirements of your application.
You can either create your class from scratch, or you may extend another format. Your class must implement the FFmphp\Formats\OutputFormat
interface, which has one method: build()
.
For example, you may wish to create a format which is specifically tuned for animation content by extending the existing MP4 class:
Or perhaps you want more control over the default encoder options. Here is an another complete example, this time creating a new MP4 format from scratch:
To use your output format, simply reference it in save()
:
Getting the Command
If you want to inspect the command that will be run (without running it), you can use the toCommand()
method:
Which will return a string like this:
Conditionally Adding Output Streams
You may want to add some output formats only when certain conditions are met. You can use when()
for those situations. For example, you might want to create a 1080p version of a video only when the source resolution was high enough:
The second parameter of when()
accepts a function that will only be applied when the first argument is true
.
Adding Encoder Options
The third parameter of save()
accepts an associative array of options that will be added to the output. In some cases this may be more convenient than creating a new class for each output type.
The third argument can also be a closure, if you need more control:
Using this feature excessively will make your code harder to read, which is why it is recommended instead to have a class for every type of output.
The Progress Callback
The run()
method accepts an optional callback function, which will be run approximately once every second for as long as FFmpeg is running. The function will receive the current time position of the input as reported by FFmpeg, formatted like 00:00:00.00
.
For example:
Raw Commands
The raw()
method is perfect if you need more control, have complicated filters, or already know FFmpeg and just want the convenience of the php integration. The raw()
method takes an array of arguments to be passed to FFmpeg.