我在我的一个视频压缩项目中使用 FFmpeg。在 Android 10 (Google Pixel 3a) 上,它会直接进入 onFailure(String message),并为执行的任何命令发送空消息。
所以我在我的应用程序 gradle 文件中指定了 (api 'com.writingminds:FFmpegAndroid:0.3.2'),
在 list 中指定权限(android.permission.WRITE_EXTERNAL_STORAGE)
我也是这样的:
InitializationCallback initializationCallback = new InitializationCallback();
try {
FFmpeg.getInstance(context).loadBinary(initializationCallback);
} catch (FFmpegNotSupportedException e) {
initializationCallback.onFailure();
initializationCallback.onFinish();
}
初始化很好,这里没有问题。
后来:
void getData(File inputFile) {
//inputFile points to: /storage/emulated/0/Android/data/{package_name}/files/temp_files/temp_1.mp4
String[] cmd = ("-i " + inputFile.getAbsolutePath()).split(" ");
try {
FFmpeg.getInstance(App.instance).execute(cmd, this);
} catch (FFmpegCommandAlreadyRunningException e) {
throw new Error(e);
}
}
@Override
public void onStart() {
//This method is called
}
@Override
public void onSuccess(String message) {
//This method is NOT called
extractAvailableData(message);
}
@Override
public void onProgress(String message) {
//This method is NOT called
extractAvailableData(message);
}
@Override
public void onFailure(String message) {
//This method is called and the message is empty
extractAvailableData(message);
}
@Override
public void onFinish() {
//This method is called
}
如果我这样做:
String command = "-i ***/file1.mp4 -map 0:v -map 0:a -preset ultrafast -s:v 750:350 ***/file2.mp4";
//file2.mp4 is a non existent file at this point
// (***) --> is just a replacement for the full path of the file, just to keep things shorter here.
String[] cmd = command.split(" ");
try {
FFmpeg.getInstance(App.instance).execute(cmd, this);
} catch (FFmpegCommandAlreadyRunningException e) {
throw new Error(e);
}
给出相同的结果,没有视频转换,只是调用 onFailure("Nothing")
即使我这样做:
String[] cmd = {"-version"};
try {
FFmpeg.getInstance(App.instance).execute(cmd, this);
} catch (FFmpegCommandAlreadyRunningException e) {
throw new Error(e);
}
我什么也没得到,根本没有输出。
到目前为止,我只在 Android 10 上遇到过这个问题,它在其他设备上运行良好。
最佳答案
Android 10 的行为发生了变化,删除了应用程序主目录的执行权限。如果您尝试从 App 主目录运行执行文件,您将获得权限被拒绝的异常。
以下是目标 SDK 版本 29 的更改详情:https://developer.android.com/about/versions/10/behavior-changes-10#execute-permission
java.io.IOException: Cannot run program "/data/user/0/<package name>/files/ffmpeg": error=13, Permission denied
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1050)
at nl.bravobit.ffmpeg.ShellCommand.run(ShellCommand.java:15)
at nl.bravobit.ffmpeg.FFcommandExecuteAsyncTask.doInBackground(FFcommandExecuteAsyncTask.java:43)
at nl.bravobit.ffmpeg.FFcommandExecuteAsyncTask.doInBackground(FFcommandExecuteAsyncTask.java:12)
at android.os.AsyncTask$3.call(AsyncTask.java:378)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Caused by: java.io.IOException: error=13, Permission denied
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:133)
at java.lang.ProcessImpl.start(ProcessImpl.java:141)
关于java - FFmpeg 不适用于 android 10,直接进入 onFailure(String message) 并显示空消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58565300/