1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
import argparse
import os
import pathlib
import platform
import re
import shlex
import subprocess
import sys
from Cython.Build import cythonize
from Cython.Compiler.AutoDocTransforms import EmbedSignature
from setuptools import Extension, find_packages, setup
FFMPEG_LIBRARIES = [
"avformat",
"avcodec",
"avdevice",
"avutil",
"avfilter",
"swscale",
"swresample",
]
# Monkey-patch Cython to not overwrite embedded signatures.
old_embed_signature = EmbedSignature._embed_signature
def new_embed_signature(self, sig, doc):
# Strip any `self` parameters from the front.
sig = re.sub(r"\(self(,\s+)?", "(", sig)
# If they both start with the same signature; skip it.
if sig and doc:
new_name = sig.split("(")[0].strip()
old_name = doc.split("(")[0].strip()
if new_name == old_name:
return doc
if new_name.endswith("." + old_name):
return doc
return old_embed_signature(self, sig, doc)
EmbedSignature._embed_signature = new_embed_signature
def get_config_from_directory(ffmpeg_dir):
"""
Get distutils-compatible extension arguments for a specific directory.
"""
if not os.path.isdir(ffmpeg_dir):
print("The specified ffmpeg directory does not exist")
exit(1)
include_dir = os.path.join(FFMPEG_DIR, "include")
library_dir = os.path.join(FFMPEG_DIR, "lib")
if not os.path.exists(include_dir):
include_dir = FFMPEG_DIR
if not os.path.exists(library_dir):
library_dir = FFMPEG_DIR
return {
"include_dirs": [include_dir],
"libraries": FFMPEG_LIBRARIES,
"library_dirs": [library_dir],
}
def get_config_from_pkg_config():
"""
Get distutils-compatible extension arguments using pkg-config.
"""
pkg_config = os.environ.get("PKG_CONFIG", "pkg-config")
try:
raw_cflags = subprocess.check_output(
[pkg_config, "--cflags", "--libs"]
+ ["lib" + name for name in FFMPEG_LIBRARIES]
)
except FileNotFoundError:
print(f"{pkg_config} is required for building PyAV")
exit(1)
except subprocess.CalledProcessError:
print(f"{pkg_config} could not find libraries {FFMPEG_LIBRARIES}")
exit(1)
known, unknown = parse_cflags(raw_cflags.decode("utf-8"))
if unknown:
print("pkg-config returned flags we don't understand: {}".format(unknown))
if "-pthread" in unknown:
print("Building PyAV against static FFmpeg libraries is not supported.")
exit(1)
return known
def parse_cflags(raw_flags):
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("-I", dest="include_dirs", action="append")
parser.add_argument("-L", dest="library_dirs", action="append")
parser.add_argument("-l", dest="libraries", action="append")
parser.add_argument("-D", dest="define_macros", action="append")
parser.add_argument("-R", dest="runtime_library_dirs", action="append")
raw_args = shlex.split(raw_flags.strip())
args, unknown = parser.parse_known_args(raw_args)
config = {k: v or [] for k, v in args.__dict__.items()}
for i, x in enumerate(config["define_macros"]):
parts = x.split("=", 1)
value = x[1] or None if len(x) == 2 else None
config["define_macros"][i] = (parts[0], value)
return config, " ".join(shlex.quote(x) for x in unknown)
# Parse command-line arguments.
FFMPEG_DIR = None
for i, arg in enumerate(sys.argv):
if arg.startswith("--ffmpeg-dir="):
FFMPEG_DIR = arg.split("=")[1]
del sys.argv[i]
# Locate ffmpeg libraries and headers.
if FFMPEG_DIR is not None:
extension_extra = get_config_from_directory(FFMPEG_DIR)
elif platform.system() != "Windows":
extension_extra = get_config_from_pkg_config()
else:
extension_extra = {
"include_dirs": [],
"libraries": FFMPEG_LIBRARIES,
"library_dirs": [],
}
IMPORT_NAME = "av"
loudnorm_extension = Extension(
f"{IMPORT_NAME}.filter.loudnorm",
sources=[
f"{IMPORT_NAME}/filter/loudnorm.py",
f"{IMPORT_NAME}/filter/loudnorm_impl.c",
],
include_dirs=[f"{IMPORT_NAME}/filter"] + extension_extra["include_dirs"],
libraries=extension_extra["libraries"],
library_dirs=extension_extra["library_dirs"],
)
compiler_directives = {
"c_string_type": "str",
"c_string_encoding": "ascii",
"embedsignature": True,
"binding": False,
"language_level": 3,
"freethreading_compatible": True,
}
# Add the cythonized loudnorm extension to ext_modules
ext_modules = cythonize(
loudnorm_extension,
compiler_directives=compiler_directives,
build_dir="src",
include_path=["include"],
)
for dirname, dirnames, filenames in os.walk(IMPORT_NAME):
for filename in filenames:
# We are looking for Cython sources.
if filename.startswith("."):
continue
if filename in {"__init__.py", "__main__.py", "about.py", "datasets.py"}:
continue
if os.path.splitext(filename)[1] not in {".pyx", ".py"}:
continue
pyx_path = os.path.join(dirname, filename)
base = os.path.splitext(pyx_path)[0]
# Need to be a little careful because Windows will accept / or \
# (where os.sep will be \ on Windows).
mod_name = base.replace("/", ".").replace(os.sep, ".")
# Cythonize the module.
ext_modules += cythonize(
Extension(
mod_name,
include_dirs=extension_extra["include_dirs"],
libraries=extension_extra["libraries"],
library_dirs=extension_extra["library_dirs"],
sources=[pyx_path],
),
compiler_directives=compiler_directives,
build_dir="src",
include_path=["include"],
)
package_folders = pathlib.Path(IMPORT_NAME).glob("**/")
package_data = {
".".join(pckg.parts): ["*.pxd", "*.pyi", "*.typed"] for pckg in package_folders
}
setup(
packages=find_packages(include=[f"{IMPORT_NAME}*"]),
package_data=package_data,
ext_modules=ext_modules,
)
|