首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在nodejs上使用ffmpeg拼接mp4视频文件

在nodejs上使用ffmpeg拼接mp4视频文件
EN

Stack Overflow用户
提问于 2020-04-03 09:09:47
回答 1查看 3.5K关注 0票数 1

我正在尝试使用FFmpeg和Nodejs来连接几个视频。我收到“没有这样的文件或目录”错误。编写的代码是:

代码语言:javascript
复制
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);

错误是

代码语言:javascript
复制
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 command line would be:

ffmpeg -i Videoofile1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate_videofile1.ts ffmpeg -i Videoofile2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate_videofile2.ts ffmpeg -i -c copy -bsf:a aac_adtstoasc ./output/output.mp4

你能帮我们吗?非常感谢。Truly.:>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-07 19:01:51

所以,我找到的解决方案是https://www.npmjs.com/package/ffmpeg-concat

第一次安装:

代码语言:javascript
复制
npm i ffmpeg-concat

然后:连接视频:

代码语言:javascript
复制
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()

它将连接视频、音频和过渡。呼呼!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61003625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档