maplesnow / php-ffmpeg
PHP FFMpeg - 视频转码与流媒体传输
Requires
- php: ^7.0
- ext-openssl: *
- php-ffmpeg/php-ffmpeg: ^0.14
- qiniu/php-sdk: ^7.2
Suggests
- php-ffmpeg/php-ffmpeg: php-ffmpeg/php-ffmpeg is required for video support.
This package is auto-updated.
Last update: 2024-09-29 06:08:30 UTC
README
该工具由aminyazdanpanah/PHP-FFmpeg-video-streaming改写而成,兼容php7.0及以上版本。
将ffmpeg常用操作封装简化,支持视频转码(video->mp4)和视频流传输(video->dash、video->hls)功能。 视频文件可选自本地或者七牛,转化后文件也可选择保留本地目录或者存储到七牛云端。
要求
-
该工具要求7.0及以上。
-
依赖 FFMpeg。需要系统支持
FFMpeg
和FFProbe
。
安装
composer require maplesnow/php-ffmpeg-video-streaming
配置
指定ffmpeg初始配置参数
$config = [ 'ffmpeg.binaries' => '/usr/bin/ffmpeg', 'ffprobe.binaries' => '/usr/bin/ffprobe', 'timeout' => 3600, // The timeout for the underlying process 'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use ]; $ffmpeg = Streaming\FFMpeg::create($config);
打开文件
目前支持2种打开方式
本地
$video = $ffmpeg->open('/var/www/media/videos/test.mp4');
七牛云
$qiniu = new \Streaming\Qiniu($accessKey,$secretKey); $video = $ffmpeg->fromQiniu($qiniu,$url);
视频转码
mp4格式输出
$video->MP4()->X264()->save('mp4/demo.mp4');
DASH
Dynamic Adaptive Streaming over HTTP (DASH),也称为MPEG-DASH,是一种自适应比特率流媒体传输技术,能够通过传统的HTTP网站服务器提供高质量的互联网流媒体内容。
$video->DASH() ->HEVC() // Format of the video. Alternatives: X264() and VP9() ->autoGenerateRepresentations() // Auto generate representations ->setAdaption('id=0,streams=v id=1,streams=a') // Set the adaption. ->save(); // It can be passed a path to the method or it can be null
自定义Representation
参数
use Streaming\Representation; $rep_1 = (new Representation())->setKiloBitrate(800)->setResize(1080 , 720); $rep_2 = (new Representation())->setKiloBitrate(300)->setResize(640 , 360); $video->DASH() ->HEVC() ->addRepresentation($rep_1) // Add a representation ->addRepresentation($rep_2) ->setAdaption('id=0,streams=v id=1,streams=a') // Set a adaption. ->save('dash/test.mpd');
更多参数详见 DASH选项
HLS
HTTP Live Streaming (也称为HLS)是由Apple Inc.作为其QuickTime、Safari、OS X和iOS软件的一部分实现的基于HTTP的自适应比特率流媒体传输通信协议。客户端实现还可在Microsoft Edge、Firefox和某些版本的Google Chrome中找到。该协议在流媒体服务器中得到了广泛的支持。
$video->HLS() ->X264() ->autoGenerateRepresentations([720, 360]) // You can limit the numbers of representatons ->save();
自定义Representation
参数
use Streaming\Representation; $rep_1 = (new Representation())->setKiloBitrate(1000)->setResize(1080 , 720); $rep_2 = (new Representation())->setKiloBitrate(500)->setResize(640 , 360); $rep_3 = (new Representation())->setKiloBitrate(200)->setResize(480 , 270); $video->HLS() ->X264() ->setHlsBaseUrl('https://bucket.s3-us-west-1.amazonaws.com/videos') // Add a base URL ->addRepresentation($rep_1) ->addRepresentation($rep_2) ->addRepresentation($rep_3) ->setHlsTime(5) // Set Hls Time. Default value is 10 ->setHlsAllowCache(false) // Default value is true ->save();
注意:不能使用HEVC
和VP9
格式。
HLS加密
加密过程需要某种类型的密钥(密钥)以及加密算法。HLS使用AES加密块链接(CBC)模式。这意味着每个块都是使用前一个块的密文进行加密的。了解更多
您需要将密钥的URL
和保存随机密钥的路径
传递给generateRandomKeyInfo
方法
//A path you want to save a random key on your server $save_to = '/var/www/my_website_project/keys/enc.key'; //A URL (or a path) to access the key on your website $url = 'https://www.aminyazdanpanah.com/keys/enc.key';// or '/keys/enc.key'; $video->HLS() ->X264() ->setTsSubDirectory('ts_files')// put all ts files in a subdirectory ->generateRandomKeyInfo($url, $save_to) ->autoGenerateRepresentations([1080, 480, 240]) ->save('/var/www/media/videos/hls/test.m3u8');
注意:在您的网站上使用令牌或会话/cookie保护您的密钥非常重要(强烈推荐)。
有关更多信息,请参阅 HLS选项
保存文件
两种文件存储方式
本地保存
指定目录保存文件
$dash = $video->DASH() ->HEVC() ->autoGenerateRepresentations() ->setAdaption('id=0,streams=v id=1,streams=a'); $dash->save('dash/test.mpd');
默认保存目录为文件来源目录
$hls = $video->HLS() ->X264() ->autoGenerateRepresentations(); $hls->save();
保存至七牛云
$mp4 = $video->MP4()->X264(); $qiniu = new \Streaming\Qiniu($accessKey,$secretKey); $mp4->save2qiniu($qiniu,'bucket-name')
开源的播放插件
您可以使用以下播放器播放您转换的流文件