簡體   English   中英

盡管支持范圍請求,但視頻無法在 safari 中播放

[英]Video not playing in safari despite range request support

我有一個支持 206 范圍請求的流媒體服務器,並在 firefox、edge 和 chrome 上成功播放和尋找視頻。 但是,當我嘗試在 Safari 上播放同一視頻時——它不起作用,而且視頻似乎甚至無法加載。 任何幫助,將不勝感激...

(我正在使用 react——因此是 jsx 語法)

服務器端代碼:

try {
    const { range, videoID } = req.requestData;
    const videoPath = "media/" + videoID + ".mp4";
    let videoSize;
    try {
      videoSize = fs.statSync(videoPath).size;
    } catch (err) {
      console.log("Error during video size scan...");
      res.status(500).end();
    }
    // Example: "bytes=32324-"
    const CHUNK_SIZE = 10 ** 6; // 1MB
    const start = Number(range.replace(/\D/g, ""));
    const end = Math.min(start + CHUNK_SIZE, videoSize - 1); // gets the end of the range, start + 1mb or end of video
    const contentLength = end - start + 1;
    
    const headers = {
      "Content-Range": `bytes ${start}-${end}/${videoSize}`,
      "Accept-Ranges": "bytes",
      "Content-Length": contentLength,
      "Content-Type": "video/mp4",
    };
    // HTTP Status 206 for Partial Content
    res.writeHead(206, headers);
    try {
      // create video read stream for this particular chunk
      const videoStream = fs.createReadStream(videoPath, { start, end });

      // Stream the video chunk to the client
      videoStream.pipe(res);
    } catch (err) {
      res.status(500).end();
    }
  } catch (err) {
    res.status(500).end();
    return;
  }

客戶端代碼:

        <video controls autoPlay preload="auto" className="w-full h-full">
          <source src={url} type="video/mp4" />
        </video>

因此,我通過添加與此示例中顯示的代碼相比缺少的內容來修改我的代碼: https://github.com/bootstrapping-microservices/video-streaming-example 現在它可以無縫運行!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM