From 2eeae84ae3dda16fd92f4bbb6fbf3d9a644ba7b2 Mon Sep 17 00:00:00 2001 From: Borewit Date: Sun, 3 Nov 2019 09:46:58 +0100 Subject: [PATCH] Just a beginning to start supporting HTTP range request handling --- modules/ffmpeg.js | 54 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/modules/ffmpeg.js b/modules/ffmpeg.js index 25f9971..6c8b9aa 100644 --- a/modules/ffmpeg.js +++ b/modules/ffmpeg.js @@ -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 + } }); } );