Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
NodeJS video streaming to iOS doesn't work, but works on desktop Chrome and OSX safari

I am trying to make this video possible to play in Safari on iOS: https://ekgcamp.com/api/videos/video1.mp4

The video works for desktop Chrome and OSX safari, but I have been struggling for days to make it play on iOS safari. Also, I am unsure how to debug it. The API supports range requests, and the video CODEC is AAC, H.264.

The API endpoint looks as follows:

import type { NextApiRequest, NextApiResponse } from "next";
import fs from "fs";

const handler = (req: NextApiRequest, res: NextApiResponse) => {
  const { videoName } = req.query;
  const filePath = `files/videos/${videoName}`;
  const stat = fs.statSync(filePath);
  const fileSize = stat.size;
  const range = req.headers.range;
  if (range) {
    const parts = range.replace(/bytes=/, "").split("-");
    const start = parseInt(parts[0], 10);
    const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
    const chunksize = end - start + 1;
    const file = fs.createReadStream(filePath, { start, end });
    const head = {
      "Content-Range": `bytes ${start}-${end}/${fileSize}`,
      "Accept-Ranges": "bytes",
      "Content-Length": chunksize,
      "Content-Type": "video/mp4",
    };
    res.writeHead(206, head);
    file.pipe(res);
  } else {
    const head = {
      "Content-Length": fileSize,
      "Content-Type": "video/mp4",
    };
    res.writeHead(200, head);
    fs.createReadStream(filePath).pipe(res);
  }
};

export default handler;

The following headers are sent to and from Chrome and iOS safari

Chrome:

From: bytes=0-
To: {
  'Content-Range': 'bytes 0-397376/397377',
  'Accept-Ranges': 'bytes',
  'Content-Length': 397377,
  'Content-Type': 'video/mp4'
}

bytes=163840-
{
  'Content-Range': 'bytes 163840-397376/397377',
  'Accept-Ranges': 'bytes',
  'Content-Length': 233537,
  'Content-Type': 'video/mp4'
}

iOS:

bytes=0-1
{
  'Content-Range': 'bytes 0-1/397377',
  'Accept-Ranges': 'bytes',
  'Content-Length': 2,
  'Content-Type': 'video/mp4'
}

bytes=0-397376
{
  'Content-Range': 'bytes 0-397376/397377',
  'Accept-Ranges': 'bytes',
  'Content-Length': 397377,
  'Content-Type': 'video/mp4'
}

How do I make it work on both desktop and iOS? Cheers.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!