php rmtp实现
PHP 实现 RTMP 推流
RTMP(Real-Time Messaging Protocol)通常用于直播推流。PHP 本身不直接支持 RTMP 协议,但可以通过调用外部工具或扩展实现。
安装 FFmpeg 并推流 确保服务器安装了 FFmpeg,通过命令行调用 FFmpeg 实现 RTMP 推流:
$ffmpegPath = '/usr/bin/ffmpeg';
$inputFile = '/path/to/input.mp4';
$rtmpUrl = 'rtmp://example.com/live/streamkey';
$command = "$ffmpegPath -re -i $inputFile -c:v libx264 -preset veryfast -maxrate 3000k -bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 -ar 44100 -f flv $rtmpUrl";
shell_exec($command);
使用 PHP-FFmpeg 库
通过 Composer 安装 PHP-FFmpeg 库:
composer require php-ffmpeg/php-ffmpeg
代码示例:
require 'vendor/autoload.php';
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('/path/to/input.mp4');
$video->save(new FFMpeg\Format\Video\FLV(), 'rtmp://example.com/live/streamkey');
使用 Nginx RTMP 模块
配置 Nginx 的 RTMP 模块接收流:
rtmp {
server {
listen 1935;
application live {
live on;
allow publish 127.0.0.1;
deny publish all;
}
}
}
PHP 通过 FFmpeg 推流到本地 Nginx RTMP 服务器。
注意事项
- RTMP 推流需要稳定的网络带宽。
- 确保服务器防火墙允许 RTMP 端口(默认 1935)。
- 推流前测试 FFmpeg 命令是否正常工作。
- 对于 HLS 等现代协议,可能需要额外配置。







