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

[question] How get the video path after streaming? #578

Closed mandaputtra closed 8 months ago

mandaputtra commented 1 year ago

I configure the RTMP server that save videos after it done streaming.

const NMS = new NodeMediaServer({
  rtmp: {
    port: 1935,
    chunk_size: 60000,
    gop_cache: true,
    ping: 30,
    ping_timeout: 60
  },
  http: {
    port: 8000,
    allow_origin: '*',
    mediaroot: './media'
  },
  trans: {
    ffmpeg: PathFFMPEG,
    tasks: [{
      app: 'live',
      mp4: true,
      mp4Flags: '[movflags=frag_keyframe+empty_moov]',
    }]
  }
})

NMS.run()

NMS.on('doneConnect', (id, args) => {
  console.log('[NodeEvent on doneConnect]', `id=${id} args=${JSON.stringify(args)}`);
});

The thing is, I want to upload the mp4 files after the user done streaming. Since I got multiple user connecting to the RTMP server, how do I know which file are from user A and B?

USER A -> rtmp://localhost:1935/userA
USER B -> rtmp://localhost:1935/userB
jayant-brightcode commented 8 months ago

solution?

mandaputtra commented 8 months ago

Everytime you connect to the RTMP server with rtmp://localhost:1935/userA it creates different stream path. So you can have it like these :

import NodeMediaServer from 'node-media-server';
import PathFFMPEG from 'ffmpeg-static';
import AWS from 'aws-sdk';

const s3 = new AWS.S3({
  accessKeyId: 'ACCESS_KEY_ID',
  secretAccessKey: 'SECRET_ACCESS_KEY',
});

const NMS = new NodeMediaServer({
  rtmp: {
    port: 1935,
    chunk_size: 60000,
    gop_cache: true,
    ping: 30,
    ping_timeout: 60,
  },
  http: {
    port: 8000,
    allow_origin: '*',
    mediaroot: './media',
  },
  trans: {
    ffmpeg: PathFFMPEG,
    tasks: [
      {
        app: 'live',
        mp4: true,
        mp4Flags: '[movflags=frag_keyframe+empty_moov]',
      },
    ],
  },
});

NMS.run();

NMS.on('doneConnect', (id, args) => {
  console.log('[NodeEvent on doneConnect]', `id=${id} args=${JSON.stringify(args)}`);
});

NMS.on('postPublish', async (id, streamPath, args) => {
  console.log('[NodeEvent on postPublish]', `id=${id} streamPath=${streamPath} args=${JSON.stringify(args)}`);

  // Upload the media file to an S3 bucket after stream end
  const params = {
    Bucket: 'BUCKET_NAME',
    Key: streamPath.replace('.flv', '.mp4'), // Replace .flv extension with .mp4 save to bucket as mp4
    Body: fs.createReadStream(streamPath),
  };

  try {
    const response = await s3.upload(params).promise();
    console.log('[S3] Uploaded file:', response.Location);
  } catch (error) {
    console.error('[S3] Error:', error);
  }
});