Compile ffmpeg as stand alone binary

2019-07-06 15:02发布

问题:

I'm trying to compile ffmpeg as a standalone binary (because I want to use it in AWS lambda)

I can get things to work fine on the server where I'm compiling, but if I copy the binary and run it from another server, I get:

./ffmpeg: error while loading shared libraries: libvdpau.so.1: cannot open shared object file: No such file or directory

So it sounds like something didn't made it into the binary. From what I've read, I've to compile ffmpeg with the flags --disable-shared and --enable-static, which I've done:

PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
  --prefix="$HOME/ffmpeg_build" \
  --pkg-config-flags="--static" \
  --extra-cflags="-I$HOME/ffmpeg_build/include" \
  --extra-ldflags="-L$HOME/ffmpeg_build/lib" \
  --bindir="$HOME/bin" \
  --disable-shared \
  --enable-static \
  --enable-gpl \
  --enable-libass \
  --enable-libfreetype \
  --enable-libmp3lame \
  --enable-libvpx \
  --enable-libx264
PATH="$HOME/bin:$PATH" make
make install
make distclean
hash -r

Is there something I'm missing?

回答1:

--enable-static and --disable-shared only affect libav* binaries. It doesn't prevent linker to use necessary shared object files.

For a pure static lib, this is going to be tricky and mess. You have to build every static lib you need, then try to add additional ldflags to configure.

Another alternative is some tools packing those libs/elfs/binaries into a big one. Some of them are listed here: Pack shared libraries into the elf



回答2:

Even though I didn't succeed to compile everything in one binary, I've been able to upload the dependencies to AWS lambda by doing:

copy all dependencies in a folder

I wrote a python script to do that. The script relies on lld to list the dependencies.

#!/usr/bin/env python
import subprocess
import os
from shutil import copyfile

def copyLibraries(bin, destination):
  if not os.path.exists(destination):
    os.makedirs(destination)

  output = subprocess.Popen(["ldd", bin], stdout=subprocess.PIPE).communicate()[0]
  for l in output.split('\n'):
    if len(l.split("=> ")) > 1:
      lib_location = l.split("=> ")[1].split(" ")[0].strip()
      if lib_location != "":
        real_location = os.path.realpath(lib_location)

        lib_name = real_location.split('/')[-1]

        copyfile(real_location, destination + lib_name)

        if os.path.islink(lib_location):
          link_name = lib_location.split('/')[-1]
          if link_name != lib_name:
            os.symlink(real_location, destination + link_name)

copyLibraries("/home/ubuntu/bin/ffmpeg", "/home/ubuntu/libs/")

AWS lambda

  • include the ffmpeg binary as well as the libs folder in the lambda zipped code.
  • in the lambda code, include the corresponding paths. In node.js this can be done as:

.

process.env['PATH'] = process.env['PATH'] +
  ':' +    process.env['LAMBDA_TASK_ROOT'] +
  ':' + process.env['LAMBDA_TASK_ROOT'] + '/bin' +
  ':' + process.env['LAMBDA_TASK_ROOT'] + '/lib';


回答3:

Add --extra-ldexeflags="-static" to get a standalone ffmpeg.