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

查看:161
本文介绍了在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天全站免登陆