我的实现中写入EPIPE错误的原因?

时间:2019-05-18 05:06:35

标签: node.js ffmpeg stream stdout stdin

我目前有一个小型的node.js express应用程序,用于使用简单缩略图包/ ffmpeg使用从.mp4视频创建的清晰和缩略图上传图像。

当上传图像时,该应用程序运行完美,文件经过锐利处理后上传,然后将其传递,最终按预期放在我的Amazon s3存储桶中。没有错误。

但是,当我通过使用ffmpeg子进程的simple-thumbnails genThumbnail()函数上传用于创建和上传缩略图的mp4视频时,缩略图会成功上传到我的s3存储桶,但是我的应用返回 EPIPE写入错误,而不返回S3上已上传文件的URL。

是什么原因造成的,以及考虑到返回的EPIPE错误,它还可以正常工作,我该如何解决?谢谢你!

所使用的关注软件包如下:

aws-sdk

multer

multer-s3 <-具有转换选项的此选项,不是标准的multer-s3软件包

simple-thumbnail

const express = require('express');
const app = express();

const aws = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3'); //github:gmenih341/multer-s3 version of multer-s3 with transform option
const sharp = require('sharp');
const genThumbnail = require('simple-thumbnail');

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Orgin, X-Requested-With, Content-Type, Accept, Authorization');
    if (req.method === 'OPTIONS') {
        res.header('Access-Control-Allow-Methods', 'POST');
        return res.status(200).json({});
    }
    next();
});

let uniqueFileName;
let s3BucketName = 'bucketname';

let s3 = new aws.S3({
    accessKeyId: ACCESS_KEY,
    secretAccessKey: SECRET_KEY,
    Bucket: s3BucketName
});

let upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: s3BucketName,
        acl: 'public-read',
        cacheControl: 'max-age=31536000',
        contentType: multerS3.AUTO_CONTENT_TYPE,
        shouldTransform: true,
        transforms: [{
            id: 'thumbnail',
            key: function (req, file, cb) {
                uniqueFileName = Date.now().toString();
                cb(null, uniqueFileName + '.jpg')
            },
            transform: function (req, file, cb) {
                if (file.mimetype == 'video/mp4') {
                    //When using simple-thumbnails' getThumbnail() on an mp4 video it uploads succesfully to S3 but node returns EPIPE write error
                    cb(null, genThumbnail(null, null, '250x?'))
                } else {
                    //When using sharp to resize an image this works perfectly and retuns the JSON below with the files S3 URL
                    cb(null, sharp().jpeg())
                }

            }
        }]
    })
});

app.post('/upload', upload.array('theFile'), (req, res) => {
    res.json({
        fileS3Url: 'https://s3.amazonaws.com/'+ s3BucketName +'/' + uniqueFileName
    });
});

app.use((req, res, next) => {
    const error = new Error('Not found');
    error.status = 404;
    next(error);
});

app.use((error, req, res, next) => {
    res.status(error.status || 500);
    res.json({
        error: {
            message: error.message
        }
    });
});

module.exports = app;

0 个答案:

没有答案