Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda