javascript - Concatenate mp4 video files using ffmpeg on nodejs - Stack Overflow

I'm trying to concatenate several videos using FFmpeg with Nodejs. I'm receiving the "No

I'm trying to concatenate several videos using FFmpeg with Nodejs. I'm receiving the "No such file or directory" error. The written code is:

const glob = require("glob");

//store all the filenames with path to the videos
var inputVideos = glob.sync("/home/r/clips/*.mp4");
const output = "./output/output.mp4";

const util = require("util");
const exec = util.promisify(require("child_process").exec);

async function concatVideos(inputVideos, outputVideoPath) {
  

  //imported to get the name of videofile easily
  var path = require("path");

  //the next ffmpeger variable,will keep the lines
  //ffmpeg -i videofile1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate_videofile1.ts
  //ffmpeg -i videofile2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2_videofile2.ts

var ffmpeger = "";
  ffmpeger = inputVideos
    .map(
      video =>
        ` ffmpeg -i ${video} -c copy
    -bsf:v h264_mp4toannexb -f mpegts ./intermediate_${
      path.parse(video).name
    }.ts`
    )
    .join("\n");

  //concatenator keeps the segment
  //"concat:intermediate_videofile1.ts|intermediate_videofile2.ts"
  var concatenator = '"concat:';
  concatenator +=
    inputVideos
      .map(video => `./intermediate_${path.parse(video).name}.ts`)
      .join("|") + '"';

  await exec(
    `
    ${ffmpeger}
    ffmpeg -i ${concatenator} -c copy -bsf:a aac_adtstoasc ${outputVideoPath}`
  );
}
concatVideos(inputVideos, output);

and the error is

concat:./intermediate_0.ts|./intermediate_1.ts|./intermediate_2.ts|./intermediate_3.ts|./intermediate_4.ts|./intermediate_5.ts|./intermediate_6.ts|./intermediate_diegoortiz1399.ts|./intermediate_dog.ts|./intermediate_dogstify.ts|./intermediate_dylan50568.ts|./intermediate_gabrieleecorrea.ts|./intermediate_golden_leo.ts|./intermediate_helenapatiih.ts|./intermediate_kaiobreno2.ts|./intermediate_khancorso.ts|./intermediate_kitakaze_s_lili.ts|./intermediate_oliver45743.ts|./intermediate_pinkie_pets.ts|./intermediate_shibakoma.ts|./intermediate_thepetcollective.ts|./intermediate_tod_the_foxx.ts|./intermediate_userpub3y9m7kb.ts|./intermediate_warriorbulldogs.ts: No such file or directory```

So the mand line would be:

ffmpeg -i videofile1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate_videofile1.ts ffmpeg -i videofile2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate_videofile2.ts ffmpeg -i "concat:intermediate_videofile1.ts|intermediate_videofile2.ts" -c copy -bsf:a aac_adtstoasc ./output/output.mp4

Could you help us? Many thanks. Truly.:>

I'm trying to concatenate several videos using FFmpeg with Nodejs. I'm receiving the "No such file or directory" error. The written code is:

const glob = require("glob");

//store all the filenames with path to the videos
var inputVideos = glob.sync("/home/r/clips/*.mp4");
const output = "./output/output.mp4";

const util = require("util");
const exec = util.promisify(require("child_process").exec);

async function concatVideos(inputVideos, outputVideoPath) {
  

  //imported to get the name of videofile easily
  var path = require("path");

  //the next ffmpeger variable,will keep the lines
  //ffmpeg -i videofile1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate_videofile1.ts
  //ffmpeg -i videofile2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2_videofile2.ts

var ffmpeger = "";
  ffmpeger = inputVideos
    .map(
      video =>
        ` ffmpeg -i ${video} -c copy
    -bsf:v h264_mp4toannexb -f mpegts ./intermediate_${
      path.parse(video).name
    }.ts`
    )
    .join("\n");

  //concatenator keeps the segment
  //"concat:intermediate_videofile1.ts|intermediate_videofile2.ts"
  var concatenator = '"concat:';
  concatenator +=
    inputVideos
      .map(video => `./intermediate_${path.parse(video).name}.ts`)
      .join("|") + '"';

  await exec(
    `
    ${ffmpeger}
    ffmpeg -i ${concatenator} -c copy -bsf:a aac_adtstoasc ${outputVideoPath}`
  );
}
concatVideos(inputVideos, output);

and the error is

concat:./intermediate_0.ts|./intermediate_1.ts|./intermediate_2.ts|./intermediate_3.ts|./intermediate_4.ts|./intermediate_5.ts|./intermediate_6.ts|./intermediate_diegoortiz1399.ts|./intermediate_dog.ts|./intermediate_dogstify.ts|./intermediate_dylan50568.ts|./intermediate_gabrieleecorrea.ts|./intermediate_golden_leo.ts|./intermediate_helenapatiih.ts|./intermediate_kaiobreno2.ts|./intermediate_khancorso.ts|./intermediate_kitakaze_s_lili.ts|./intermediate_oliver45743.ts|./intermediate_pinkie_pets.ts|./intermediate_shibakoma.ts|./intermediate_thepetcollective.ts|./intermediate_tod_the_foxx.ts|./intermediate_userpub3y9m7kb.ts|./intermediate_warriorbulldogs.ts: No such file or directory```

So the mand line would be:

ffmpeg -i videofile1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate_videofile1.ts ffmpeg -i videofile2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate_videofile2.ts ffmpeg -i "concat:intermediate_videofile1.ts|intermediate_videofile2.ts" -c copy -bsf:a aac_adtstoasc ./output/output.mp4

Could you help us? Many thanks. Truly.:>

Share Improve this question edited Dec 19, 2020 at 3:12 Romualdo Arrechea Hernández asked Apr 3, 2020 at 1:09 Romualdo Arrechea HernándezRomualdo Arrechea Hernández 4991 gold badge8 silver badges18 bronze badges 2
  • Use the concat demuxer (documentation & wiki) instead of the concat protocol. You can avoid the temporary files with the concat demuxer. – llogan Commented Apr 3, 2020 at 4:47
  • Thanks Ilogan – Romualdo Arrechea Hernández Commented Apr 7, 2020 at 11:50
Add a ment  | 

2 Answers 2

Reset to default 3

So, the solution I found is in https://www.npmjs./package/ffmpeg-concat.

First install:

npm i ffmpeg-concat

Then: To concatenate videos:

const concat = require('ffmpeg-concat')
const glob=require('glob')

//an array of video path to concatenate
const videos=glob.sync('/home/username/Downloads/clips/*.mp4')

const output='./output/concatenated.mp4'

//a function to merge an array of videos with custom music
//and a transition fadegrayscale of 500ms duration between videos.
async function oneTransitionMergeVideos(){
  await concat({
   output,
   videos,
   audio:"/home/username/Downloads/music/music.m4a",
   transition: {
     name:"fadegrayscale",
     duration: 500
   }
})
}

oneTransitionMergeVideos()

It will concatenate a video, with audio, and transitions. Wohooo!!!

Use editly

npm i editly

Concat videos like this:

const clips = [
{
    layers: {
        type: "video",
        path: "path/to/clip1.mp4"
    }
},
{
    layers: {
        type: "video",
        path: "path/to/clip2.mp4"
    }
}
];
await editly({
    keepSourceAudio: true,   //to keep audio
    outPath: "./data/merged.mp4",
    clips: clips,
    //lot's of other great features
});

(I've spent a long day on ffmpeg and other non-nodejs solutions as well,this one is fare the easiest solution, altho using ffmpeg directly might be faster)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745248257a4618517.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信