fluent-ffmpeg / node-fluent-ffmpeg

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

Streaming audio files #284

Closed seifsallam closed 10 years ago

seifsallam commented 10 years ago

I'm working on radio streaming and was trying to pipe data from an audio file to socket.io on data. I've been able to do read data using createReadStream and on data event, i send an event using socket.io with binary data.

I've tried to implement same logic using node-fluent-ffmpeg but it doesn't seem to be working. Event with the simple example on the docs, i get An error occurred: ffmpeg exited with code 1: pipe:1: Invalid argument

Is there something i'm doing wrong?

var outStream = fs.createWriteStream('output.mp3');

ffmpeg(filePath)
  .on('data', function(chunk) {
    console.log('data', chunk);
  })
  .on('error', function(err) {
    console.log('An error occurred: ' + err.message);
  })
  .on('end', function() {
    console.log('Processing finished !');
  })
  .pipe(outStream, { end: true });

node v0.10.26 fluent-ffmpeg 2.0.0-rc1

njoyard commented 10 years ago

Which OS are you running on, and with which ffmpeg version ?

Could you please log the full command line with:

.on('start', function(cmdline) {
  console.log('Command line: ' + cmdline);
});
seifsallam commented 10 years ago

OSX 10.9.4, and ffmpeg version 2.2.1

Command line: ffmpeg -i /Users/seif/Projects/radio-one-streamer/app/controllers/../../public/audio/rock-channel/101.mp3 pipe:1
seifsallam commented 10 years ago

I've updated now to ffmpeg 2.2.4, and now it shows shows an extra error Conversion failed!

Command line: ffmpeg -i /Users/seif/Projects/radio-one-streamer/app/controllers/../../public/audio/rock-channel/101.mp3 pipe:1
An error occurred: ffmpeg exited with code 1: pipe:1: Invalid argument
Conversion failed!
njoyard commented 10 years ago

Could you please run ffmpeg -protocols from the command line and paste the output here ?

seifsallam commented 10 years ago
ffmpeg version 2.2.4 Copyright (c) 2000-2014 the FFmpeg developers
  built on Jun 23 2014 16:31:17 with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/2.2.4 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --enable-avresample --enable-vda --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libfaac --enable-libmp3lame --enable-libxvid
  libavutil      52. 66.100 / 52. 66.100
  libavcodec     55. 52.102 / 55. 52.102
  libavformat    55. 33.100 / 55. 33.100
  libavdevice    55. 10.100 / 55. 10.100
  libavfilter     4.  2.100 /  4.  2.100
  libavresample   1.  2.  0 /  1.  2.  0
  libswscale      2.  5.102 /  2.  5.102
  libswresample   0. 18.100 /  0. 18.100
  libpostproc    52.  3.100 / 52.  3.100
Supported file protocols:
Input:
cache
concat
crypto
data
ffrtmphttp
file
ftp
gopher
hls
http
httpproxy
mmsh
mmst
pipe
rtmp
rtmpt
rtp
srtp
tcp
udp
unix
Output:
ffrtmphttp
file
ftp
gopher
http
httpproxy
md5
pipe
rtmp
rtmpt
rtp
srtp
tcp
udp
unix
njoyard commented 10 years ago

Hmm, that's weird, "pipe" is supported indeed. Can you log ffmpeg stdout and stderr please? You can do so using:

.on('error', function(err, stdout, stderr) {
  console.log('ffmpeg stdout: ' + stdout);
  console.log('ffmpeg stderr: ' + stderr);
});

EDIT: use 'error', not 'end'.

seifsallam commented 10 years ago
Command line: ffmpeg -i /Users/seif/Projects/radio-one-streamer/app/controllers/../../public/audio/rock-channel/song-1.mp3 pipe:1
ffmpeg stdout: null
ffmpeg stderr: ffmpeg version 2.2.4 Copyright (c) 2000-2014 the FFmpeg developers
  built on Jun 23 2014 16:31:17 with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/2.2.4 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --enable-avresample --enable-vda --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libfaac --enable-libmp3lame --enable-libxvid
  libavutil      52. 66.100 / 52. 66.100
  libavcodec     55. 52.102 / 55. 52.102
  libavformat    55. 33.100 / 55. 33.100
  libavdevice    55. 10.100 / 55. 10.100
  libavfilter     4.  2.100 /  4.  2.100
  libavresample   1.  2.  0 /  1.  2.  0
  libswscale      2.  5.102 /  2.  5.102
  libswresample   0. 18.100 /  0. 18.100
  libpostproc    52.  3.100 / 52.  3.100
Input #0, mp3, from '/Users/seif/Projects/radio-one-streamer/app/controllers/../../public/audio/rock-channel/song-1.mp3':
  Metadata:
    title           : Cry of Achilles
    artist          : Alter Bridge
    album           : Fortress
    album_artist    : Alter Bridge
    disc            : 1/1
    genre           : Alt. Rock
    track           : 1/12
    TOPE            : Alter Bridge
    encoded_by      : - sa1sa -
    date            : 2013
  Duration: 00:06:30.92, start: 0.025056, bitrate: 321 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 320 kb/s
    Stream #0:1: Video: mjpeg, yuvj420p(pc), 500x500 [SAR 96:96 DAR 1:1], 90k tbr, 90k tbn, 90k tbc
    Metadata:
      title           :
      comment         : Cover (front)
[NULL @ 0x7f966a01ea00] Unable to find a suitable output format for 'pipe:1'
pipe:1: Invalid argument
Conversion failed!
njoyard commented 10 years ago

Oh! I'm dumb. I missed the 'format' word in the error message. You have to specify output format when outputting to a stream (ffmpeg cannot determine it automatically). You should add a toFormat() call.

seifsallam commented 10 years ago

It worked! Thanks alot :smile:

seifsallam commented 10 years ago

@njoyard Quick (unrelated) question. Is it possible to control chunk or bufferLength received on data?

njoyard commented 10 years ago

I don't think so. You'd have to write an intermediary stream (for example by extending a TransportStream) that would re-emit data in chunks the size you want.