使用 NodeJS 将 ffmpeg 文件输出上传到 AWS s3

Upload ffmpeg file output to AWS s3 using NodeJS

ffmpeg.output("path/file.mp4") 需要一个 string 路径作为将输出文件写入其中的参数。但是 s3 bucket.upload(parms, ...) 需要一个二进制文件作为 parms JSON

Body: 的值

问题:无法使用 NodeJS 环境中 s3 存储桶的文件路径提供文件数据

FFmpeg()
  .input("source.mp4") //video
  .setStartTime(startTime)
  .setDuration(duration)
  .output(output)    //output file path: string
  .on("end", function() {
    console.log("Processing finished successfully");
    var params = {
      Bucket: process.env.S3_BUCKET,
      Key: "videos/filename.mp4",
      Body: output    //binary file data to be provided not file path
    };
    const bucket = new S3({
      accessKeyId: process.env.S3_ACCESS_KEY_ID,
      secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
      region: process.env.S3_REGION
    });
    bucket.upload(params, function(err, data) {
      console.log(err, data);
    });
  })
  .run();

使用 fs.readFileSync() 完成,它为我提供了我传递给 putObject()

的文件缓冲区
import fs = require("fs");

const inputPath = "source.mp4"
const outputPath = "clip.mp4";

FFmpeg()
  .input(inputPath)            //input video path or URL
  .setStartTime(2)
  .setDuration(2)
  .output(outputPath)          //output file path: string
  .on("end", function() {
    console.log("Processing finished successfully");
    const fileContent = fs.readFileSync(outputPath);
    var params = {
      Bucket: process.env.S3_BUCKET,
      Key: "videos/clip.mp4",
      Body: fileContent        //got buffer by reading file path
    };
    const bucket = new S3({
      accessKeyId: process.env.S3_ACCESS_KEY_ID,
      secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
      region: process.env.S3_REGION
    });
    bucket.putObject(params, function(err, data) {
      console.log(err, data);
    });
  })
  .run();