bping/ffmpeg-push

使用ffmpeg二进制文件将视频流推送到直播服务器

dev-master 2019-05-28 02:45 UTC

This package is not auto-updated.

Last update: 2024-09-29 02:45:21 UTC


README

Push video stream to live server with ffmpeg  binary

安装(安装)

composer require bping/ffmpeg-push dev-master

用法

首先

  • 安装 ffmpeg,并且必须包含ffprobe命令
  • 将可执行文件目录配置到环境变量PATH中
  • 安装ffmpeg,必须同时包含ffprobe命令
  • 将可执行文件目录配置到环境变量PATH中

推流

require __DIR__ . '/trunk/vendor/autoload.php';

use FFMpegPush\PushFormat;
use FFMpegPush\PushInput;
use FFMpegPush\PushOutput;
use FFMpegPush\PushVideo;

 ///** @var  $ffprobe FFProbeCommand */
 //$ffprobe = FFProbeCommand::create();
 //var_dump($ffprobe->format('test.mp4'));

// Push Command 推流命令
// ffmpeg -re  -i  \"test/test.mp4\" -c:v copy -c:a copy -f flv rtmp://pili-publish.heliwebs.com
 $pushUrl = 'rtmp://pili-publish.heliwebs.com';
 $pushCmd = PushVideo::create();
 // listening  the progress of push flow  监听推流进度
 $pushCmd->onProgress(function ($percent, $remaining, $rate) {
 //    var_dump(func_get_args());
     echo "progress:$percent% remaining:$remaining(s) rate:$rate(kb/s)\n";
 });
 
 $pushCmd->setInput(
     PushInput::create()
         ->setStartTime(0)
         ->setInputVideo('res/test.mp4')
 )
     ->setFormat(
         PushFormat::create()
             ->setVideoCodec(PushFormat::CODE_V_COPY)
     )
     ->setOutput(
         PushOutput::create()
             ->setPushUrl($pushUrl)
     );
 
 echo $pushCmd->getCommandLine();
 
 // start to push
 $pushCmd->push();
 
 echo $pushCmd->getErrorOutput();
 echo "\n";
 echo "Exit Code: " . $pushCmd->getExitCode();   
        
// Stop pushing, asynchronous calls are required 停止推流,需要异步调用 
// $pushCmd->stop();           

结果 PushInfo

// Is Successful 是否成功
  $pushinfo->isSuccessful()    
//输出
  $pushinfo->getOutput()    
// Error output 错误输出
  $pushinfo->getErrOutput()    
// 执行返回码
  $pushinfo->getExitCode()
// 目前推流时间,可以用中途断流重推起点时间
  $pushinfo->getCurrentTime()
// More infomation: PushInfo

输入 PushInput

  PushInput::create()
  ->setStartTime(10)
  ->setInputVideo('test/test.mp4')

转码 PushFormat

        PushFormat::create()
            ->setVideoCodec(PushFormat::CODE_V_COPY)
            ->setAudioCodec(PushFormat::CODE_A_COPY)
            ->setAudioKiloBitrate(125)
            ->setVideoKiloBitrate(500)
            ->setAdditionalParamaters(
                array(
                    '--preset',
                    'ultrafast',
                    ' --tune',
                    'zerolatency',
                )
            );

输出 PushOutput

 PushOutput::create()->setPushUrl($pushUrl)

获取视频文件信息

///** @var  $ffprobe FFProbeCommand */
$ffprobe = FFProbeCommand::create();
var_dump($ffprobe->format('test/test.mp4'));
var_dump($ffprobe->stream('test/test.mp4'));

配置

ffmpeg.binaries:

ffmpeg命令的名称或路径。如果您想使用简单名称,请记住将ffmpeg添加到环境变量PATH中。默认值:ffmpeg

ffmpeg命令名称或路径。如果您想使用简单名称,请记住将ffmpeg添加到环境变量PATH中。默认值:ffmpeg

ffprobe.binaries:

ffprobe命令的名称或路径。如果您想使用简单名称,请记住将ffprobe添加到环境变量PATH中。默认值:ffprobe

ffprobe命令名称或路径。如果您想使用简单名称,请记住将ffprobe添加到环境变量PATH中。默认值:ffprobe

timeout:

命令执行的超时时间,单位(秒)。考虑到推流时间通常较长,默认值为一整天。

命令执行的超时时间,单位(秒)。考虑到推流时间通常较长,所以默认值为一天。

$pushCmd = PushVideo::create(Configuration::create(
    array(
    'ffmpeg.binaries'=>array('ffmpeg'),
    'ffprobe.binaries'=>array('ffprobe'),
    'timeout'=>10800,
    )
));

主要依赖