Skip to content

Commit

Permalink
fix(main/python-torchaudio): enable ffmpeg support by creating a torc…
Browse files Browse the repository at this point in the history
…haudio-ffmpeg subpackage

based on code from the audacity-ffmpeg subpackage
https://github.com/termux/termux-packages/tree/fd2e21fcffc72c0f5192bf296cf31a89f448eb39/x11-packages/audacity
  • Loading branch information
robertkirkman authored and TomJo2000 committed Dec 18, 2024
1 parent 5675664 commit 28f6259
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 5 deletions.
82 changes: 77 additions & 5 deletions packages/python-torchaudio/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,82 @@ TERMUX_PKG_DESCRIPTION="Data manipulation and transformation for audio signal pr
TERMUX_PKG_LICENSE="BSD 2-Clause"
TERMUX_PKG_MAINTAINER="@termux"
TERMUX_PKG_VERSION=2.5.0
TERMUX_PKG_SRCURL=git+https://github.com/pytorch/audio
# FFMPEG
TERMUX_PKG_DEPENDS="libc++, python, python-pip, python-torch"
TERMUX_PKG_REVISION=1
# FFmpeg 7 is not yet supported. The subpackage should be removed when FFmpeg 7
# is supported by an upstream release.
# https://github.com/pytorch/audio/issues/3857
_FFMPEG_VERSION=6.1.2
_FFMPEG_SRCURL="https://www.ffmpeg.org/releases/ffmpeg-$_FFMPEG_VERSION.tar.xz"
_FFMPEG_RM_AFTER_INSTALL="
opt/torchaudio/include
opt/torchaudio/lib/pkgconfig
opt/torchaudio/share
"
TERMUX_PKG_SRCURL=(https://github.com/pytorch/audio/archive/refs/tags/v${TERMUX_PKG_VERSION}.tar.gz
$_FFMPEG_SRCURL)
TERMUX_PKG_SHA256=(fca49590d36966879f37cef29dcc83507e97e7cad68035a851734d93066c018e
3b624649725ecdc565c903ca6643d41f33bd49239922e45c9b1442c63dca4e38)
TERMUX_PKG_DEPENDS="libc++, python, python-pip, python-torch, torchaudio-ffmpeg"
TERMUX_PKG_PYTHON_COMMON_DEPS="wheel, setuptools"
TERMUX_PKG_RM_AFTER_INSTALL=$_FFMPEG_RM_AFTER_INSTALL

_ffmpeg_configure_make_install() {
export _FFMPEG_PREFIX=${TERMUX_PREFIX}/opt/torchaudio
LDFLAGS="-Wl,-rpath=${_FFMPEG_PREFIX}/lib ${LDFLAGS}"

local _ARCH
case ${TERMUX_ARCH} in
arm ) _ARCH=armeabi-v7a ;;
i686 ) _ARCH=x86 ;;
* ) _ARCH=$TERMUX_ARCH ;;
esac

mkdir -p _ffmpeg-${_FFMPEG_VERSION}
pushd _ffmpeg-${_FFMPEG_VERSION}
$TERMUX_PKG_SRCDIR/ffmpeg-${_FFMPEG_VERSION}/configure \
--prefix=${_FFMPEG_PREFIX} \
--cc=${CC} \
--pkg-config=false \
--arch=${_ARCH} \
--cross-prefix=llvm- \
--enable-cross-compile \
--target-os=android \
--disable-version3 \
--disable-static \
--enable-shared \
--disable-all \
--disable-autodetect \
--disable-doc \
--enable-avcodec \
--enable-avformat \
--enable-avdevice \
--enable-avfilter \
--disable-asm
make -j ${TERMUX_PKG_MAKE_PROCESSES}
make install
popd
}

_ffmpeg_post_make_install() {
local _FFMPEG_DOCDIR=$TERMUX_PREFIX/share/doc/torchaudio-ffmpeg
mkdir -p ${_FFMPEG_DOCDIR}
ln -sfr ${TERMUX_PREFIX}/share/LICENSES/LGPL-2.1.txt \
${_FFMPEG_DOCDIR}/LICENSE
}

