【问题标题】:Read S3 video file, process it with ffmpeg and upload to S3读取 S3 视频文件,用 ffmpeg 处理并上传到 S3
【发布时间】:2020-04-28 16:40:51
【问题描述】:

我有一个存储在 s3 存储桶中的视频,带有经过身份验证的读取 ACL。

我需要阅读并使用 ffmpeg (nodejs) 制作预告片

这是我用来生成预告片的代码

exports.generatePreview = (req, res) => {
    const getParams = {
        Bucket: S3_CREDENTIALS.bucketName,
        Key: req.params.key
    }
    s3.getSignedUrl('getObject', getParams, (err, signedRequest) => {
        console.log(signedRequest, err, 'getSignedUrl')
        ffmpeg(new URL(signedRequest))
            .size('640x?')
            .aspect('4:3')
        .seekInput('3:00')
        .duration('0:30')
        .then(function (video) {
            s3.putObject({ Bucket: S3_CREDENTIALS.bucketName, key: 'preview_' + req.body.key, Body: video }, function (err, data) {
                console.log(err, data)
            })
        });
});

}

不幸的是,构造函数路径似乎没有读取远程 url。如果我尝试使用相同的signedurl(即ffmpeg -i "https://[bucketname].s3.eu-west-1.amazonaws.com/[key.mp4]?[signedParams]" -vn -acodec pcm_s16le -ar 44100 -ac 2 video.wav)执行ffmpeg命令行

我得到的错误是signedRequest url 'The input file does not exist'

似乎 fs.readFileSync https 不受支持,即使我尝试使用 http 请求得到相同的结果。 fs.readFileSync(signedurl) => 给出相同的结果

如何解决这个问题?

【问题讨论】:

    标签: amazon-web-services amazon-s3 ffmpeg


    【解决方案1】:

    如果您使用的是 node-ffmpeg,这是不可能的,因为该库仅接受指向本地路径的字符串,但 fluent-ffmpeg 确实支持读取流,因此请尝试一下。

    例如(未经测试,只是吐口水):

    const ffmpeg = require('fluent-ffmpeg');
    const stream = require('stream');
    
    exports.generatePreview = (req, res) => {
      let params = {Bucket: S3_CREDENTIALS.bucketName, Key: req.params.key};
    
      // Retrieve object stream
      let readStream = s3.getObject(params).createReadStream();
    
      // Set up the ffmpeg process
      let ffmpegProcess = new ffmpeg(readStream)
        //Add your args here
        .toFormat('mp4');  
    
      ffmpegProcess.on('error', (err, stdout, stderr) => {
        // Handle errors here
      }).on('end', () => {
        // Processing is complete
      }).pipe(() => {
        // Create a new stream
        let pt = new stream.PassThrough();
    
        // Reuse the same params object and set the Body to the stream
        params.Key = 'preview_' + req.body.key;
        params.Body = pt;
    
        // Upload and wait for the result
        s3.upload(params, (err, data) => {
          if (err)
            return console.error(err);
    
          console.log("done");
        })
      });
    });
    

    这将有很高的内存要求,因此如果这是一个 Lambda 函数,您可能会尝试仅检索文件的前 X 个字节并仅转换它。

    【讨论】:

    • 这个问题解决了。谢谢你。现在它给了我一个转换错误,但也许是用 args 来生成较小的预告片。
    猜你喜欢
    • 2019-10-26
    • 2018-09-30
    • 2012-04-23
    • 1970-01-01
    • 2020-07-26
    • 2013-05-19
    • 2013-06-03
    • 2017-10-17
    • 2015-06-24
    相关资源
    最近更新 更多