将FFmpeg输出直接上传到Amazon S3 [英] Upload FFmpeg output directly to Amazon S3

查看:124
本文介绍了将FFmpeg输出直接上传到Amazon S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有node.js的fluent-ffmpeg库,将原始Flash影片格式的视频转码为具有多种分辨率,1080p等的mp3格式.转码完成后,我想移动转码后的视频视频到s3存储桶.

I am using the fluent-ffmpeg library with node.js to transcode videos originally in a flash movie format to the mp3 format with multiple resolutions, 1080p, etc.. Once the transcoding is complete, I would like to move the transcoded video to an s3 bucket.

我从源s3存储桶中提取原始.flv文件,并将流传递给ffmpeg构造函数.问题是转码完成后,我如何才能将mp4数据流发送到s3.

I pull the original .flv file from a source s3 bucket and pass the stream to the ffmpeg constructor function. The issue is after the transcoding completes, how do I then get the stream of the mp4 data to send to s3.

这是我到目前为止的代码:

Here is the code I have so far:

        var params = {
            Bucket: process.env.SOURCE_BUCKET,
            Key: fileName
        };
        s3.getObject(params, function(err, data) {
            if (err) console.log(err, err.stack); // an error occurred

            var format = ffmpeg(data)
            .size('854x480')
            .videoCodec('libx264')
            .format('flv')
            .toFormat('mp4');
            .on('end', function () {
                //Ideally, I would like to do the uploading here

                var params = {
                   Body: //{This is my confusion, how do I get the stream to add here?},
                   Bucket: process.env.TRANSCODED_BUCKET,
                   Key: fileName
                };
                s3.putObject(params, function (err, data) {

               });
            })
            .on('error', function (err) {
                console.log('an error happened: ' + err.message);
            });

        });

对于上面的代码,我在哪里可以获取转码后的流以添加到params对象的"Body"属性中?

For the code above, where can I get the transcoded stream to add to the "Body" property of the params object?

更新:

这是我要做的事情的修订版:

Here is a revision of what I am trying to do:

var outputStream: MemoryStream = new MemoryStream();

        var proc = ffmpeg(currentStream)
            .size('1920x1080')
            .videoCodec('libx264')
            .format('avi')
            .toFormat('mp4')
            .output(outputStream)
            // setup event handlers
            .on('end', function () {
                uploadFile(outputStream, "").then(function(){
                    resolve();
                })
            })
            .on('error', function (err) {
                console.log('an error happened: ' + err.message);
            });

我想避免将文件从s3复制到本地文件系统,而是希望处理内存中的文件并在完成后上传回s3. fluent-ffmpeg会允许这种情况吗?

I would like to avoid copying the file to the local filesystem from s3, rather I would prefer to process the file in memory and upload back to s3 when finished. Would fluent-ffmpeg allow this scenario?

推荐答案

您似乎没有将转码输出保存在任何地方.

You don't seem to be saving the output of the transcoding anywhere.

  1. 使用 putObject文档Body参数接受:
  1. Save the output of the transcoding (your new .flv file) using output to your local filesystem.
  2. Provide your new file's contents to putObject. According to the putObject documentation, the Body parameter accepts:

Body —(BufferTyped ArrayBlobStringReadableStream)对象数据.

Body — (Buffer, Typed Array, Blob, String, ReadableStream) Object data.

这是一些经过修改的示例代码:

Here's some revised sample code:

// Generate a filename for the `.flv` version
var flvFileName = fileName.substring(0, fileName.length - path.extname(fileName).length) + '.flv';

// Perform transcoding, save new video to new file name
var format = ffmpeg(data)
    .size('854x480')
    .videoCodec('libx264')
    .format('flv')
    .toFormat('mp4');
    .output(flvFileName)
    .on('end', function () {
        // Provide `ReadableStream` of new video as `Body` for `pubObject`
        var params = {
             Body: fs.createReadStream(flvFileName)
             Bucket: process.env.TRANSCODED_BUCKET,
             Key: flvFileName
        };

        s3.putObject(params, function (err, data) {

        });
    })

注意:您可以根据需要从fluent-ffmpeg创建输出流并将其上传到AWS S3,但这会使逻辑和错误处理复杂化.

Note: You may be able to create an output stream from fluent-ffmpeg and upload that stream to AWS S3, if you prefer, but this will complicate the logic and error handling.

这篇关于将FFmpeg输出直接上传到Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