简体   繁体   English

如何让 HLS.js 从服务器端获取数据?

[英]How to let HLS.js fetch data from server side?

I used hls.js to implement my video player,and I have some ts files in https:/// with one m3u8 file.我使用 hls.js 来实现我的视频播放器,我在 https:/// 中有一些 ts 文件和一个 m3u8 文件。

I readed the m3u8 file with php and sent the content to js (res["manifest"]=the content of m3u8),then put it into hls.loadSource(),as below.我用php读取m3u8文件并将内容发送到js(res [“manifest”] = m3u8的内容),然后将其放入hls.loadSource(),如下所示。

var manifest = res["manifest"];
var blob = new Blob([manifest]);
var hls = new Hls();
var url = URL.createObjectURL(blob);

hls.loadSource(url);
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED, function() {
    video.load();
});

After this,the hls started to get the files listed in m3u8 by在此之后,hls 开始通过以下方式获取 m3u8 中列出的文件

blob:https://<my domain>/<video name>.mp4

Of course,it can't find the files because they are in当然,它找不到文件,因为它们在

https://<my domain>/<video name>.mp4

instead of代替

blob:https://<my domain>/<video name>.mp4

request log请求日志

The url starts with "blob:" is created by URL.createObjectURL in front-end,but now I want that hls sends requests to video.php,then video.php reads the ts files and responses the contents back from server side. The url starts with "blob:" is created by URL.createObjectURL in front-end,but now I want that hls sends requests to video.php,then video.php reads the ts files and responses the contents back from server side.

Any method to do this?有什么方法可以做到这一点? (modify the source code,change setting,...etc) (修改源代码,更改设置,...等)

I can't believe that I achieved it.我不敢相信我做到了。

I modified the source code of src/loader/fragment.ts below:我在下面修改了 src/loader/fragment.ts 的源代码:

@@ -78,9 +78,7 @@ export class BaseSegment {
 
   get url(): string {
     if (!this._url && this.baseurl && this.relurl) {
-      this._url = buildAbsoluteURL(this.baseurl, this.relurl, {
-        alwaysNormalize: true,
-      });
+      this._url = 'video.php?f=' + this.relurl;
     }
     return this._url || '';
   }

then recompiled with webpack.然后用 webpack 重新编译。 Now it sends the get request to video.php with query parameter "f=<video name>.mp4",so I can process the request with my video.php.现在它使用查询参数“f=<video name>.mp4”向video.php发送get请求,所以我可以用我的video.php处理请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM