gpt4 book ai didi

Android - ffmpeg 最佳方法

转载 作者:行者123 更新时间:2023-11-29 17:52:28 25 4
gpt4 key购买 nike

我正在尝试为 android 构建 ffmpeg。我想用它实现两件事。1.旋转视频2.加入两个或多个视频。

在我的应用程序中有两种方法可以使用 ffmpeg。1. 使 ffmpeg 可执行,将其复制到/data/package/并执行 ffmpeg 命令。2.用ndk构建ffmpeg库.so文件,编写jni代码等。

哪种方法最符合我的需要?我可以有一些遵循这些方法的代码 fragment 吗?

最佳答案

你可以通过两种方式实现,我会用第一种:

将您的 ffmpeg 文件放入您的 raw 文件夹中。

您需要使用命令使用 ffmpeg 可执行文件,但您需要将文件放入文件系统文件夹并更改文件的权限,因此请使用此代码:

public static void installBinaryFromRaw(Context context, int resId, File file) {
final InputStream rawStream = context.getResources().openRawResource(resId);
final OutputStream binStream = getFileOutputStream(file);

if (rawStream != null && binStream != null) {
pipeStreams(rawStream, binStream);

try {
rawStream.close();
binStream.close();
} catch (IOException e) {
Log.e(TAG, "Failed to close streams!", e);
}

doChmod(file, 777);
}
}
public static OutputStream getFileOutputStream(File file) {
try {
return new FileOutputStream(file);
} catch (FileNotFoundException e) {
Log.e(TAG, "File not found attempting to stream file.", e);
}
return null;
}

public static void pipeStreams(InputStream is, OutputStream os) {
byte[] buffer = new byte[IO_BUFFER_SIZE];
int count;
try {
while ((count = is.read(buffer)) > 0) {
os.write(buffer, 0, count);
}
} catch (IOException e) {
Log.e(TAG, "Error writing stream.", e);
}
}
public static void doChmod(File file, int chmodValue) {
final StringBuilder sb = new StringBuilder();
sb.append("chmod");
sb.append(' ');
sb.append(chmodValue);
sb.append(' ');
sb.append(file.getAbsolutePath());

try {
Runtime.getRuntime().exec(sb.toString());
} catch (IOException e) {
Log.e(TAG, "Error performing chmod", e);
}
}

调用这个方法:

private void installFfmpeg() {
File ffmpegFile = new File(getCacheDir(), "ffmpeg");
String mFfmpegInstallPath = ffmpegFile.toString();
Log.d(TAG, "ffmpeg install path: " + mFfmpegInstallPath);
if (!ffmpegFile.exists()) {
try {
ffmpegFile.createNewFile();
} catch (IOException e) {
Log.e(TAG, "Failed to create new file!", e);
}
Utils.installBinaryFromRaw(this, R.raw.ffmpeg, ffmpegFile);
}else{
Log.d(TAG, "It was installed");
}

ffmpegFile.setExecutable(true);
}

然后,您将准备好供命令使用的 ffmpeg 文件。 (这种方式对我有用,但有些人说它不起作用,我不知道为什么,希望不是你的情况)。然后,我们使用带有此代码的 ffmpeg:

String command = "data/data/YOUR_PACKAGE/cache/ffmpeg" + THE_REST_OF_YOUR_COMMAND;
try {
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
Log.d(TAG, "Process finished");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

正如我所说,你必须通过命令使用ffmpeg文件,所以你应该在互联网上搜索并选择你想要使用的命令,然后将它添加到命令字符串中。如果命令失败,您不会收到任何日志提醒,因此您应该使用终端仿真器尝试您的命令并确保它有效。如果它不起作用,您将看不到任何结果。

希望有用!!

关于Android - ffmpeg 最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21996070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com