Lambda 上的 FFmpeg 转码导致无法使用(静态)音频 [英] FFmpeg transcoding on Lambda results in unusable (static) audio

查看:26
本文介绍了Lambda 上的 FFmpeg 转码导致无法使用(静态)音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 AWS 中转向无服务器音频转码例程.我一直在尝试设置一个 Lambda 函数来做到这一点;执行静态 FFmpeg 二进制文件并重新上传生成的音频文件.我使用的静态二进制文件是这里.

I'd like to move towards serverless for audio transcoding routines in AWS. I've been trying to setup a Lambda function to do just that; execute a static FFmpeg binary and re-upload the resulting audio file. The static binary I'm using is here.

我在 Python 中使用的 Lambda 函数如下所示:

The Lambda function I'm using in Python looks like this:

import boto3

s3client = boto3.client('s3')
s3resource = boto3.client('s3')

import json
import subprocess 

from io import BytesIO

import os

os.system("cp -ra ./bin/ffmpeg /tmp/")
os.system("chmod -R 775 /tmp")

def lambda_handler(event, context):

    bucketname = event["Records"][0]["s3"]["bucket"]["name"]
    filename = event["Records"][0]["s3"]["object"]["key"]

    audioData = grabFromS3(bucketname, filename)

    with open('/tmp/' + filename, 'wb') as f:
        f.write(audioData.read())

    os.chdir('/tmp/')

    try:
        process = subprocess.check_output(['./ffmpeg -i /tmp/joe_and_bill.wav /tmp/joe_and_bill.aac'], shell=True, stderr=subprocess.STDOUT)
        pushToS3(bucketname, filename)
        return process.decode('utf-8')
    except subprocess.CalledProcessError as e:
        return e.output.decode('utf-8'), os.listdir()


def grabFromS3(bucket, file):

    obj = s3client.get_object(Bucket=bucket, Key=file)
    data = BytesIO(obj['Body'].read())

    return(data)

def pushToS3(bucket, file):

    s3client.upload_file('/tmp/' + file[:-4] + '.aac', bucket, file[:-4] + '.aac')

    return

您可以在此处收听此内容的输出.警告:调低音量,否则您的耳朵会流血.

You can listen to the output of this here. WARNING: Turn your volume down or your ears will bleed.

可以在这里收听原始文件.

有谁知道可能导致编码错误的原因是什么?文件上传似乎没有问题,因为 Lambda fs 上的 md5 与上传文件的 MD5 匹配.

Does anyone have any idea what might be causing the encoding errors? It doesn't seem to be an issue with the file upload, since the md5 on the Lambda fs matches the MD5 of the uploaded file.

我还尝试在 EC2 中的 Amazon Linux 实例上构建静态二进制文件,然后将其压缩并移植到 Lambda 项目中,但同样的问题仍然存在.

I've also tried building the static binary on an Amazon Linux instance in EC2, then zipping and porting it into the Lambda project, but the same issue persists.

我被难住了!:(

推荐答案

好吧,这很有趣.

所以事实证明,Python 子进程从一些在后台运行的 Lambda 进程继承了 stdin.我正在观看 这个 AWS re:Invent 主题演讲,他描述了他们遇到的一些问题.这个问题.

So it turns out the Python subprocess inherits stdin from some Lambda processes going on in the background. I was watching this AWS re:Invent keynote and he was describing some issues they were having w.r.t. this issue.

我将 stdin=subprocess.DEVNULL 添加到子进程调用中,现在音频已修复.

I added stdin=subprocess.DEVNULL to the subprocess call and the audio is now fixed.

如果你问我,这是一个非常有趣的错误.

Very interesting bug if you ask me.

这篇关于Lambda 上的 FFmpeg 转码导致无法使用(静态)音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