install ffmpeg on amazon ecr linux python

Viewed 38

I'm trying to install ffmpeg on docker for amazon lambda function. Code for Dockerfile is:

FROM public.ecr.aws/lambda/python:3.8

# Copy function code
COPY app.py ${LAMBDA_TASK_ROOT}

# Install the function's dependencies using file requirements.txt
# from your project folder.

COPY requirements.txt  .
RUN  yum install gcc -y
RUN  pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
RUN  yum install -y ffmpeg

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.handler" ]

I am getting an error:

 > [6/6] RUN  yum install -y ffmpeg:
#9 0.538 Loaded plugins: ovl
#9 1.814 No package ffmpeg available.
#9 1.843 Error: Nothing to do
1 Answers

Since the ffmpeg package is not available with yum package manager, I have manually installed ffmpeg and made it part of the container. Here are the steps:

  1. Downloaded the static build from here (the build for the public.ecr.aws/lambda/python:3.8 image is ffmpeg-release-amd64-static.tar.xz

    Here is a bit more info on the topic.

  2. Manually unarchived it in the root folder of my project (where my Dockerfile and app.py files are). I use a CodeCommit repo but this is not mandatory of course.

  3. Added the following line my Dockerfile:

    COPY ffmpeg-5.1.1-amd64-static /usr/local/bin/ffmpeg
    
  4. In the requirements.txt I added the following line (so that the python package managed installs ffmpeg-python package):

    ffmpeg-python
    
  5. And here is how I use it in my python code:

    import ffmpeg
    
    ...
    
    process1 = (ffmpeg
              .input(sourceFilePath)
              .output("pipe:", format="s16le", acodec="pcm_s16le", ac=1, ar="16k", loglevel="quiet")
              .run_async(pipe_stdout=True, cmd=r"/usr/local/bin/ffmpeg/ffmpeg")
          )
    

    Note that in order to work, in the run method (or run_async in my case) I needed to specify the cmd property with the location of the ffmpeg executable.

  6. I was able to build the container and the ffmpeg is working properly for me.

Related