Skip to content

Commit 4e749d1

Browse files
committed
ffmpeg, python3: include binary
1 parent 6494ac1 commit 4e749d1

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

pythonforandroid/recipes/android/src/android/_android.pyx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,43 @@ class AndroidBrowser(object):
280280
import webbrowser
281281
webbrowser.register('android', AndroidBrowser)
282282

283+
# Native android executable support
284+
# Ref:
285+
# https://github.com/agnostic-apollo/Android-Docs/blob/master/site/pages/en/projects/docs/apps/processes/app-data-file-execute-restrictions.md#apk-native-library
286+
287+
import os
288+
import sys
289+
from os.path import join, isdir, islink, isfile
290+
291+
_EXECUTABLES = {
292+
'python': 'libpythonbin.so',
293+
'python3': 'libpythonbin.so',
294+
'ffmpeg': 'libffmpegbin.so',
295+
}
296+
297+
app_info = mActivity.getApplicationInfo()
298+
native_lib_dir = app_info.nativeLibraryDir
299+
files_dir = mActivity.getFilesDir().getAbsolutePath()
300+
bin_dir = join(files_dir, 'app', '.bin')
301+
302+
if not isdir(bin_dir):
303+
os.makedirs(bin_dir)
304+
305+
for exe, exe_lib in _EXECUTABLES.items():
306+
_exe = join(bin_dir, exe)
307+
_exe_lib = join(native_lib_dir, exe_lib)
308+
if isfile(_exe_lib) and not islink(_exe):
309+
try:
310+
os.symlink(_exe_lib, _exe)
311+
print(f'Symlink executable: {exe_lib} -> {exe}')
312+
except Exception as e:
313+
print(f'Symlink failed for {exe_lib} -> {exe}')
314+
print(e)
315+
316+
os.environ['LD_LIBRARY_PATH'] = native_lib_dir
317+
os.environ['PATH'] = bin_dir
318+
os.environ['PYTHONPATH'] = ":".join(sys.path)
319+
sys.executable = join(bin_dir, 'python')
283320

284321
def start_service(title="Background Service",
285322
description="", arg="",

pythonforandroid/recipes/ffmpeg/__init__.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
from pythonforandroid.toolchain import Recipe, current_directory, shprint
22
from os.path import exists, join, realpath
33
import sh
4+
from multiprocessing import cpu_count
45

56

67
class FFMpegRecipe(Recipe):
78
version = 'n6.1.2'
89
# Moved to github.com instead of ffmpeg.org to improve download speed
910
url = 'https://github.com/FFmpeg/FFmpeg/archive/{version}.zip'
10-
depends = ['sdl2'] # Need this to build correct recipe order
11+
depends = [('sdl2', 'sdl3')] # Need this to build correct recipe order
1112
opts_depends = ['openssl', 'ffpyplayer_codecs', 'av_codecs']
1213
patches = ['patches/configure.patch']
14+
_libs = [
15+
"libavcodec.so",
16+
"libavfilter.so",
17+
"libavutil.so",
18+
"libswscale.so",
19+
"libavdevice.so",
20+
"libavformat.so",
21+
"libswresample.so",
22+
"libffmpegbin.so",
23+
]
24+
built_libraries = dict.fromkeys(_libs, "./lib")
1325

1426
def should_build(self, arch):
1527
build_dir = self.get_build_dir(arch.arch)
@@ -98,9 +110,8 @@ def build_arch(self, arch):
98110
'--disable-symver',
99111
]
100112

101-
# disable binaries / doc
113+
# disable doc
102114
flags += [
103-
'--disable-programs',
104115
'--disable-doc',
105116
]
106117

@@ -148,11 +159,9 @@ def build_arch(self, arch):
148159

149160
configure = sh.Command('./configure')
150161
shprint(configure, *flags, _env=env)
151-
shprint(sh.make, '-j4', _env=env)
162+
shprint(sh.make, '-j', f"{cpu_count()}", _env=env)
152163
shprint(sh.make, 'install', _env=env)
153-
# copy libs:
154-
sh.cp('-a', sh.glob('./lib/lib*.so'),
155-
self.ctx.get_libs_dir(arch.arch))
164+
shprint(sh.cp, "ffmpeg", "./lib/libffmpegbin.so")
156165

157166

158167
recipe = FFMpegRecipe()

pythonforandroid/recipes/python3/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ class Python3Recipe(TargetPythonRecipe):
183183
disable_gil = False
184184
'''python3.13 experimental free-threading build'''
185185

186+
built_libraries = {"libpythonbin.so" : "./android-build/"}
187+
186188
def __init__(self, *args, **kwargs):
187189
self._ctx = None
188190
super().__init__(*args, **kwargs)
@@ -369,6 +371,9 @@ def build_arch(self, arch):
369371
# better way, although this is probably acceptable
370372
sh.cp('pyconfig.h', join(recipe_build_dir, 'Include'))
371373

374+
# rename executable
375+
sh.cp('python', 'libpythonbin.so')
376+
372377
def compile_python_files(self, dir):
373378
'''
374379
Compile the python files (recursively) for the python files inside

0 commit comments

Comments
 (0)