ffmpeg 文件转换 AWS Lambda

ffmpeg file conversion AWS Lambda

我希望 .webm 文件在到达我的 S3 存储桶后转换为 .wav 文件。我关注了 this tutorial and tried to adapt it from my use case using the .webm -> .wav ffmpeg command described here.

我的 AWS Lambda 函数通常可以正常工作,因为当我的 .webm 文件到达源存储桶时,它会转换为 .wav 并最终进入目标存储桶。但是,生成的文件 .wav 始终为 0 字节(尽管 .webm 不是,包括适当的音频)。我是否调整了代码错误?我只更改了第一个 link 的 ffmpeg_cmd 行。

import json
import os
import subprocess
import shlex
import boto3

S3_DESTINATION_BUCKET = "hmtm-out"
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 = s3_source_basename + ".wav"

    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 -i \"" + s3_source_signed_url + "\" -c:a pcm_f32le " + s3_destination_filename + " -"
    
    
    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')
    }
 

所提供的代码使用 ffmpeg 的输出作为要上传的源数据。为此,ffmpeg 需要输出数据。分解命令

"-i \"" + s3_source_signed_url + "\" " +   # The input filename to use
"-c:a pcm_f32le " +                        # The encoder to use
s3_destination_filename + " " +            # The output filename to write to
"-"                                        # Output data to stdout

换句话说,您是在告诉 ffmpeg 使用两个不同的输出。这不是你想要的。最重要的是,如果您删除输出文件名,使其仅尝试使用标准输出,它将不知道要使用哪种文件格式。

如果您使用如下命令:

ffmpeg_cmd = "/opt/bin/ffmpeg -i \"" + s3_source_signed_url + "\" -c:a pcm_f32le -f wav -"

它应该能满足您的需求。这里 ffmpeg 已被指示输出为 wav 文件,并将输出仅发送到 stdout。

我尝试了将数据输出到 stdout 的方法,但花了超过 15 分钟,但失败了。

所以我使用可以容纳超过 512 MB 文件的 EFS。 Using Amazon EFS with Lambda - AWS Lambda