Just a beginning to start supporting HTTP range request handling

This commit is contained in:
Borewit 2019-11-03 09:46:58 +01:00
parent 25aeba6316
commit 2eeae84ae3

View File

@ -4,11 +4,20 @@ const ffmpeg = require("fluent-ffmpeg");
const winston = require('winston');
const codecMap = {
'mp3': 'libmp3lame',
'opus': 'libopus',
'aac': 'aac'
'mp3': {codec: 'libmp3lame', contentType: 'audio/mpeg'},
'opus': {codec: 'libopus', contentType: 'audio/ogg'},
'aac': {codec: 'aac', contentType: 'audio/aac'}
};
function initHeaders(res, audioTypeId, audioPath) {
const contentType = codecMap[audioTypeId].contentType;
return res.header({
'Accept-Ranges': 'bytes',
'Content-Type': contentType,
// 'Content-Length': stat.size
});
}
exports.setup = (mstream, program) => {
const platform = ffbinaries.detectPlatform();
@ -36,20 +45,31 @@ exports.setup = (mstream, program) => {
return;
}
ffmpeg(pathInfo.fullPath)
.noVideo()
.format(program.transcode.defaultCodec)
.audioCodec(codecMap[program.transcode.defaultCodec])
.audioBitrate(program.transcode.defaultBitrate)
.on('end', () => {
// console.log('file has been converted succesfully');
})
.on('error', err => {
winston.error('Transcoding Error!');
console.log(err);
})
// save to stream
.pipe(res, { end: true });
// Stream audio data
if (req.method === 'GET') {
initHeaders(res, program.transcode.defaultCodec, pathInfo.fullPath);
ffmpeg(pathInfo.fullPath)
.noVideo()
.format(program.transcode.defaultCodec)
.audioCodec(codecMap[program.transcode.defaultCodec].codec)
.audioBitrate(program.transcode.defaultBitrate)
.on('end', () => {
// console.log('file has been converted succesfully');
})
.on('error', err => {
winston.error('Transcoding Error!');
console.log(err);
})
// save to stream
.pipe(res, { end: true });
} else if (req.method === 'HEAD') {
// The HEAD request should return the same headers as the GET request, but not the body
initHeaders(res, program.transcode.defaultCodec, pathInfo.fullPath).sendStatus(200);
} else {
res.sendStatus(405); // Method not allowed
}
});
}
);