简体   繁体   中英

Video not playing in safari despite range request support

I have a streaming server that supports 206 range requests and successfully plays and seeks video on firefox, edge, and chrome. However, when I try to play the same video on Safari--it doesn't work and the video doesn't seem to even load. Any help would be appreciated...

(I'm using react--thus the jsx syntax)

Serverside Code:

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;
  }

Client-Side code:

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

So, I modified my code by adding what whatever was missing as compared to the code shown in this example: https://github.com/bootstrapping-microservices/video-streaming-example . And it works seamlessly now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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