视频处理已经成为现代 Web 应用的“标配”,从社交媒体到在线教育:格式转换、缩略图抽取、压缩优化、音轨处理与合成,都离不开稳定强大的工具链。FFmpeg 作为事实标准,功能强大但命令行参数繁多;在 PHP 中直接集成若处理不当,容易踩到错误处理、资源管理与安全风控的坑。
本文给出一套面向生产的实践指南,带你快速、稳健地将 FFmpeg 与 PHP 集成,覆盖常用库选择、安装与环境准备、核心用法、进阶技巧、性能优化、安全要点与常见故障排查。配合完整的代码示例,你可以在短时间内搭建可靠的音视频处理能力。
FFmpeg 是跨平台的音视频录制、转换与流媒体处理套件,是诸多应用(如 YouTube、Netflix、VLC)的底层基石。它支持数百种编解码器与容器格式,是多媒体处理领域的事实标准。
通过 exec()
/shell_exec()
直接调用 FFmpeg 往往会遇到:
最流行且维护活跃的 OO 封装库,大幅简化常见视频任务。
安装(Composer):
composer require php-ffmpeg/php-ffmpeg
基础示例:
<?php
require 'vendor/autoload.php';
use FFMpeg\FFMpeg;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Format\Video\X264;
// 初始化 FFMpeg
$ffmpeg = FFMpeg::create([
'ffmpeg.binaries' => '/usr/local/bin/ffmpeg',
'ffprobe.binaries' => '/usr/local/bin/ffprobe',
'timeout' => 3600,
'ffmpeg.threads' => 12,
]);
// 打开视频文件
$video = $ffmpeg->open('input.mp4');
// 转换为另一种格式
$format = new X264('aac');
$format->setKiloBitrate(1000)
->setAudioChannels(2)
->setAudioKiloBitrate(256);
$video->save($format, 'output.mp4');
特性亮点
通过编译 PHP 扩展直接调用 FFmpeg 库,部署复杂度更高,但高吞吐下性能更优。
安装依赖(以 Debian/Ubuntu 为例):
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
git clone https://github.com/char101/ffmpeg-php.git
cd ffmpeg-php
phpize
./configure
make && sudo make install
用法示例:
<?php
$movie = new ffmpeg_movie('input.mp4');
echo "Duration: " . $movie->getDuration() . " seconds\n";
echo "Frame count: " . $movie->getFrameCount() . "\n";
echo "Frame rate: " . $movie->getFrameRate() . " fps\n";
// 在第 10 秒抽取一帧
$frame = $movie->getFrame(10);
if ($frame) {
$gd_image = $frame->toGDImage();
imagepng($gd_image, 'thumbnail.png');
}
主打轻量与易用,适合基础处理任务。
安装:
composer require streamio/ffmpeg
简单转换示例:
<?php
use Streamio\FFMpeg;
$ffmpeg = new FFMpeg('/usr/local/bin/ffmpeg');
$ffmpeg->convert()
->input('input.avi')
->output('output.mp4')
->go();
Ubuntu/Debian:
sudo apt update
sudo apt install ffmpeg
CentOS/RHEL:
sudo yum install epel-release
sudo yum install ffmpeg
macOS(Homebrew):
brew install ffmpeg
<?php
use FFMpeg\FFMpeg;
use FFMpeg\Format\Video\WebM;
use FFMpeg\Format\Video\MP4;
class VideoConverter
{
private $ffmpeg;
public function __construct()
{
$this->ffmpeg = FFMpeg::create([
'ffmpeg.binaries' => '/usr/bin/ffmpeg',
'ffprobe.binaries' => '/usr/bin/ffprobe',
'timeout' => 3600,
'ffmpeg.threads' => 8,
]);
}
public function convertToMP4($inputPath, $outputPath, $quality = 'medium')
{
try {
$video = $this->ffmpeg->open($inputPath);
$format = new MP4('aac', 'libx264');
// 设置质量参数
switch ($quality) {
case 'high':
$format->setKiloBitrate(2000);
break;
case 'medium':
$format->setKiloBitrate(1000);
break;
case 'low':
$format->setKiloBitrate(500);
break;
}
$video->save($format, $outputPath);
return ['success' => true, 'message' => 'Conversion completed'];
} catch (Exception $e) {
return ['success' => false, 'error' => $e->getMessage()];
}
}
}
// 用法
$converter = new VideoConverter();
$result = $converter->convertToMP4('input.avi', 'output.mp4', 'high');
<?php
use FFMpeg\FFMpeg;
use FFMpeg\Coordinate\TimeCode;
class ThumbnailGenerator
{
private $ffmpeg;
public function __construct()
{
$this->ffmpeg = FFMpeg::create();
}
public function generateThumbnails($videoPath, $outputDir, $count = 5)
{
try {
$video = $this->ffmpeg->open($videoPath);
$duration = $video->getFFProbe()
->format($videoPath)
->get('duration');
$interval = $duration / ($count + 1);
$thumbnails = [];
for ($i = 1; $i <= $count; $i++) {
$timeSeconds = $interval * $i;
$outputPath = $outputDir . '/thumb_' . $i . '.jpg';
$video->frame(TimeCode::fromSeconds($timeSeconds))
->save($outputPath);
$thumbnails[] = $outputPath;
}
return ['success' => true, 'thumbnails' => $thumbnails];
} catch (Exception $e) {
return ['success' => false, 'error' => $e->getMessage()];
}
}
}
<?php
use FFMpeg\FFProbe;
class VideoAnalyzer
{
private $ffprobe;
public function __construct()
{
$this->ffprobe = FFProbe::create();
}
public function getVideoInfo($videoPath)
{
try {
$format = $this->ffprobe->format($videoPath);
$videoStream = $this->ffprobe->streams($videoPath)
->videos()
->first();
$audioStream = $this->ffprobe->streams($videoPath)
->audios()
->first();
return [
'success' => true,
'info' => [
'duration' => $format->get('duration'),
'size' => $format->get('size'),
'bitrate' => $format->get('bit_rate'),
'video' => [
'codec' => $videoStream->get('codec_name'),
'width' => $videoStream->get('width'),
'height' => $videoStream->get('height'),
'fps' => $videoStream->get('r_frame_rate'),
],
'audio' => $audioStream ? [
'codec' => $audioStream->get('codec_name'),
'channels' => $audioStream->get('channels'),
'sample_rate' => $audioStream->get('sample_rate'),
] : null
]
];
} catch (Exception $e) {
return ['success' => false, 'error' => $e->getMessage()];
}
}
}
<?php
use FFMpeg\FFMpeg;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Filters\Video\ResizeFilter;
use FFMpeg\Format\Video\X264;
class VideoResizer
{
private $ffmpeg;
public function __construct()
{
$this->ffmpeg = FFMpeg::create();
}
public function resizeVideo($inputPath, $outputPath, $width, $height, $mode = ResizeFilter::RESIZEMODE_INSET)
{
try {
$video = $this->ffmpeg->open($inputPath);
// 创建尺寸对象
$dimension = new Dimension($width, $height);
// 应用缩放滤镜
$video->filters()
->resize($dimension, $mode)
->synchronize();
// 保存为适当的格式
$format = new X264('aac');
$video->save($format, $outputPath);
return ['success' => true, 'message' => 'Video resized successfully'];
} catch (Exception $e) {
return ['success' => false, 'error' => $e->getMessage()];
}
}
public function createMultipleResolutions($inputPath, $outputDir)
{
$resolutions = [
'720p' => ['width' => 1280, 'height' => 720],
'480p' => ['width' => 854, 'height' => 480],
'360p' => ['width' => 640, 'height' => 360],
];
$results = [];
foreach ($resolutions as $name => $dimensions) {
$outputPath = $outputDir . '/' . $name . '_output.mp4';
$result = $this->resizeVideo(
$inputPath,
$outputPath,
$dimensions['width'],
$dimensions['height']
);
$results[$name] = $result;
}
return $results;
}
}
<?php
use FFMpeg\FFMpeg;
use FFMpeg\Format\Audio\Mp3;
use FFMpeg\Format\Audio\Wav;
class AudioProcessor
{
private $ffmpeg;
public function __construct()
{
$this->ffmpeg = FFMpeg::create();
}
public function extractAudio($videoPath, $outputPath, $format = 'mp3')
{
try {
$video = $this->ffmpeg->open($videoPath);
switch (strtolower($format)) {
case 'mp3':
$audioFormat = new Mp3();
$audioFormat->setAudioKiloBitrate(192);
break;
case 'wav':
$audioFormat = new Wav();
break;
default:
throw new Exception('Unsupported audio format');
}
$video->save($audioFormat, $outputPath);
return ['success' => true, 'message' => 'Audio extracted successfully'];
} catch (Exception $e) {
return ['success' => false, 'error' => $e->getMessage()];
}
}
public function adjustVolume($inputPath, $outputPath, $volumeLevel)
{
try {
$audio = $this->ffmpeg->open($inputPath);
// 应用音量滤镜
$audio->filters()
->custom("volume={$volumeLevel}");
$format = new Mp3();
$audio->save($format, $outputPath);
return ['success' => true, 'message' => 'Volume adjusted successfully'];
} catch (Exception $e) {
return ['success' => false, 'error' => $e->getMessage()];
}
}
}
<?php
use FFMpeg\FFMpeg;
use FFMpeg\Format\Video\X264;
class OptimizedVideoProcessor
{
private $ffmpeg;
private $maxMemoryUsage;
public function __construct($maxMemoryMB = 512)
{
$this->maxMemoryUsage = $maxMemoryMB * 1024 * 1024;
$this->ffmpeg = FFMpeg::create([
'ffmpeg.binaries' => '/usr/bin/ffmpeg',
'ffprobe.binaries' => '/usr/bin/ffprobe',
'timeout' => 3600,
'ffmpeg.threads' => min(4, cpu_count()),
]);
}
public function processWithMemoryCheck($inputPath, $outputPath)
{
// 处理前的内存检查
$memoryBefore = memory_get_usage(true);
if ($memoryBefore > $this->maxMemoryUsage * 0.8) {
return ['success' => false, 'error' => 'Insufficient memory'];
}
try {
$video = $this->ffmpeg->open($inputPath);
$format = new X264('aac');
$format->setKiloBitrate(1000);
$video->save($format, $outputPath);
// 强制释放
unset($video);
gc_collect_cycles();
return ['success' => true, 'message' => 'Processing completed'];
} catch (Exception $e) {
return ['success' => false, 'error' => $e->getMessage()];
}
}
}
<?php
use FFMpeg\Format\ProgressListener\AbstractProgressListener;
use FFMpeg\Format\Video\X264;
class ProgressTracker extends AbstractProgressListener
{
private $sessionId;
public function __construct($sessionId)
{
$this->sessionId = $sessionId;
}
public function handle($type, $format, $percentage)
{
// 将进度写入缓存/数据库
file_put_contents(
'/tmp/progress_' . $this->sessionId,
json_encode([
'type' => $type,
'format' => $format,
'percentage' => $percentage,
'timestamp' => time()
])
);
}
}
// 结合进度监听的用法
$progressTracker = new ProgressTracker('unique_session_id');
$format = new X264('aac');
$format->on('progress', $progressTracker);
$video->save($format, 'output.mp4');
<?php
use FFMpeg\FFMpeg;
use FFMpeg\Format\Video\X264;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class RobustVideoProcessor
{
private $ffmpeg;
private $logger;
public function __construct()
{
$this->ffmpeg = FFMpeg::create([
'timeout' => 3600,
]);
$this->logger = new Logger('video_processor');
$this->logger->pushHandler(new StreamHandler('/var/log/video_processing.log'));
}
public function safeProcessVideo($inputPath, $outputPath)
{
try {
// 基础校验
if (!file_exists($inputPath)) {
throw new Exception('Input file does not exist');
}
if (!is_readable($inputPath)) {
throw new Exception('Input file is not readable');
}
// 可用磁盘空间检查
$freeSpace = disk_free_space(dirname($outputPath));
$inputSize = filesize($inputPath);
if ($freeSpace < ($inputSize * 2)) {
throw new Exception('Insufficient disk space');
}
$this->logger->info('Starting video processing', [
'input' => $inputPath,
'output' => $outputPath
]);
$video = $this->ffmpeg->open($inputPath);
$format = new X264('aac');
$video->save($format, $outputPath);
$this->logger->info('Video processing completed successfully');
return ['success' => true, 'message' => 'Processing completed'];
} catch (Exception $e) {
$this->logger->error('Video processing failed', [
'error' => $e->getMessage(),
'input' => $inputPath,
'output' => $outputPath
]);
// 清理半成品
if (file_exists($outputPath)) {
unlink($outputPath);
}
return ['success' => false, 'error' => $e->getMessage()];
}
}
}
报错 “FFmpeg not found” 时,显式指定路径:
$ffmpeg = FFMpeg::create([
'ffmpeg.binaries' => '/usr/local/bin/ffmpeg', // 按需调整
'ffprobe.binaries' => '/usr/local/bin/ffprobe', // 按需调整
]);
长视频任务需提高超时时间:
$ffmpeg = FFMpeg::create([
'timeout' => 7200, // 2 小时
'ffmpeg.threads' => 4,
]);
设置合适的 PHP 限制并控制执行时长:
ini_set('memory_limit', '1G');
ini_set('max_execution_time', 3600);
确保目录可被 PHP 进程读写:
chmod 755 /path/to/videos/
chown www-data:www-data /path/to/videos/
严禁未净化的路径与文件名进入命令行。示例:
<?php
function validateVideoPath($path)
{
// 目录穿越
if (strpos($path, '..') !== false) {
throw new Exception('Invalid path');
}
// 扩展名校验
$allowedExtensions = ['mp4', 'avi', 'mov', 'mkv', 'webm'];
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
if (!in_array($extension, $allowedExtensions)) {
throw new Exception('Unsupported file format');
}
return true;
}
对文件大小与时长设置上限,避免滥用:
<?php
use FFMpeg\FFProbe;
class SecureVideoProcessor
{
private $maxFileSize = 100 * 1024 * 1024; // 100MB
private $maxDuration = 3600; // 1 小时
public function validateVideo($path)
{
$size = filesize($path);
if ($size > $this->maxFileSize) {
throw new Exception('File too large');
}
$probe = FFProbe::create();
$duration = $probe->format($path)->get('duration');
if ($duration > $this->maxDuration) {
throw new Exception('Video too long');
}
return true;
}
}
Q:最简单的入门方式是什么?
A: 使用 PHP-FFMpeg
通过 Composer 安装(composer require php-ffmpeg/php-ffmpeg
)。该库提供直观的 OO API,覆盖大多数常见任务,无需深入 FFmpeg 细节。
Q:如何处理大文件避免内存问题?
A: 合理设置 PHP 内存与超时;能流式就流式;实现进度监控;将长任务放入后台队列(如 Laravel Queue、Symfony Messenger),必要时分片处理。
Q:能否并发处理多段视频?
A: 可以,但务必限制并发度与系统资源占用。通过进程控制或作业队列协调,防止 CPU、内存、磁盘与 I/O 压垮系统。
Q:如何在不同服务器上统一 FFmpeg 安装?
A: 建议使用 Docker 做环境封装,或在部署流程中编写一致的安装脚本,固定 FFmpeg 版本与编译参数,并记录依赖。
Q:怎么优化视频处理性能?
A: 合理配置线程数与超时;选择高效编解码器与档位;缓存中间结果;监控系统资源(CPU/内存/磁盘/网络);按需横向扩展。
Q:允许用户上传并处理视频是否安全?
A: 严格做类型校验、大小/时长限制、路径净化、沙箱/隔离执行,避免命令注入。永远不要信任用户输入。
将 FFmpeg 集成进 PHP 能为你的应用解锁强大的多媒体处理能力。选择合适的库(多数场景推荐 PHP-FFMpeg
),建立完备的错误处理与安全策略,结合合理的性能优化与资源管理,即可在生产环境获得稳定可靠的效果。
从本文提供的示例开始,先让基础功能跑通,再按业务需求逐步扩展