amazon-web-services - AWS Lambda : ffmpeg thumbnails Generator: empty JPG

标签 amazon-web-services amazon-s3 ffmpeg aws-lambda

当在 S3 上上传视频时,我想存储 JPG 屏幕截图。

在亚马逊 AWS 的 lambda 函数上,我这样做:

...

  let tmpFile = createWriteStream(`/tmp/screenshot.jpg`)

  var ffmpeg = spawn(ffmpegPath, [
      "-ss","00:00:05",
      "-i", target,
      "-vf", "thumbnail,scale=200:200", 
      "-qscale:v" ,"2",
      "-frames:v", "1",
      "-f", "image2",
      "-c:v", "mjpeg",
      "pipe:1"
    ]);

  ffmpeg.stdout.pipe(tmpFile).on("error", err => {
      console.log("Error A: ",err);
    });

  ffmpeg.on('error', err => {
    console.log("Error B", err)
    reject()
  })

  ffmpeg.on('close', code => {
    tmpFile.end();
    console.log('Log A', ffmpeg);

    child_process.exec("echo `ls -l -R /tmp`",
      (error, stdout, stderr) => {
        console.log(stdout)
    });

    resolve()
  })
...

但结果是 S3 中的一个空 JPG 文件。

日志显示没有错误,我的“目标”没问题,stdout ls 显示空的 JPG 文件。

我尝试了很多东西,比如使用其他版本的 ffmpeg 但相同。

