الملفات
codepill-sfu/rtmpServers/demo/client-server-axios/server/RtmpStreamer.js
2025-01-16 01:40:19 +01:00

67 أسطر
2.0 KiB
JavaScript

'use strict';
const { PassThrough } = require('stream');
const ffmpeg = require('fluent-ffmpeg');
const ffmpegPath = '/usr/local/bin/ffmpeg'; // /usr/bin/ffmpeg (Linux) | /usr/local/bin/ffmpeg (Mac)
ffmpeg.setFfmpegPath(ffmpegPath);
class RtmpStreamer {
constructor(rtmpUrl, rtmpKey) {
this.rtmpUrl = rtmpUrl;
this.rtmpKey = rtmpKey;
this.stream = new PassThrough();
this.ffmpegStream = null;
this.initFFmpeg();
this.run = true;
}
initFFmpeg() {
this.ffmpegStream = ffmpeg()
.input(this.stream)
.inputOptions('-re')
.videoCodec('libx264')
.videoBitrate('3000k')
.size('1280x720')
.audioCodec('aac')
.audioBitrate('128k')
.outputOptions(['-f flv'])
.output(this.rtmpUrl)
.on('start', (commandLine) => console.info('ffmpeg command', { id: this.rtmpKey, cmd: commandLine }))
.on('error', (err, stdout, stderr) => {
if (!err.message.includes('Exiting normally')) {
console.error('FFmpeg error:', { id: this.rtmpKey, error: err.message });
}
this.end();
})
.on('end', () => {
console.info('FFmpeg process ended', this.rtmpKey);
this.end();
})
.run();
}
write(data) {
if (this.stream) this.stream.write(data);
}
isRunning() {
return this.run;
}
end() {
if (this.stream) {
this.stream.end();
this.stream = null;
console.info('RTMP streaming stopped', this.rtmpKey);
}
if (this.ffmpegStream && !this.ffmpegStream.killed) {
this.ffmpegStream.kill('SIGTERM');
this.ffmpegStream = null;
console.info('FFMPEG closed successfully', this.rtmpKey);
}
this.run = false;
}
}
module.exports = RtmpStreamer;