4

I'm using Node.js, Express (and connect), and fluent-ffmpeg.

We want to stream audio files that are stored on Amazon S3 through http.

We have all working, except that we would like to add a feature, the on-the-fly conversion of the stream through ffmpeg.

This is working well, the problem is that some browsers checks in advance before actually getting the file.

Incoming requests containing the Range header, for which we reply with a 206 with all the info from S3, have a fundamental problem: we need to know in advance the content-length of the file.

We don't know that since it is going through ffmpeg.

One solution might be to write out the resulting content-length directly on S3 when storing the file (in a special header), but this means we have to go through the pain of having queues to encode after upload just to know the size for future requests. It also means that if we change compressor or preset we have to go through all this over again, so it is not a viable solution.

We also noticed big differencies in the way Chrome and Safari request the audio tag src, but this may be discussion for another topic.

Fact is that without a proper content-length header in response everything seems to break or browsers goes in an infinite loop or restart the stream at pleasure.

Ideas?

kain
  • 5,510
  • 3
  • 27
  • 36

1 Answers1

3

This seems to be working for me.

It would be great if you could confirm whether it gives the expect results in your browsers too.

res.writeHead(200, {
                       'Transfer-Encoding': 'chunked'
                     , 'Content-Type': 'audio/mpeg'
                     , 'Accept-Ranges': 'bytes' //just to please some players, we do not actually allow seeking
});

Basically, you tell the browser that you are going to stream using chunked encoding. An issue may be that some browsers do not like streaming without know how much bytes they should expect in total.

Tom
  • 8,536
  • 31
  • 133
  • 232
  • so you always send 200 to a Range request? – kain Aug 30 '11 at 17:48
  • @kain, that's what I do yes. This is only because I do not actually allow players to seek. – Tom Aug 30 '11 at 19:30
  • We ended up building a perfect proxy for S3 streaming. What it does is to have an additional authentication layer, hides real urls from S3, and converts file on the fly, all that without touching the filesystem; we are still working a bit on the details of the on the fly encoding because we want to be compliant, but we take your solution for good. – kain Aug 30 '11 at 20:05