1

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:

  1. 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?

Martin Harrison
  • 145
  • 2
  • 11

1 Answers1

1

To use pydub library and many others sound playing libraries you have to install the ffmpeg.exe to the same directory. You can download that executable from here https://ffmpeg.org/

Aleksander Ikleiw
  • 2,549
  • 1
  • 8
  • 26
  • ok, I added ffmpeg executable (ffmpeg-4.3-i686-static from here: https://johnvansickle.com/ffmpeg/) and the ffmpeg error has gone. However, I now have an ffprobe error and adding ffprobe to my project root (with chmod +x) hasn't helped. Any ideas? – Martin Harrison Jul 14 '20 at 22:11
  • Update: so there seems to be a problem with ffprobe and python 3.8? I've moved to 2.7 and now don't get the ffmpeg/ffprobe complaints. However, I have new issues, posted here: https://stackoverflow.com/questions/62905735/no-such-file-error-using-pydub-on-osx-with-pycharm – Martin Harrison Jul 15 '20 at 00:14
  • @MichaelHarrison as the FFmpeg error is gone can you accept my question and upvote it, please? – Aleksander Ikleiw Jul 15 '20 at 07:19
  • @MichaelHarrison can you accept it, please? Click the second button below the downvote, please :) – Aleksander Ikleiw Jul 15 '20 at 09:44