有“console.log('Log A', ffmpeg)”:
ChildProcess {
 _events: [Object: null prototype] { error: [Function], close: [Function] },
 _eventsCount: 2,
 _maxListeners: undefined,
 _closesNeeded: 3,
 _closesGot: 3,
 connected: false,
 signalCode: 'SIGSEGV',
 exitCode: null,
 killed: false,
 spawnfile: '/opt/nodejs/ffmpeg',
 _handle: null,
 spawnargs: [
   '/opt/nodejs/ffmpeg',
   '-ss',
   '00:00:05',
   '-i',
   'https://xxxxxxxxx',
   '-vf',
   'thumbnail,scale=200:200',
   '-qscale:v',
   '2',
   '-frames:v',
   '1',
   '-f',
   'image2',
   '-v',
   '16',
   '-c:v',
   'mjpeg',
   'pipe:1'
 ],
 pid: 24,
 stdin: Socket {
   connecting: false,
   _hadError: false,
   _parent: null,
   _host: null,
   _readableState: ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: null, tail: null, length: 0 },
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: null,
     ended: false,
     endEmitted: false,
     reading: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     resumeScheduled: false,
     paused: true,
     emitClose: false,
     autoDestroy: false,
     destroyed: true,
     defaultEncoding: 'utf8',
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null
   },
   readable: false,
   _events: [Object: null prototype] { end: [Function: onReadableStreamEnd] },
   _eventsCount: 1,
   _maxListeners: undefined,
   _writableState: WritableState {
     objectMode: false,
     highWaterMark: 16384,
     finalCalled: false,
     needDrain: false,
     ending: false,
     ended: false,
     finished: false,
     destroyed: true,
     decodeStrings: false,
     defaultEncoding: 'utf8',
     length: 0,
     writing: false,
     corked: 0,
     sync: true,
     bufferProcessing: false,
     onwrite: [Function: bound onwrite],
     writecb: null,
     writelen: 0,
     bufferedRequest: null,
     lastBufferedRequest: null,
     pendingcb: 0,
     prefinished: false,
     errorEmitted: false,
     emitClose: false,
     autoDestroy: false,
     bufferedRequestCount: 0,
     corkedRequestsFree: [Object]
   },
   writable: false,
   allowHalfOpen: false,
   _sockname: null,
   _pendingData: null,
   _pendingEncoding: '',
   server: null,
   _server: null,
   [Symbol(asyncId)]: 5,
   [Symbol(kHandle)]: null,
   [Symbol(lastWriteQueueSize)]: 0,
   [Symbol(timeout)]: null,
   [Symbol(kBuffer)]: null,
   [Symbol(kBufferCb)]: null,
   [Symbol(kBufferGen)]: null,
   [Symbol(kBytesRead)]: 0,
   [Symbol(kBytesWritten)]: 0
 },
 stdout: Socket {
   connecting: false,
   _hadError: false,
   _parent: null,
   _host: null,
   _readableState: ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: null, tail: null, length: 0 },
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: false,
     ended: true,
     endEmitted: true,
     reading: false,
     sync: false,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     resumeScheduled: false,
     paused: false,
     emitClose: false,
     autoDestroy: false,
     destroyed: true,
     defaultEncoding: 'utf8',
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null
   },
   readable: false,
   _events: [Object: null prototype] {
     end: [Function: onReadableStreamEnd],
     close: [Function]
   },
   _eventsCount: 2,
   _maxListeners: undefined,
   _writableState: WritableState {
     objectMode: false,
     highWaterMark: 16384,
     finalCalled: false,
     needDrain: false,
     ending: false,
     ended: false,
     finished: false,
     destroyed: true,
     decodeStrings: false,
     defaultEncoding: 'utf8',
     length: 0,
     writing: false,
     corked: 0,
     sync: true,
     bufferProcessing: false,
     onwrite: [Function: bound onwrite],
     writecb: null,
     writelen: 0,
     bufferedRequest: null,
     lastBufferedRequest: null,
     pendingcb: 0,
     prefinished: false,
     errorEmitted: false,
     emitClose: false,
     autoDestroy: false,
     bufferedRequestCount: 0,
     corkedRequestsFree: [Object]
   },
   writable: false,
   allowHalfOpen: false,
   _sockname: null,
   _pendingData: null,
   _pendingEncoding: '',
   server: null,
   _server: null,
   write: [Function: writeAfterFIN],
   [Symbol(asyncId)]: 6,
   [Symbol(kHandle)]: null,
   [Symbol(lastWriteQueueSize)]: 0,
   [Symbol(timeout)]: null,
   [Symbol(kBuffer)]: null,
   [Symbol(kBufferCb)]: null,
   [Symbol(kBufferGen)]: null,
   [Symbol(kBytesRead)]: 0,
   [Symbol(kBytesWritten)]: 0
 },
 stderr: Socket {
   connecting: false,
   _hadError: false,
   _parent: null,
   _host: null,
   _readableState: ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: null, tail: null, length: 0 },
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: null,
     ended: true,
     endEmitted: true,
     reading: false,
     sync: false,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     resumeScheduled: false,
     paused: true,
     emitClose: false,
     autoDestroy: false,
     destroyed: true,
     defaultEncoding: 'utf8',
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null
   },
   readable: false,
   _events: [Object: null prototype] {
     end: [Function: onReadableStreamEnd],
     close: [Function]
   },
   _eventsCount: 2,
   _maxListeners: undefined,
   _writableState: WritableState {
     objectMode: false,
     highWaterMark: 16384,
     finalCalled: false,
     needDrain: false,
     ending: false,
     ended: false,
     finished: false,
     destroyed: true,
     decodeStrings: false,
     defaultEncoding: 'utf8',
     length: 0,
     writing: false,
     corked: 0,
     sync: true,
     bufferProcessing: false,
     onwrite: [Function: bound onwrite],
     writecb: null,
     writelen: 0,
     bufferedRequest: null,
     lastBufferedRequest: null,
     pendingcb: 0,
     prefinished: false,
     errorEmitted: false,
     emitClose: false,
     autoDestroy: false,
     bufferedRequestCount: 0,
     corkedRequestsFree: [Object]
   },
   writable: false,
   allowHalfOpen: false,
   _sockname: null,
   _pendingData: null,
   _pendingEncoding: '',
   server: null,
   _server: null,
   write: [Function: writeAfterFIN],
   [Symbol(asyncId)]: 7,
   [Symbol(kHandle)]: null,
   [Symbol(lastWriteQueueSize)]: 0,
   [Symbol(timeout)]: null,
   [Symbol(kBuffer)]: null,
   [Symbol(kBufferCb)]: null,
   [Symbol(kBufferGen)]: null,
   [Symbol(kBytesRead)]: 0,
   [Symbol(kBytesWritten)]: 0
 },
 stdio: [
   Socket {
     connecting: false,
     _hadError: false,
     _parent: null,
     _host: null,
     _readableState: [ReadableState],
     readable: false,
     _events: [Object: null prototype],
     _eventsCount: 1,
     _maxListeners: undefined,
     _writableState: [WritableState],
     writable: false,
     allowHalfOpen: false,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     server: null,
     _server: null,
     [Symbol(asyncId)]: 5,
     [Symbol(kHandle)]: null,
     [Symbol(lastWriteQueueSize)]: 0,
     [Symbol(timeout)]: null,
     [Symbol(kBuffer)]: null,
     [Symbol(kBufferCb)]: null,
     [Symbol(kBufferGen)]: null,
     [Symbol(kBytesRead)]: 0,
     [Symbol(kBytesWritten)]: 0
   },
   Socket {
     connecting: false,
     _hadError: false,
     _parent: null,
     _host: null,
     _readableState: [ReadableState],
     readable: false,
     _events: [Object: null prototype],
     _eventsCount: 2,
     _maxListeners: undefined,
     _writableState: [WritableState],
     writable: false,
     allowHalfOpen: false,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     server: null,
     _server: null,
     write: [Function: writeAfterFIN],
     [Symbol(asyncId)]: 6,
     [Symbol(kHandle)]: null,
     [Symbol(lastWriteQueueSize)]: 0,
     [Symbol(timeout)]: null,
     [Symbol(kBuffer)]: null,
     [Symbol(kBufferCb)]: null,
     [Symbol(kBufferGen)]: null,
     [Symbol(kBytesRead)]: 0,
     [Symbol(kBytesWritten)]: 0
   },
   Socket {
     connecting: false,
     _hadError: false,
     _parent: null,
     _host: null,
     _readableState: [ReadableState],
     readable: false,
     _events: [Object: null prototype],
     _eventsCount: 2,
     _maxListeners: undefined,
     _writableState: [WritableState],
     writable: false,
     allowHalfOpen: false,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     server: null,
     _server: null,
     write: [Function: writeAfterFIN],
     [Symbol(asyncId)]: 7,
     [Symbol(kHandle)]: null,
     [Symbol(lastWriteQueueSize)]: 0,
     [Symbol(timeout)]: null,
     [Symbol(kBuffer)]: null,
     [Symbol(kBufferCb)]: null,
     [Symbol(kBufferGen)]: null,
     [Symbol(kBytesRead)]: 0,
     [Symbol(kBytesWritten)]: 0
   }
 ]
} ```

最佳答案

我通过将 Lambda 函数的内存限制从 512 MB 增加到 1024 MB 解决了这个问题。

关于amazon-web-services - AWS Lambda : ffmpeg thumbnails Generator: empty JPG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60131261/

相关文章:

c++ - 在 libffmpeg C++ 中连接文件

FFMPEG 将不正确的 mp4 转换为 ts

php - 502 高负载下使用 PHP-FPM 的 nginx 网关错误

amazon-web-services - 由于端口正在使用,我无法将新容器部署到我的 ECS 集群

javascript - 使用 javascript sdk 的亚马逊 s3 上传无法正常工作

sql - 无法使用 AWS S3 Select 对 JSON 进行查询

ios - 带有自定义 header 的 AWSS3GetPreSignedURLRequest 上传,给出 403

ffmpeg 如何将静止图像放在完整的 vstack 上?

ssh - 尝试通过 ssh 进入实例时关闭了 AWS EC2 连接

javascript - 使用 node.js 将 JSON 从 AWS Lambda 保存到 AWS S3