forked from PyAV-Org/PyAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
292 lines (227 loc) · 8.92 KB
/
setup.py
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from __future__ import print_function
from distutils.core import Command
from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext
from subprocess import Popen, PIPE
import ctypes.util
import errno
import os
import re
try:
from Cython.Build import cythonize
except ImportError:
cythonize = None
version = '0.2.2'
proc = Popen(['git', 'describe', '--tags'], stdout=PIPE, stderr=PIPE)
commit, _ = proc.communicate()
commit = commit.strip()
def library_config(name):
"""Get distutils compatible extension extras for the given library.
When availible, this uses ``pkg-config``.
"""
try:
proc = Popen(['pkg-config', '--cflags', '--libs', name], stdout=PIPE, stderr=PIPE)
except OSError:
print('pkg-config is required for building PyAV; aborting!')
exit(1)
raw_config, err = proc.communicate()
if proc.wait():
return
config = {}
for chunk in raw_config.decode('utf8').strip().split():
if chunk.startswith('-I'):
config.setdefault('include_dirs', []).append(chunk[2:])
elif chunk.startswith('-L'):
config.setdefault('library_dirs', []).append(chunk[2:])
elif chunk.startswith('-l'):
config.setdefault('libraries', []).append(chunk[2:])
elif chunk.startswith('-D'):
name = chunk[2:].split('=')[0]
config.setdefault('define_macros', []).append((name, None))
return config
def check_for_func(lib_names, func_name):
"""Define macros if we can find the given function in one of the given libraries."""
for lib_name in lib_names:
lib_path = ctypes.util.find_library(lib_name)
if not lib_path:
continue
# Open the lib. Look in the path returned by find_library, but also all
# the paths returned by pkg-config (since we don't get an absolute path
# on linux).
lib_paths = [lib_path]
lib_paths.extend(
os.path.join(root, os.path.basename(lib_path))
for root in set(extension_extra.get('library_dirs', []))
)
for lib_path in lib_paths:
try:
lib = ctypes.CDLL(lib_path)
break
except OSError:
pass
else:
print('Could not find', lib_name, 'with ctypes; looked in:')
print('\n'.join('\t' + path for path in lib_paths))
continue
return hasattr(lib, func_name)
else:
print('Could not find %r with ctypes.util.find_library' % (lib_names, ))
print('Some libraries can not be found for inspection; aborting!')
exit(2)
# The "extras" to be supplied to every one of our modules.
extension_extra = {
'include_dirs': ['include'],
}
def update_extend(dst, src):
for k, v in src.items():
dst.setdefault(k, []).extend(v)
config_macros = [
("PYAV_VERSION", version),
("PYAV_VERSION_STR", '"%s"' % version),
("PYAV_COMMIT_STR", '"%s"' % (commit or 'unknown-commit'))
]
is_missing_libraries = False
# Get the config for the libraries that we require.
for name in 'libavformat', 'libavcodec', 'libavdevice', 'libavutil', 'libswscale':
config = library_config(name)
if config:
update_extend(extension_extra, config)
config_macros.append(('PYAV_HAVE_' + name.upper(), '1'))
else:
print('Could not find', name, 'with pkg-config.')
is_missing_libraries = True
# Get the config for either swresample OR avresample.
for name in 'libswresample', 'libavresample':
config = library_config(name)
if config:
update_extend(extension_extra, config)
config_macros.append(('PYAV_HAVE_' + name.upper(), '1'))
break
else:
print('Could not find either of libswresample or libavresample with pkg-config.')
is_missing_libraries = True
if is_missing_libraries:
# TODO: Warn Ubuntu 12 users that they can't satisfy requirements with the
# default package sources.
print('Some required libraries are missing, and PyAV cannot be built; aborting!')
exit(1)
# Check for some specific functions.
for libs, func in (
(['avformat', 'avutil', 'avcodec'], 'av_frame_get_best_effort_timestamp'),
(['avformat'], 'avformat_close_input'),
(['avformat'], 'avformat_alloc_output_context2'),
(['avutil'], 'av_calloc'),
):
if check_for_func(libs, func):
config_macros.append(('PYAV_HAVE_' + func.upper(), '1'))
# Normalize the extras.
extension_extra = dict((k, sorted(set(v))) for k, v in extension_extra.items())
# Construct the modules that we find in the "av" directory.
ext_modules = []
for dirname, dirnames, filenames in os.walk('av'):
for filename in filenames:
# We are looing for Cython sources.
if filename.startswith('.') or os.path.splitext(filename)[1] != '.pyx':
continue
pyx_path = os.path.join(dirname, filename)
base = os.path.splitext(pyx_path)[0]
mod_name = base.replace('/', '.')
c_path = os.path.join('src', base + '.c')
# We go with the C sources if Cython is not installed, and fail if
# those also don't exist. We can't `cythonize` here though, since the
# `pyav/include.h` must be generated (by `build_ext`) first.
if not cythonize and not os.path.exists(c_path):
print('Cython is required to build PyAV from raw sources.')
print('Please `pip install Cython`.')
exit(3)
ext_modules.append(Extension(
mod_name,
sources=[c_path if not cythonize else pyx_path],
**extension_extra
))
class CythonizeCommand(Command):
user_options = []
def initialize_options(self):
self.extensions = None
def finalize_options(self):
self.extensions = self.distribution.ext_modules
def run(self):
# Cythonize, if required. We do it individually since we must update
# the existing extension instead of replacing them all.
for i, ext in enumerate(self.extensions[:]):
if any(s.endswith('.pyx') for s in ext.sources):
new_ext = cythonize(ext,
# Keep these in sync with the Makefile cythonize target.
compiler_directives=dict(
c_string_type='str',
c_string_encoding='ascii',
),
build_dir='src',
include_path=ext.include_dirs,
embed=True,
)[0]
ext.sources = new_ext.sources
class BuildExtCommand(build_ext):
def run(self):
# We write a header file containing everything we have discovered by
# inspecting the libraries which exist. This is the main mechanism we
# use to detect differenced between FFmpeg anf Libav.
include_dir = os.path.join(self.build_temp, 'include')
pyav_dir = os.path.join(include_dir, 'pyav')
try:
os.makedirs(pyav_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
header_path = os.path.join(pyav_dir, 'config.h')
print('writing', header_path)
with open(header_path, 'w') as fh:
fh.write('#ifndef PYAV_COMPAT_H\n')
fh.write('#define PYAV_COMPAT_H\n')
for k, v in config_macros:
fh.write('#define %s %s\n' % (k, v))
fh.write('#endif\n')
self.include_dirs = self.include_dirs or []
self.include_dirs.append(include_dir)
self.run_command('cythonize')
return build_ext.run(self)
setup(
name='av',
version=version,
description='Pythonic bindings for FFmpeg/Libav.',
author="Mike Boers",
author_email="[email protected]",
url="https://github.com/mikeboers/PyAV",
packages=find_packages(exclude=['build*', 'tests*', 'examples*']),
zip_safe=False,
ext_modules=ext_modules,
cmdclass={
'build_ext': BuildExtCommand,
'cythonize': CythonizeCommand,
},
test_suite = 'nose.collector',
entry_points = {
'console_scripts': [
'pyav = av.__main__:main',
],
},
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Natural Language :: English',
'Operating System :: MacOS :: MacOS X',
'Operating System :: POSIX',
'Operating System :: Unix',
'Programming Language :: Cython',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 2.4',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Multimedia :: Sound/Audio',
'Topic :: Multimedia :: Sound/Audio :: Conversion',
'Topic :: Multimedia :: Video',
'Topic :: Multimedia :: Video :: Conversion',
],
)