illuspas / Node-Media-Server

A Node.js implementation of RTMP/HTTP-FLV/WS-FLV/HLS/DASH/MP4 Media Server
https://www.npmjs.com/package/node-media-server
MIT License
5.89k stars 1.51k forks source link

Hls.js player doesn't load the stream if the first segment of hls doesn't exist #610

Closed MisterKirill closed 7 months ago

MisterKirill commented 7 months ago

Probably a problem with Hls.js, but when I'm trying to load stream in player like this:

if(Hls.isSupported()) {
  const hls = new Hls();
  hls.loadSource(url);
  hls.attachMedia(videoRef.current);
} else if (videoRef.current.canPlayType('application/vnd.apple.mpegurl')) {
  videoRef.current.src = url;
}

it works perfectly only if the first segment is exist in playlist, and if there is no first segment, it loads only like 5 seconds of black screen: Screenshot 2023-11-19 142504

Node media server configuration:

const config = {
  rtmp: {
    port: 1935,
    chunk_size: 60000,
    gop_cache: true,
    ping: 30,
    ping_timeout: 60
  },
  http: {
    port: 8000,
    mediaroot: './media',
    allow_origin: '*'
  },
  trans: {
    ffmpeg: 'ffmpeg.exe',
    tasks: [
      {
        app: 'live',
        hls: true,
        hlsFlags: '[hls_time=2:hls_list_size=3:hls_flags=delete_segments]',
      }
    ]
  },
}
TideOPL commented 5 months ago

How did you fix this, if you did?

TideOPL commented 5 months ago

For future reference and people that have the same problem, I have a fix! It seems the command that NMS uses to transcode FLV to HLS is causing problems. The fix is as follows:

In NMS, look for node_trans_session.js.

Around line 67 it sets loads of arguments to run. I will paste what I have below and you can change it for whatever you need.

    let argv = ['-i', inPath];
    // Array.prototype.push.apply(argv, ['-c:v', vc]);
    // Array.prototype.push.apply(argv, this.conf.vcParam);
    // Array.prototype.push.apply(argv, ['-c:a', ac]);
    // Array.prototype.push.apply(argv, this.conf.acParam);
    Array.prototype.push.apply(argv, ['-c:v', 'h264', '-c:a', 'aac', '-strict', '-2', '-f', 'hls', '-x264-params', 'keyint=15:min-keyint=15', '-hls_time', '1', '-hls_flags', 'delete_segments', '-hls_list_size', '20', '-http_persistent', '0', mapStr]);

image

Maybe someone could look into the real problem and create a pull request to fix it.