6

我能够将图像上传到S3. 现在,如果选择的文件是.gif,我希望能够将.gif文件转换为并将转换后的文件.mp4上传到S3. 只有给出文件的路径,我才能将 a 转换.gif.mp4with 。ffmpeg如何访问上传的文件Multer?下面是我的代码:

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var aws = require('aws-sdk');
var multer = require('multer');
var multerS3 = require('multer-s3');
var s3 = new aws.S3();
var ffmpeg = require('fluent-ffmpeg');


var upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: 'myBucket',
        key: function (req, file, cb) {
            console.log(file);
            var extension = file.originalname.substring(file.originalname.lastIndexOf('.')+1).toLowerCase();

                if(extension=="gif"){
                console.log("Uploaded a .gif file");

                ffmpeg(file) //THIS IS NOT WORKING
                    .setFfmpegPath("C:\\ffmpeg\\bin\\ffmpeg.exe")
                      .output('./outputs/2.mp4')    //TRYING TO UPLOAD LOCALLY, WHICH FAILS
                      .on('end', function() {
                        console.log('Finished processing');
                      })
                      .run();
            }

            cb(null, filename);
        }
    })
});

我正在尝试像这样访问上传的文件: ffmpeg(file)因为file是函数中传递的参数multer

我的表格:

<form action="/upload" method="post"  enctype="multipart/form-data">
    <input type="file" name="file"> <br />
    <input type="submit" value="Upload">
</form>

在过程的哪个部分转换文件?

请帮忙。非常感谢。

4

1 回答 1

8

您正在尝试在本地处理位于s3. 该文件需要是您服务器的文件系统,或者至少可以在s3. 所以你在这里有两个选择。

a)您可以先将所有文件上传到运行express的服务器上(不是s3我们先暂时存储它们)。如果文件是.gif,则对其进行处理并上传结果.mp4文件,否则上传到s3. 这是一个工作示例:

var fs         = require('fs')
var path       = require('path')
var express    = require('express');
var bodyParser = require('body-parser');
var aws        = require('aws-sdk');
var multer     = require('multer');
var ffmpeg     = require('fluent-ffmpeg');
var shortid    = require('shortid');


aws.config.update(/* your config */);


var app    = express();
var s3     = new aws.S3();
var bucket = 'myBucket';

var upload = multer({
    storage: multer.diskStorage({
        destination: './uploads/',
        filename: function (req, file, cb){
            // user shortid.generate() alone if no extension is needed
            cb( null, shortid.generate() + path.parse(file.originalname).ext);
        }
    })
});


//----------------------------------------------------
app.post('/upload', upload.single('file'), function (req, res, next) {

    var fileInfo = path.parse(req.file.filename);

    if(fileInfo.ext === '.gif'){

        var videoPath = 'uploads/' + fileInfo.name + '.mp4';

        ffmpeg(req.file.path)
            .setFfmpegPath("C:\\ffmpeg\\bin\\ffmpeg.exe")
            .output(videoPath)
            .on('end', function() {
                console.log('[ffmpeg] processing done');
                uploadFile(videoPath, fileInfo.name + '.mp4');
            })
            .run();
    }
    else {
        uploadFile(req.file.path, req.file.filename);
    }

    res.end();
});


//----------------------------------------------------
function uploadFile(source, target){

    fs.readFile(source, function (err, data) {

        if (!err) {

            var params = {
                Bucket      : bucket,
                Key         : target,
                Body        : data
            };

            s3.putObject(params, function(err, data) {
                if (!err) {
                    console.log('[s3] file uploaded:');
                    console.log(data);
                    fs.unlink(source); // optionally delete the file
                }
                else {
                    console.log(err);
                }
            });
        }
    });
}


app.listen(3000);

b)或者,如果您可以将s3文件公开,您可以使用multer-s3. 由于ffmpeg还接受网络位置作为输入路径,您可以将文件的s3位置传递给它.gif,然后上传转换后的.mp4文件:

var fs         = require('fs')
var path       = require('path')
var express    = require('express');
var bodyParser = require('body-parser');
var aws        = require('aws-sdk');
var multer     = require('multer');
var ffmpeg     = require('fluent-ffmpeg');
var multerS3   = require('multer-s3');


aws.config.update(/* your config */);


var app    = express();
var s3     = new aws.S3();
var bucket = 'myBucket';

var upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: bucket,
        key: function (req, file, cb) {
            cb(null, file.originalname);
        },
        acl: 'public-read'
    })
});

----------------------------------------------------
app.post('/upload', upload.single('file'), function (req, res, next) {

    var fileInfo = path.parse(req.file.originalname);

    if(fileInfo.ext === '.gif'){

        var videoPath = 'uploads/' + fileInfo.name + '.mp4';

        ffmpeg(req.file.location)
            .setFfmpegPath("C:\\ffmpeg\\bin\\ffmpeg.exe")
            .output(videoPath)
            .on('end', function() {
                console.log('[ffmpeg] processing done');
                uploadFile(videoPath, fileInfo.name + '.mp4');
            })
            .run();
    }

    res.end();
})


//----------------------------------------------------
function uploadFile(source, target){

    fs.readFile(source, 'base64', function (err, data) {

        if (!err) {

            var params = {
                Bucket      : bucket,
                Key         : target,
                Body        : data,
                ContentType : 'video/mp4'
            };

            s3.putObject(params, function(err, data) {
                if (!err) {
                    console.log('[s3] file uploaded:');
                    console.log(data);
                    fs.unlink(source); // optionally delete the file
                }
                else {
                    console.log(err);
                }
            });
        }
    });
}

app.listen(3000);

对于这两个示例,请记住创建一个uploads/文件夹并使用您的aws配置。

于 2017-04-10T15:44:09.240 回答