Ok, so I have the same problem as here: Using pydub and AWS Lambda but thought it would be cleaner to re-ask here as I have made some progress (I think?).
I followed the instructions here: https://medium.com/faun/how-to-use-aws-lambda-layers-f4fe6624aff1 like this:
- I created a lambda_function.py file:
from python.pydub import AudioSegment
print('Loading function')
def lambda_handler(event, context):
print(event)
sound = AudioSegment.from_mp3("https://s3-eu-west-1.amazonaws.com/audio.mp3")
etc...
Using docker, I created the dependencies with this script:
#!/bin/bash
export PKG_DIR="python"
rm -rf ${PKG_DIR} && mkdir -p ${PKG_DIR}
docker run --rm -v $(pwd):/foo -w /foo lambci/lambda:build-python3.6 \
pip install -r requirements.txt --no-deps -t ${PKG_DIR}
and the requirements.txt file:
pydub
ffmpeg
Docker downloaded 4 folders:
ffmpeg
ffmpeg-1.4.dist-info
pydub
pydub-0.24.1.dist-info
and I uploaded the whole project to lambda as a zip file.
When I run the code I get an error:
/var/task/pydub/utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
I'm guessing that I need an ffmpeg binary uploading to Lambda but I'm not sure, and if I do, not sure how to do it.
What steps am I missing?
UPDATE:
So I've modified my code and also ran the code using pycharm. Although ffmpeg and ffprobe are uploaded to my lambda function, and I have programmatically confirmed they are present, they fail to run.
My code is now like this:
def lambda_handler(event, context):
l = logging.getLogger("pydub.converter")
l.setLevel(logging.DEBUG)
l.addHandler(logging.StreamHandler())
AudioSegment.converter='/var/task/ffmpeg'
print(event)
print("00000")
print (os.getcwd())
print("00000")
print ("1111")
path = "/var/task"
dir_list = os.listdir(path)
print (dir_list)
print ("1111")
print ("11111111111111111")
out = check_output(['/var/task/ffprobe', 'test.mp3'])
print (out)
print ("11111111111111111")
and this is the cloudwatch error log:
00000
/var/task
00000
1111
['.DS_Store', 'ffmpeg', 'get_layer_packages.sh', 'lambda_function.py', 'lambda_function.py.orig.py', 'python', 'requirements.txt', 'test.mp3']
1111
11111111111111111
[Errno 8] Exec format error: '/var/task/ffmpeg': OSError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 32, in lambda_handler
out = check_output(['/var/task/ffmpeg', 'test.mp3'])
File "/var/lang/lib/python3.6/subprocess.py", line 356, in check_output
**kwargs).stdout
File "/var/lang/lib/python3.6/subprocess.py", line 423, in run
with Popen(*popenargs, **kwargs) as process:
File "/var/lang/lib/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/var/lang/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/var/task/ffmpeg'
Any ideas? I have made ffmpeg and ffprobe executable using chmod +x
Could this be a lambda permissions issue somehow?