fluent-ffmpeg / node-fluent-ffmpeg

A fluent API to FFMPEG (http://www.ffmpeg.org)
MIT License
7.65k stars 874 forks source link

express.js streaming issues #494

Open echo66 opened 8 years ago

echo66 commented 8 years ago

Greetings!

I have the following code to cut remote mp3 files:

var express = require('express');
var ffmpeg = require('fluent-ffmpeg');
var app = express();

app.get ('/', function(req,res) {
  res.writeHead(200, {
    'Content-Type': 'audio/mpeg',
  });
  console.log(req.query.path.replace(/ /g, '%20'));
  ffmpeg(req.query.path.replace(/ /g, '%20'))
      .on('error', function(err) {
            console.log('Processing error! ' + err);
      })
      .format('mp3')
      .audioCodec('copy')
      .seekInput(req.query.start).duration(req.query.duration)
      .pipe(res, {end:true});

 });

var server = app.listen(2000);

The code seems to do the trick but, for remote files (i.e.: not in the server disk), I get the following error:

Processing error! Error: Output stream closed

njoyard commented 8 years ago

The error message is correct, in that this happens when the browser closes the connection.

This is probably because most browsers make two requests for some filetypes: a first one to get a slice of the beginning of the file to determine its format/codec, and then a chunked request to actually stream the file.

echo66 commented 8 years ago

According to your description, it is an "error" and not an error. Am I correct? :P

njoyard commented 8 years ago

Actually that's more an exception. Whether it qualifies as an error is entirely your interpretation :D But more seriously, there's no way to distinguish between "legit" stream closes and other actual errors. That's just how ffmpeg reports them.