【nodejs】Windows环境 ffmpeg添加水印

发布时间 2023-10-28 19:48:16作者: 蓝色星辰1993

一、Windows下面获取到的字体路径需要做处理,否则无法执行

  • 路径中: 改为 \:
  • 路径中: \ 改为 /
  • 不要使用中文的名称

 

原路径:

D:\Users\670493228\Desktop\public\font\default.ttf

  

使用水印命令(-loglevel debug 可以看到执行日志,方便定位问题)

ffmpeg -i 1.mp4 -vf "drawtext=fontfile='D\:/Users/670493228/Desktop/public/font/default.ttf':text='TEXT':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=15:y=10" -codec:a copy output.mp4  -loglevel debug

 

二、nodejs使用flutter-ffmpeg打水印,直接上代码

  /**
     * Window报错兼容
     * D:\Users\670493228\Desktop\public\font 转为 'D\:/Users/670493228/Desktop/public/font'
     */
    fontFile = fontFile.replaceAll('\\', '/');
    fontFile = fontFile.replaceAll(':', '\\:');
    fontFile = "'" + fontFile + "'";


  return new Promise((reslove, reject) => {
      /**
       * .complexFilter 中参数为文字水印配置
       * .outputOptions 配置了音频设置为视频背景音乐
       */
      ffmpeg(fileName)
        .input(mp3File)
        .complexFilter([
          {
            filter: 'drawtext',
            options: {
              text: "water text",
              x: '100',
              y: '300',
              fontsize: 60,
              fontcolor: 'white',
              fontfile: fontFile,
              box:1,
              boxcolor: 'yellow'
            },
            inputs: '0:v',
          },
        ])
        .outputOptions([
          '-map 0:v:0', 
          '-map 1:a:0'])
        .saveToFile(outFile)
        .on("progress", (progress) => {
          console.log('视频水印,进度: ' + progress.percent + '% done');
        })
        .on("end", () => {
          console.log("视频水印完成"); 
          reslove();
        });
    });