如何减小 Android 中使用 FFMPEG 的默认摄像头捕获的视频大小?

发布于 2024-12-08 22:12:25 字数 274 浏览 0 评论 0原文

我正在尝试减小 Android 中默认摄像头(它正在生成高分辨率视频)捕获的视频大小。 FFMPEG 是否具有以给定分辨率对视频进行编码的属性?我尝试谷歌,但所有示例都使用 FFMPEG 的命令行模式。

我的问题是:

  1. 我们可以在Android中使用ffmpeg命令行吗?
  2. 如果不是那么我们将如何实现它?
  3. 我们可以在Android中直接使用ffmpeg录制视频吗?
  4. 对此还有其他解决方案吗?

I am trying to reduce the video size captured by the default camera (it's generating high resolution video) in Android. Does FFMPEG have a property to encode a video with given resolution? I tried to Google, but all examples are using command line mode for FFMPEG.

My questions are:

  1. Can we use ffmpeg command line in Android?
  2. If not then how we will achieve it?
  3. Can we able record a video directly using ffmpeg in Android?
  4. Is there any other solution for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向地狱狂奔 2024-12-15 22:12:25

可以为 Android 编译 ffmpeg,也可以从命令行运行 ffmpeg。除非您需要比命令行提供的更高级的用法,否则无需深入研究本机代码和 jni 调用。

作为参考,这是我运行的编译 ffmpeg 的 shell 脚本(在 Ubuntu 下运行,它使事情比 Windows 容易得多)

#!/bin/bash


ANDROID_API=android-3
export ANDROID_NDK=${HOME}/android-ndk
export ANDROID_SDK=${HOME}/android-sdk
SYSROOT=$ANDROID_NDK/platforms/$ANDROID_API/arch-arm
ANDROID_BIN=$ANDROID_NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/*-x86/bin/
CROSS_COMPILE=${ANDROID_BIN}/arm-linux-androideabi-
export PATH=$PATH:$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools

export ARM_ROOT=${HOME}android-ndk
export ARM_INC=$ARM_ROOT/platforms/android-5/arch-arm/usr/include
export ARM_LIB=$ARM_ROOT/platforms/android-5/arch-arm/usr/lib
export LIB_INC=${HOME}/include
export LIB_LIB=${HOME}/lib
CFLAGS=" -I$ARM_INC -fPIC -DANDROID -fpic -mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__  -Wno-psabi -march=armv5te -mtune=xscale -msoft-float -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -DANDROID  -Wa,--noexecstack -MMD -MP "
LDFLAGS=" -nostdlib -Bdynamic  -Wl,--no-undefined -Wl,-z,noexecstack  -Wl,-z,nocopyreloc -Wl,-soname,/system/lib/libz.so -Wl,-rpath-link=$ARM_LIB,-dynamic-linker=/system/bin/linker -L$ARM_LIB -nostdlib $ARM_LIB/crtbegin_dynamic.o $ARM_LIB/crtend_android.o -lc -lm -ldl -lgcc "

FLAGS="--target-os=linux --enable-cross-compile --cross-prefix=$CROSS_COMPILE --arch=arm --prefix=$HOME --disable-shared --enable-static --extra-libs=-static --extra-cflags=--static --enable-small --disable-asm --disable-yasm --disable-amd3dnow --disable-amd3dnowext --disable-mmx --disable-mmx2 --disable-sse --disable-ssse3 --disable-indevs"
export CFLAGS=$EXTRA_CFLAGS
export LDFLAGS=$EXTRA_LDFLAGS
./configure $FLAGS --extra-cflags="$CFLAGS" --extra-ldflags="$LDFLAGS" \
--cc="${CROSS_COMPILE}gcc --sysroot=${SYSROOT}" --extra-ldflags="$LDFLAGS" \
--cxx="${CROSS_COMPILE}g++ --sysroot=${SYSROOT}" \
--nm="${CROSS_COMPILE}nm" \
--ar="${CROSS_COMPILE}ar"
make clean
make -j4 || exit 1
make install || exit 1

至于运行 ffmpeg,首先需要将 ffmpeg 复制到应用程序的文件目录中,使用 getRuntime chmod 755 它。如下所示,然后运行 ​​ffmpeg

Process p = Runtime.getRuntime().exec("/data/data/yourpackagename/files/ffmpeg -i in.mp4 out.mp4")

: 弄清楚。我有一个关于该主题的 stackoverflow 问题: 使用 ffmpeg 实时解码 Android 硬件编码的 H264 摄像头源

Compiling ffmpeg for android is possible, as well as running ffmpeg from command line. There's no need to delve into native code and jni calls unless you need more advanced usage than what the command line provides.

For reference, this is the shell script I run to compile ffmpeg (run under Ubuntu, it makes things a lot easier than windows)

#!/bin/bash


ANDROID_API=android-3
export ANDROID_NDK=${HOME}/android-ndk
export ANDROID_SDK=${HOME}/android-sdk
SYSROOT=$ANDROID_NDK/platforms/$ANDROID_API/arch-arm
ANDROID_BIN=$ANDROID_NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/*-x86/bin/
CROSS_COMPILE=${ANDROID_BIN}/arm-linux-androideabi-
export PATH=$PATH:$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools

export ARM_ROOT=${HOME}android-ndk
export ARM_INC=$ARM_ROOT/platforms/android-5/arch-arm/usr/include
export ARM_LIB=$ARM_ROOT/platforms/android-5/arch-arm/usr/lib
export LIB_INC=${HOME}/include
export LIB_LIB=${HOME}/lib
CFLAGS=" -I$ARM_INC -fPIC -DANDROID -fpic -mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__  -Wno-psabi -march=armv5te -mtune=xscale -msoft-float -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -DANDROID  -Wa,--noexecstack -MMD -MP "
LDFLAGS=" -nostdlib -Bdynamic  -Wl,--no-undefined -Wl,-z,noexecstack  -Wl,-z,nocopyreloc -Wl,-soname,/system/lib/libz.so -Wl,-rpath-link=$ARM_LIB,-dynamic-linker=/system/bin/linker -L$ARM_LIB -nostdlib $ARM_LIB/crtbegin_dynamic.o $ARM_LIB/crtend_android.o -lc -lm -ldl -lgcc "

FLAGS="--target-os=linux --enable-cross-compile --cross-prefix=$CROSS_COMPILE --arch=arm --prefix=$HOME --disable-shared --enable-static --extra-libs=-static --extra-cflags=--static --enable-small --disable-asm --disable-yasm --disable-amd3dnow --disable-amd3dnowext --disable-mmx --disable-mmx2 --disable-sse --disable-ssse3 --disable-indevs"
export CFLAGS=$EXTRA_CFLAGS
export LDFLAGS=$EXTRA_LDFLAGS
./configure $FLAGS --extra-cflags="$CFLAGS" --extra-ldflags="$LDFLAGS" \
--cc="${CROSS_COMPILE}gcc --sysroot=${SYSROOT}" --extra-ldflags="$LDFLAGS" \
--cxx="${CROSS_COMPILE}g++ --sysroot=${SYSROOT}" \
--nm="${CROSS_COMPILE}nm" \
--ar="${CROSS_COMPILE}ar"
make clean
make -j4 || exit 1
make install || exit 1

As for running ffmpeg, first you need to copy ffmpeg into your application's files directory, chmod 755 it using getRuntime.exec() as shown below, then run ffmpeg with the following line:

Process p = Runtime.getRuntime().exec("/data/data/yourpackagename/files/ffmpeg -i in.mp4 out.mp4")

Now, getting the camera's input to ffmpeg in a format it can understand is the tough bit, which I'm still trying to figure out. I've got a stackoverflow question going on the topic: Decode android's hardware encoded H264 camera feed using ffmpeg in real time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文