Last active
January 14, 2019 01:44
-
-
Save Marak/9d4d99a78ecccb3fec43 to your computer and use it in GitHub Desktop.
Hook for transcoding video streams using FFMpeg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Transcoder = require('stream-transcoder'); | |
module['exports'] = function transcodeVideoStream (hook) { | |
var readStream = hook.open(hook.params.video); | |
hook.debug('Opening read stream to video'); | |
hook.res.writeHead(200, { | |
"Content-Type": "video/mp4" | |
}); | |
hook.debug('Set Content-Type to video/mp4') | |
// The transcoder API is FFMpeg, see: https://www.ffmpeg.org/ | |
new Transcoder(readStream) | |
.maxSize(hook.params.maxWidth, hook.params.maxHeight) | |
.videoCodec('h264') | |
.videoBitrate(800 * 1000) | |
.fps(25) | |
.audioCodec('libfaac') | |
.sampleRate(44100) | |
.channels(2) | |
.audioBitrate(128 * 1000) | |
.format('mp4') | |
/* | |
you can also use custom flags like 'ss' and 'tt' to slice video | |
.custom('ss', '20') | |
.custom('t', '10') | |
*/ | |
.stream().pipe(hook.res); | |
}; | |
module['exports'].schema = { | |
"video": { | |
"type": "string", | |
"default": "http://hook.io/video/InfinityMirror.mp4", | |
"required": true | |
}, | |
"maxHeight": { | |
"type": "number", | |
"default": 320, | |
"required": true | |
}, | |
"maxWidth": { | |
"type": "number", | |
"default": 240, | |
"required": true | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment