0

I need to take an input stream's audio and another stream's video and combine them with fluent-ffmpeg. I am using nodejs. Also, I need to pipe the output. Both of the inputs have video and audio, but I need to merge a stream's audio only, while doing the same with video on the other stream.

Thank you.

mr noob
  • 345
  • 2
  • 14

1 Answers1

3

you don't need to use fluent-ffmpeg, if you have ffmpeg installed on your machine you can use child-process.

const {exec} = require("child_process"); 
const command = "ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac output.mp4" 

I got this command from how to combine audio and video using ffmpeg

exec(command, (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.error(`stderr: ${stderr}`);
})

;

Ahmed Magdy
  • 1,054
  • 11
  • 16
  • Hello, sorry for the late answer. I had internet issues. The reason I used fluent-ffmpeg in the first place is that the inputs are streams. And I want to pipe the output to express as a response. I know how to do that, but I don't know the FFmpeg part. Also, both streams have audio and video as proposed above, so will it still work? – mr noob Jan 14 '21 at 13:30
  • you can look into this [answer](https://stackoverflow.com/questions/22167893/pipe-output-of-ffmpeg-using-nodejs-stdout) – Ahmed Magdy Jan 14 '21 at 14:56
  • 1
    Yes, I did look at that answer before I came here, but it just says that stout is undefined. Also, what about the input? Can I just do something like `-i pipe:4` and will it work? I will try it again, but rigt now I am in the middle of class, so I will get back to you later – mr noob Jan 14 '21 at 16:08
  • OK, so I tried the code in the link, and I get `dest.on is not a function` Here is the link to the code and error: https://pastebin.com/zMXDbeF7 error: https://pastebin.com/pFiYTC42 – mr noob Jan 14 '21 at 22:59
  • you have to pass a write stream to pipe function, so `createWriteStream` to the `output.mp4`. – Ahmed Magdy Jan 15 '21 at 16:53
  • I will try it after class! I will mark your answer as correct if it works. Tanks! (that is a way of saying thank you.) – mr noob Jan 15 '21 at 17:45
  • no problem, if you need anything else feel free to ask. – Ahmed Magdy Jan 15 '21 at 18:13
  • Sorry, but when I try it, it runs without any errors, but the mp4 outputted is just blank. I also tried to hook it up with express, but it still downloaded a blank file. Sorry! – mr noob Jan 15 '21 at 19:35
  • first check if you are actually output buffer array from `data.stdout.on("data",(chunck)=>{console.log(chunck);})` if there is any output then `res.contentType("video/mp4"); data.stdout.pipe(res); ` res is basically a writableStream . – Ahmed Magdy Jan 16 '21 at 18:44
  • I tried the code to log the chunks, but it just comes out blank. I think that ffmpeg is not outputting the output to the stream correctly, because when I change the `console.log()` in the chunk part to something like `console.log("chunk has happened");`, the string is not outputted at all. What could be the issue? – mr noob Jan 16 '21 at 20:14
  • try this code `data.stderr.on("data",(err)=>{console.log(err.toString())});` also remove the **out.mp4** from the command. the prev code should return ffmpeg error – Ahmed Magdy Jan 17 '21 at 00:36
  • 1
    It finally worked! That command helped so much! I first got `no suitable format found for pipe:1`, so I added `-f mp4` to the command and got `muxer does not support non-seekable output`, so I did a bit of research and found out that I need to add `-movflags frag_keyframe+empty_moov` and it worked! Thank you so much! You have this 5th grader's respect! – mr noob Jan 17 '21 at 16:44
  • no problem, if you need help with anything feel free to contact me on my email: ahmed.magdy.9611@gmail.com – Ahmed Magdy Jan 17 '21 at 19:54
  • @mrnoob do you mind sharing the snippet? I can't seem to get the piping working. – Core taxxe Jun 21 '22 at 11:40