python - Lambda/ffmpeg 延时生成 - 输出零字节,无法调试 ffmpeg

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

我正在尝试使用 AWS Lambda FFMPEG 层在 S3 存储桶中构建静态图像的延时摄影。首先,我的项目基于位于 here 的教程。 .
我可以复制教程中的步骤,所以我知道 FFMPEG 层在 Lambda 中工作。我已经在独立服务器上复制了 FFMPEG 命令,所以我知道它们是正确的。
这是我的设置:我有两个 S3 存储桶,lambda-source-bucketlambda-destination-bucket . lambda-source-bucket的内容是:

1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
6.jpg
7.jpg
files.txt
files.txt包含这个:
file 'https://lambda-source-bucket.s3.us-west-2.amazonaws.com/1.jpg'
file 'https://lambda-source-bucket.s3.us-west-2.amazonaws.com/2.jpg'
file 'https://lambda-source-bucket.s3.us-west-2.amazonaws.com/3.jpg'
file 'https://lambda-source-bucket.s3.us-west-2.amazonaws.com/4.jpg'
file 'https://lambda-source-bucket.s3.us-west-2.amazonaws.com/5.jpg'
file 'https://lambda-source-bucket.s3.us-west-2.amazonaws.com/6.jpg'
file 'https://lambda-source-bucket.s3.us-west-2.amazonaws.com/7.jpg'
这是我的 Lambda 函数代码(在 Python 中):
import json
import os
import subprocess
import shlex
import boto3

S3_DESTINATION_BUCKET = "lambda-destination-bucket"
SIGNED_URL_TIMEOUT = 60

def lambda_handler(event, context):

    s3_source_bucket = event['Records'][0]['s3']['bucket']['name']
    s3_source_key = event['Records'][0]['s3']['object']['key']

    s3_source_basename = os.path.splitext(os.path.basename(s3_source_key))[0]
    s3_destination_filename = "timelapse.mp4"

    s3_client = boto3.client('s3')
    s3_source_signed_url = s3_client.generate_presigned_url('get_object',
        Params={'Bucket': s3_source_bucket, 'Key': s3_source_key},
        ExpiresIn=SIGNED_URL_TIMEOUT)

    ffmpeg_cmd = "/opt/bin/ffmpeg -y -r 24 -f concat -safe 0 -protocol_whitelist file,http,tcp,https,tls -I ""https://lambda-source-bucket.s3.us-west-2.amazonaws.com/files.txt"" -c copy -s 1024x576 -vcodec libx264 -"    
command1 = shlex.split(ffmpeg_cmd)
    p1 = subprocess.run(command1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    resp = s3_client.put_object(Body=p1.stdout, Bucket=S3_DESTINATION_BUCKET, Key=s3_destination_filename)

    return {
        'statusCode': 200,
        'body': json.dumps('Processing complete successfully')
    }
Lambda 函数的触发器是新的 files.txt文件添加到 lambda-source-bucket .
到目前为止,我已经能够触发触发器,该函数应该可以正常运行(在 Cloudwatch 中),并且该函数会创建一个新的 timelapse.mp4lambda-destination-bucket .但是这个文件是0 bytes .我在 Cloudwatch 控制台中看不到 FFMPEG 错误,但我不确定我是否知道如何配置我的 Lambda 函数代码来记录 FFMPEG 错误。
另外:如果我以完全错误的方式处理这个问题,我很想听听反馈。我猜 concatfiles.txt循环方法https://这不是最有效的方法,但这是我迄今为止唯一能解决这个问题的方法。
任何帮助都是最真诚和谦卑的感谢。

最佳答案

我正在尝试使用相同的教程做完全相同的事情。
这是我最终“解决”(解决?)的方法:
我放弃了 concat 文件*。我也放弃了将输出直接流式传输到 s3 对象。相反,我在 lambda 上做了所有事情。
我将每个图像下载到“/tmp”(例如 /tmp/1.jpg )。然后我运行 ffmpeg 并将其输出到那里(例如 /tmp/timelapse.mp4 )。最后我跑了s3_client.upload_file()将该mp4放入s3。
我不确定哪个部分没有工作(concat 文件或流输出),并且由于我的本地 lambda 似乎没有超时,我不愿意恢复和排除故障(还)。
*对于它的值(value),我的 concat 文件有预签名的 URL,并且各种文档说在每个之间添加一行,并带有“duration [x]”(其中 x 是一个 int),然后最后一行应该是一个重复最后一张图片。

关于python - Lambda/ffmpeg 延时生成 - 输出零字节,无法调试 ffmpeg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68912955/

相关文章:

python - Python 中的 Json 文件 - 输出摘要

java - 找不到依赖项 'com.amazonaws'

ruby-on-rails - 无法在同一 VPC 上将 aws redis 与 ec2 连接

ffmpeg - 向视频添加mp3时音质低,为什么?

python - sqlalchemy.exc.InvalidRequestError : Mapper '...' has no property '...' 错误

python - 编写Python或TCL VMD脚本

python - Seaborn BarPlot 反转 y 轴并将 x 轴保持在图表区域的底部

amazon-web-services - 从Internet下载文件到S3存储桶

google-app-engine - ffmpeg 无法在谷歌应用引擎标准 nodejs 中正确执行

python - 捕获 RTP 时间戳