Skip to content

Instantly share code, notes, and snippets.

@Marak
Last active January 14, 2019 01:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Marak/9d4d99a78ecccb3fec43 to your computer and use it in GitHub Desktop.
Save Marak/9d4d99a78ecccb3fec43 to your computer and use it in GitHub Desktop.
Hook for transcoding video streams using FFMpeg
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