Install and Use FFmpeg with PHP‑FFMpeg on Ubuntu
This guide explains what FFmpeg is, shows how to install it on Ubuntu 18.04, demonstrates integrating the Webman framework and PHP‑FFMpeg library, and provides step‑by‑step code examples for extracting images, adding watermarks, and basic video editing.
Overview
FFmpeg is an open‑source suite for recording, converting, and streaming digital audio and video. It provides the libavcodec library with many codecs written from scratch for portability and quality. Although originally developed for Linux, FFmpeg can be built on Windows, macOS and other operating systems.
GitHub releases: https://github.com/BtbN/FFmpeg-Builds/releases
Installation on Ubuntu 18.04
Install the pre‑built binaries from the Ubuntu repositories: sudo apt-get install ffmpeg Verify the installation:
ffmpeg -version
ffprobe -versionIntegration with the Webman framework
Install Webman
$ composer create-project workerman/webman webman2024Install PHP‑FFMpeg
Requires PHP 8.0 or higher; the examples use PHP 8.2.14.
composer require php-ffmpeg/php-ffmpegIf a specific PHP binary is needed, invoke Composer with that binary:
/usr/local/php-8.2.14/bin/php /home/www/build/composer.phar require php-ffmpeg/php-ffmpegExample 1 – Extract a frame from an MP4 video
/**
* @desc Example 1: Extract a single image from an MP4 video
*/
public function ffmpeg(Request $request)
{
// 1. Initialise FFmpeg configuration
$ffmpeg = \FFMpeg\FFMpeg::create([
'ffmpeg.binaries' => '/usr/bin/ffmpeg',
'ffprobe.binaries'=> '/usr/bin/ffprobe'
]);
// 2. Open the source video
$video = $ffmpeg->open(runtime_path() . DIRECTORY_SEPARATOR . 'tinywan.mp4');
// 3. Capture a frame at 20 seconds and save it as an image
$video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(20))
->save(runtime_path() . DIRECTORY_SEPARATOR . 'tinywan.jpg');
}Test endpoint: curl http://127.0.0.1:8888/index/ffmpeg
The command returns the internal FFmpeg object structure (truncated for brevity).
Resulting screenshot captured by FFmpeg:
Example 2 – Add a watermark to an MP4 video
/**
* @desc Example 2: Add a watermark to an MP4 video
*/
public function watermark(Request $request)
{
// 1. Initialise FFmpeg configuration
$ffmpeg = \FFMpeg\FFMpeg::create([
'ffmpeg.binaries' => '/usr/bin/ffmpeg',
'ffprobe.binaries'=> '/usr/bin/ffprobe'
]);
// 2. Open the source video
$video = $ffmpeg->open(runtime_path() . DIRECTORY_SEPARATOR . 'tinywan.mp4');
// 3. Path to the watermark image
$watermarkPath = runtime_path() . DIRECTORY_SEPARATOR . 'tinywan-watermark.png';
// 4. Apply the watermark filter (relative positioning, 120 px from bottom and right)
$video->filters()->watermark($watermarkPath, [
'position' => 'relative',
'bottom' => 120,
'right' => 120,
]);
// 5. Choose an output format (x264 for H.264/AVC)
$format = new \FFMpeg\Format\Video\X264();
// 6. Save the watermarked video
$video->save($format, runtime_path() . DIRECTORY_SEPARATOR . 'tinywan-watermark.mp4');
}Test endpoint: curl http://127.0.0.1:8888/index/watermark (processing time depends on video size).
The output file tinywan-watermark.mp4 now contains the "Open Source Technology Stack" logo as a watermark.
Example 3 – Basic video clipping (code omitted)
The article mentions a third example for video clipping, but the source code was not provided.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
