gpt4 book ai didi

node.js - 使用 NodeJS 将 ffmpeg 文件输出上传到 AWS s3

转载 作者:行者123 更新时间:2023-12-04 23:02:16 29 4
gpt4 key购买 nike

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

问题:无法使用 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();

关于node.js - 使用 NodeJS 将 ffmpeg 文件输出上传到 AWS s3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59685461/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com