php 读取视频流(mp4视频文件),快速读取视频解决方案(案例及配置)

在网站中,时长会有视频类的文件,但在读取视频时非常慢,现在处理流程和方案分享出来。

一、liunx 配置

1.1、打开文件 /etc/sysctl.conf ,配置参数如下:

bash 复制代码
net.core.rmem_max = 2500000
net.core.wmem_max = 2500000
net.core.rmem_default = 2097152
net.core.wmem_default = 2097152
net.ipv4.tcp_rmem = '4096 87380 2500000'
net.ipv4.tcp_wmem = '4096 16384 2500000'
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.tcp_fin_timeout = 30
net.ipv4.ip_forward = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.tcp_fastopen=3
net.core.rmem_max=134217728
net.core.wmem_max=134217728

然后执行 sysctl -p 生效

1.2、网络设置

bash 复制代码
查看网卡的内容(默认1500):
ifconfig 
设置网卡最大帧:
ifconfig eth0 mtu 9000

1.3、打开文件数

编辑 /etc/security/limits.conf 文件,打开文件后在最后面加上

bash 复制代码
* soft nofile 1024000 
* hard nofile 1024000

二、php 读取视频流

按文件路径参数传递,输出视频流

php 复制代码
function viewVideoStream ($video_url) {
	// 检查文件
	 if (!file_exists($video_url)) {
	     header('HTTP/1.1 404 Not Found');
	     exit('Video not found');
	 }
	 $size = filesize($video_url);
	 $start = 0;
	 $end = $size - 1;
	 $length = $size;
	 // 定义支持的视频格式及其对应的MIME类型
	 $video_types = [
	     'mp4'  => 'video/mp4',
	     'mov'  => 'video/quicktime',
	     'avi'  => 'video/x-msvideo',
	     'mkv'  => 'video/x-matroska',
	     'webm' => 'video/webm',
	     'flv'  => 'video/x-flv',
	 ];
	 $extension = pathinfo($video_url, PATHINFO_EXTENSION);// 获取文件扩展名
	 if (isset($_SERVER['HTTP_RANGE'])) {  // 解析 Range 头
	     preg_match('/bytes=(\d*)-(\d*)?/', $_SERVER['HTTP_RANGE'], $matches);
	     $start = (int)($matches[1] ?? 0);
	     $end = isset($matches[2]) && $matches[2] !== '' ? (int)$matches[2] : $end;
	     $length = $end - $start + 1;
	     // 范围验证
	     if ($start > $end || $start >= $size) {
	         header('HTTP/1.1 416 Range Not Satisfiable');
	         header("Content-Range: bytes */{$size}");
	         exit;
	     }
	     header('HTTP/1.1 206 Partial Content');
	     header("Content-Range: bytes {$start}-{$end}/{$size}");
	 }
	 header('Content-Type: ' . $video_types[$extension]);
	 header("Accept-Ranges: bytes");
	 header("Content-Length: {$length}");
	 header("Cache-Control:public, max-age=31536000",);
	 // 读取并输出文件
	 $fp = fopen($video_url, 'rb');
	 fseek($fp, $start);
	 $buffer = 1024*1024*1; // 1MB 缓冲区
	 while (!feof($fp) && ($p = ftell($fp)) <= $end) {
	     if ($p + $buffer > $end) {
	         $buffer = $end - $p + 1;
	     }
	     echo fread($fp, $buffer);
	     flush();
	 }
	 fclose($fp);
	 exit();
}
viewVideoStream ("xxx/path.mp4")
相关推荐
BingoGo2 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack2 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo3 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack3 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack4 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo4 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
JaguarJack5 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理6 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1236 天前
matlab画图工具
开发语言·matlab
dustcell.6 天前
haproxy七层代理
java·开发语言·前端