简体   繁体   English

如何检测 URL 是 mp4 还是 m3u8 并在浏览器上播放?

[英]How can I detect the URL is mp4 or m3u8 and play it on browser?

I'm using React, I have 2 types of video that I want to play, 1 is m3u8 and 1 is mp4.我正在使用 React,我有 2 种类型的视频要播放,1 种是 m3u8,1 种是 mp4。

I have a set of episodes of film series, but it mixed with m3u8 and mp4 together.我有一组电影系列剧集,但它与 m3u8 和 mp4 混合在一起。

So how can I detect whatever it is so it can display it on Browser.那么我怎样才能检测到它是什么,以便它可以在浏览器上显示它。

I'm using ReactHlsPlayer that can easily play the m3u8, but the problem is if it's the URL that contains an mp4 file, it refuses to read and display it on my browser.我正在使用可以轻松播放 m3u8 的 ReactHlsPlayer,但问题是如果它是包含 mp4 文件的 URL,它拒绝在我的浏览器上读取和显示它。

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import ReactHlsPlayer from 'react-hls-player';


ReactDOM.render(
  <React.StrictMode>
    <App />
    <ReactHlsPlayer
    src = "url-that-contain-m3u8"
    autoPlay = {false}
    controls = {true}
    width = "100%"
    height = "auto"
    />
  </React.StrictMode>,
  document.getElementById('root')
);

If I use如果我使用

<source src="url-that-contain-mp4" type="video/mp4">

It'll read the mp4 URL perfectly, but then, it'll only read the mp4, and refuse the m3u8它会完美地读取 mp4 URL,但是,它只会读取 mp4,并拒绝 m3u8

So is there a way I can create a function or a component that can detect if my URL is m3u8 or mp4 and play it with a suitable solution?那么有没有一种方法可以创建 function 或可以检测我的 URL 是 m3u8 还是 mp4 的组件并使用合适的解决方案播放它? Thanks.谢谢。

Based on my condition, I just need to recognize if the URL that I get is type video/mp4 or not (it'll be the m3u8 then), if your case is similar or has more types that need to recognize, I think it'll work the same way.根据我的情况,我只需要识别我得到的 URL 是否为 video/mp4 类型(然后将是 m3u8),如果您的情况相似或有更多需要识别的类型,我认为是会以同样的方式工作。

First I take a quick look at MIME Types that @ControlAltDel provide, and then I do a little bit more research on how to take the URL request, which leads toXMLHttpRequest due to I workaround with POSTMAN to see the Header of the URL Request that I get, things become clearer, that I need to get into the HEADER and get the Content-Type from it. First I take a quick look at MIME Types that @ControlAltDel provide, and then I do a little bit more research on how to take the URL request, which leads toXMLHttpRequest due to I workaround with POSTMAN to see the Header of the URL Request that我明白了,事情变得更清楚了,我需要进入 HEADER 并从中获取 Content-Type。

您可以在此处通过 POSTMAN 检查 Content-Type

So here's a quick code that I do to get things that I want.所以这里有一个快速代码,我可以用它来获得我想要的东西。

let URL = the-url-that-contain-mp4-or-m3u8
// Make a function or variable to get the URL you want, in my case it's the episode URL.

let xhr = new XMLHttpRequest();
// Requests the headers that would be returned if the HEAD request's URL was instead requested with the HTTP GET method
xhr.open('HEAD', url, true);

xhr.onload = function() {
// In here I get the Content Type from the HEAD of the response
    let contentType = xhr.getResponseHeader('Content-Type');
    if (contentType == 'video/mp4'){
        console.log(contentType);
        console.log("This is mp4 video")
//Function to play mp4 file
    }
    else {
        console.log("This is m3u8 then")
// Function to play HLS m3u8 file
    }

};

xhr.send();

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

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