1

I'm trying to record audio on a webrtc server in NodeJS using FFmpeg.

I am able to record the audio if I explicitly create an input.sdp file and use it with the "-i" flag of FFmpeg.

My question is: How to create a string in the code to dynamically change port numbers for recording different streams at the same time?

I tried doing this:

    const sdpInfo = `data:application/sdp;charset=UTF-8,v=0\no=- 0 0 IN IP4 ${ipAddr}\ns=-\nc=IN IP4 ${ipAddr}\nt=0 0\nm=audio ${port} RTP/AVPF 111\na=rtcp:${port+1}\na=rtpmap:111 opus/48000/2\na=fmtp:111 minptime=10;useinbandfec=1`

However, if I give it as input with "-i" flag, I get the following error:

data:application/sdp;charset=UTF-8,v=0: Invalid data found when processing input

Can someone please help?

NaGaBaBa
  • 93
  • 5

1 Answers1

0

You must pipe the SDP to the stdin. For that, you need to convert it to a stream first. Use the following code to achieve what you want:

import * as childProcess from 'child_process';
import FFmpegStatic from 'ffmpeg-static';

function convertStringToStream(stringToConvert)
{

    const stream = new Readable();



    stream._read = () => { };

    stream.push(stringToConvert);

    stream.push(null);


    return stream;

}

const sdpInfo = `v=0\no=- 0 0 IN IP4 ${ipAddr}\n` +
    `s=-\nc=IN IP4 ${ipAddr}\n` +
    `t=0 0\nm=audio ${port} RTP/AVPF 111\n` +
    `a=rtcp:${port+1}\na=rtpmap:111 opus/48000/2\n` +
    `a=fmtp:111 minptime=10;useinbandfec=1`;

const ffmpegArgs = [

   '-loglevel',

    'debug',

   '-protocol_whitelist',

    'pipe,udp,rtp',
    '-f',
    'sdp',
    '-i',
    'pipe:0',
    // Rest of your parameters
];

const process = childProcess.spawn(FFmpegStatic, ffmpegArgs);
const sdpStream = convertStringToStream(sdpInfo);

sdpStream.resume();

sdpStream.pipe(process.stdin);

Farzan
  • 745
  • 10
  • 25