_ffmpeg_post_massage() {
rm -rf lib/pkgconfig
}

termux_step_pre_configure() {
_ffmpeg_configure_make_install
termux_setup_cmake
termux_setup_ninja

export BUILD_VERSION=$TERMUX_PKG_VERSION
# FIXME: The same as torchvision, upstream doesn't support ffmpeg 6.
export USE_FFMPEG=0
export FFMPEG_ROOT="$_FFMPEG_PREFIX"
# use this FFMPEG_ROOT when the system ffmpeg package can work
# export FFMPEG_ROOT="$TERMUX_PREFIX"
export TORCHAUDIO_CMAKE_PREFIX_PATH="$TERMUX_PYTHON_HOME/site-packages/torch;$TERMUX_PREFIX"
export host_alias="$TERMUX_HOST_PLATFORM"
}
Expand All @@ -26,3 +90,11 @@ termux_step_configure() {
termux_step_make_install() {
pip -v install --no-build-isolation --no-deps --prefix "$TERMUX_PREFIX" "$TERMUX_PKG_SRCDIR"
}

termux_step_post_make_install() {
_ffmpeg_post_make_install
}

termux_step_post_massage() {
_ffmpeg_post_massage
}
51 changes: 51 additions & 0 deletions packages/python-torchaudio/ffmpeg-configure.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
+++ ./ffmpeg-6.1.2/configure
@@ -5336,13 +5336,9 @@
striptype=""
;;
android)
- disable symver
enable section_data_rel_ro
add_cflags -fPIE
add_ldexeflags -fPIE -pie
- SLIB_INSTALL_NAME='$(SLIBNAME)'
- SLIB_INSTALL_LINKS=
- SHFLAGS='-shared -Wl,-soname,$(SLIBNAME)'
;;
haiku)
prefix_default="/boot/common"
+++ ./ffmpeg-6.1.2/libavcodec/allcodecs.c
@@ -154,7 +154,6 @@
extern const FFCodec ff_h264_crystalhd_decoder;
extern const FFCodec ff_h264_v4l2m2m_decoder;
extern const FFCodec ff_h264_mediacodec_decoder;
-extern const FFCodec ff_h264_mediacodec_encoder;
extern const FFCodec ff_h264_mmal_decoder;
extern const FFCodec ff_h264_qsv_decoder;
extern const FFCodec ff_h264_rkmpp_decoder;
@@ -850,6 +849,7 @@
extern const FFCodec ff_libopenh264_decoder;
extern const FFCodec ff_h264_amf_encoder;
extern const FFCodec ff_h264_cuvid_decoder;
+extern const FFCodec ff_h264_mediacodec_encoder;
extern const FFCodec ff_h264_mf_encoder;
extern const FFCodec ff_h264_nvenc_encoder;
extern const FFCodec ff_h264_omx_encoder;
+++ ./ffmpeg-6.1.2/libavutil/file_open.c
@@ -119,7 +119,7 @@
#undef free
free(ptr);
#else
- size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
+ size_t len = strlen(prefix) + strlen("@TERMUX_PREFIX@/tmp/") + 7; /* room for "@TERMUX_PREFIX@/tmp/" and "XXXXXX\0" */
*filename = av_malloc(len);
#endif
/* -----common section-----*/
@@ -136,7 +136,7 @@
# endif
fd = open(*filename, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
#else
- snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
+ snprintf(*filename, len, "@TERMUX_PREFIX@/tmp/%sXXXXXX", prefix);
fd = mkstemp(*filename);
#if defined(_WIN32) || defined (__ANDROID__)
if (fd < 0) {
6 changes: 6 additions & 0 deletions packages/python-torchaudio/torchaudio-ffmpeg.subpackage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

TERMUX_SUBPKG_DESCRIPTION="Minimal FFmpeg libraries for python-torchaudio"
TERMUX_SUBPKG_INCLUDE="
opt/torchaudio/lib/
share/doc/torchaudio-ffmpeg/
"

0 comments on commit 28f6259

Please sign in to comment.