fluent-ffmpeg / node-fluent-ffmpeg

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

Convert raw RTP packets from mediasoup to video file #719

Open zacharynevin opened 7 years ago

zacharynevin commented 7 years ago

Version information

About the issue

I am using mediasoup as a WebRTC server. In mediasoup, you can intercept raw RTP packets on the serverside using the following code:

peer
  .on('newrtpreceiver', (rtpReceiver) => {
    rtpReceiver.on('rtpraw', (packet) => {
      // do something with this packet
    })
  })

These packets are vp8 encoded. I want to pass the packets into FFMPEG and write them to an mp4 file.

My first attempt at doing this used the following procedure:

Here is an example of the code:

peer
  .on('newrtpreceiver', (rtpReceiver) => {
    let readStream = new Readable({
      objectMode: false,
      read(size) { return true }
    })

    let ffmpegStream = ffmpeg(readStream)
      .noAudio()
      .videoCodec('libvpx')
      .size('640x?')
      .format('mp4')
      .on('start', (cmdline) => {
        console.log('Command line: ' + cmdline)
      })
      .on('progress', (progress) => {
        console.log('Processing: ' + progress.percent + '% done')
      })
      .on('stderr', (stderrLine) => {
        console.log('Stderr output: ' + stderrLine)
      })
      .on('error', (err, stdout, stderr) => {
        console.log('Cannot process video: ' + err.message)
      })
      .on('end', () => {
        console.log('Finished processing')
      })
      .pipe('myfile.mp4')

    rtpReceiver
      .on('rtpraw', (packet) => {
        readStream.push(packet)
      })
      .on('close', () => { 
        readStream.push(null) 
      })
  })

When I run this, I get the error Invalid data when processing input. Here are the console logs:

Command line: ffmpeg -i pipe:0 -y -an -vcodec libvpx -filter:v scale=w=640:h=trunc(ow/a/2)*2 -f mp4 mymov.mp4
Stderr output: ffmpeg version 3.2.4 Copyright (c) 2000-2017 the FFmpeg developers
Stderr output:   built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
Stderr output:   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-ffplay --enable-frei0r --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-libopenjpeg --disable-decoder=jpeg2000 --extra-cflags=-I/usr/local/Cellar/openjpeg/2.1.2/include/openjpeg-2.1 --enable-nonfree --enable-vda
Stderr output:   libavutil      55. 34.101 / 55. 34.101
Stderr output:   libavcodec     57. 64.101 / 57. 64.101
Stderr output:   libavformat    57. 56.101 / 57. 56.101
Stderr output:   libavdevice    57.  1.100 / 57.  1.100
Stderr output:   libavfilter     6. 65.100 /  6. 65.100
Stderr output:   libavresample   3.  1.  0 /  3.  1.  0
Stderr output:   libswscale      4.  2.100 /  4.  2.100
Stderr output:   libswresample   2.  3.100 /  2.  3.100
Stderr output:   libpostproc    54.  1.100 / 54.  1.100
Stderr output: pipe:0: Invalid data found when processing input
Stderr output: 
Cannot process video: ffmpeg exited with code 1: pipe:0: Invalid data found when processing input

Thank you for all your help!

njoyard commented 7 years ago

Well, all we can tell from the error output is that ffmpeg receives data it's not prepared to handle. My hint would be : dump your rtp packets into a file, pipe this file (in a shell) to ffmpeg and try to get it working on command line. When you found what's missing, go back to implementing it with fluent-ffmpeg :)