Making streamable / seekable video
Author: jbonnett92Created Mar 26, 2017Updated Mar 13, 2025
Version information
- fluent-ffmpeg version: 4.1.2
- ffmpeg version: 3.2.4
- OS: OSX Sierra
Code to reproduce
const http = require('http');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
var server = http.createServer(function(req, res) {
var pathToMovie = __dirname + '/src/test.mkv';
var stat = fs.statSync(pathToMovie);
var range = req.headers.range || 'bytes=0-';
var parts = range.replace(/bytes=/, "").split("-");
var total = stat.size;
var start = parseInt(parts[0], 10);
var end = parts[1] ? parseInt(parts[1], 10) : total-1;
var chunksize = (end-start)+1;
var file = fs.createReadStream(pathToMovie, {start: start, end: end});
res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + total,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
'Connection':'keep-alive'
});
var proc = new ffmpeg(file)
.outputOptions(['-frag_duration 100','-movflags frag_keyframe+empty_moov+faststart'])
//.withSize('1440x?')
.toFormat('mp4')
.on('error', function(err,stdout,stderr) {
console.log('an error happened: ' + err.message);
console.log('ffmpeg stdout: ' + stdout);
console.log('ffmpeg stderr: ' + stderr);
})
.pipe(res, { end: true });
});
server.listen(8080);
Expected results
Should make the video streamable at 127.0.0.1:8080 with seeking functionality. If I remove the FFMPEG functionality, I can stream the video and seek. The same goes for removing the seeking functionality I can successfully stream the converted video with the file path as input rather than a read stream.
Also if anyone knows an alternative way (rather than chunking) to accomplish this as I know some clients aren't compatible one of them being the Chromecast.
Observed results
A video play appears with no output, when I look in the terminal I see that I get the error:
an error happened: Output stream closed
ffmpeg stdout: undefined
ffmpeg stderr: undefinedAlthough if I change the test.mkv file to an .mp4 file I get the error:
an error happened: ffmpeg exited with code 1: Error opening filters!
ffmpeg stdout:
ffmpeg stderr: ffmpeg version 3.2.4 Copyright (c) 2000-2017 the FFmpeg developers
built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
configuration: --prefix=/usr/local/Cellar/ffmpeg/3.2.4 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-vda
libavutil 55. 34.101 / 55. 34.101
libavcodec 57. 64.101 / 57. 64.101
libavformat 57. 56.101 / 57. 56.101
libavdevice 57. 1.100 / 57. 1.100
libavfilter 6. 65.100 / 6. 65.100
libavresample 3. 1. 0 / 3. 1. 0
libswscale 4. 2.100 / 4. 2.100
libswresample 2. 3.100 / 2. 3.100
libpostproc 54. 1.100 / 54. 1.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fbdcf002600] stream 0, offset 0x4f: partial file
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fbdcf002600] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none, 1920x1080, 6539 kb/s): unspecified pixel format
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'pipe:0':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp41isom
creation_time : 2017-03-23T04:41:39.000000Z
date : 2017
Duration: 00:03:19.34, bitrate: N/A
Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 1920x1080, 6539 kb/s, 60 fps, 60 tbr, 60k tbn, 120k tbc (default)
Metadata:
creation_time : 2017-03-23T04:41:39.000000Z
handler_name : VideoHandler
encoder : AVC Coding
Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 116 kb/s (default)
Metadata:
creation_time : 2017-03-23T04:41:39.000000Z
handler_name : SoundHandler
[buffer @ 0x7fbdcee00400] Unable to parse option value "-1" as pixel format
Last message repeated 1 times
[buffer @ 0x7fbdcee00400] Error setting option pix_fmt to value -1.
[graph 0 input from stream 0:0 @ 0x7fbdcee00280] Error applying options to the filter.
Error opening filters!Source: fluent-ffmpeg/node-fluent-ffmpeg