Skip to content

Commit 881e007

Browse files
committed
let it run on Android Q
see bravobit#126
1 parent 37f8ee3 commit 881e007

File tree

9 files changed

+12
-143
lines changed

9 files changed

+12
-143
lines changed

android-ffmpeg/build.gradle

+2-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ ext {
2222
}
2323

2424
android {
25-
compileSdkVersion 28
25+
compileSdkVersion 29
2626

2727
defaultConfig {
2828
minSdkVersion 16
29-
targetSdkVersion 28
29+
targetSdkVersion 29
3030
versionCode 17
3131
versionName "1.1.7"
3232
}
@@ -46,8 +46,6 @@ android {
4646
}
4747

4848
dependencies {
49-
implementation fileTree(dir: 'libs', include: ['*.jar'])
50-
5149
implementation 'androidx.appcompat:appcompat:1.0.2'
5250
}
5351

@@ -98,7 +96,6 @@ version = libraryVersion
9896

9997
task sourcesJar(type: Jar) {
10098
from android.sourceSets.main.java.srcDirs
101-
classifier = 'sources'
10299
}
103100

104101
task javadoc(type: Javadoc) {

android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FFmpeg.java

+2-53
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package nl.bravobit.ffmpeg;
22

33
import android.content.Context;
4-
import android.content.SharedPreferences;
54
import android.os.AsyncTask;
65

76
import java.io.File;
8-
import java.io.IOException;
9-
import java.io.InputStream;
107
import java.lang.reflect.Array;
118
import java.util.Map;
129

1310
public class FFmpeg implements FFbinaryInterface {
14-
private static final int VERSION = 17; // up this version when you add a new ffmpeg build
15-
private static final String KEY_PREF_VERSION = "ffmpeg_version";
1611

1712
private final FFbinaryContextProvider context;
1813

@@ -50,57 +45,11 @@ public boolean isSupported() {
5045
// get ffmpeg file
5146
File ffmpeg = FileUtils.getFFmpeg(context.provide());
5247

53-
SharedPreferences settings = context.provide().getSharedPreferences("ffmpeg_prefs", Context.MODE_PRIVATE);
54-
int version = settings.getInt(KEY_PREF_VERSION, 0);
55-
56-
// check if ffmpeg file exists
57-
if (!ffmpeg.exists() || version < VERSION) {
58-
String prefix = "arm/";
59-
if (cpuArch == CpuArch.x86) {
60-
prefix = "x86/";
61-
}
62-
Log.d("file does not exist, creating it...");
63-
64-
try {
65-
InputStream inputStream = context.provide().getAssets().open(prefix + "ffmpeg");
66-
if (!FileUtils.inputStreamToFile(inputStream, ffmpeg)) {
67-
return false;
68-
}
69-
70-
Log.d("successfully wrote ffmpeg file!");
71-
72-
settings.edit().putInt(KEY_PREF_VERSION, VERSION).apply();
73-
} catch (IOException e) {
74-
Log.e("error while opening assets", e);
75-
return false;
76-
}
77-
}
78-
7948
// check if ffmpeg can be executed
8049
if (!ffmpeg.canExecute()) {
8150
// try to make executable
82-
try {
83-
try {
84-
Runtime.getRuntime().exec("chmod -R 777 " + ffmpeg.getAbsolutePath()).waitFor();
85-
} catch (InterruptedException e) {
86-
Log.e("interrupted exception", e);
87-
return false;
88-
} catch (IOException e) {
89-
Log.e("io exception", e);
90-
return false;
91-
}
92-
93-
if (!ffmpeg.canExecute()) {
94-
// our last hope!
95-
if (!ffmpeg.setExecutable(true)) {
96-
Log.e("unable to make executable");
97-
return false;
98-
}
99-
}
100-
} catch (SecurityException e) {
101-
Log.e("security exception", e);
102-
return false;
103-
}
51+
Log.e("ffmpeg cannot execute");
52+
return false;
10453
}
10554

10655
Log.d("ffmpeg is ready!");

android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FFprobe.java

+2-54
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package nl.bravobit.ffmpeg;
22

33
import android.content.Context;
4-
import android.content.SharedPreferences;
54
import android.os.AsyncTask;
65

76
import java.io.File;
8-
import java.io.IOException;
9-
import java.io.InputStream;
107
import java.lang.reflect.Array;
118
import java.util.Map;
129

1310
public class FFprobe implements FFbinaryInterface {
14-
private static final int VERSION = 17; // up this version when you add a new ffprobe build
15-
private static final String KEY_PREF_VERSION = "ffprobe_version";
1611

1712
private final FFbinaryContextProvider context;
1813

@@ -50,57 +45,10 @@ public boolean isSupported() {
5045
// get ffprobe file
5146
File ffprobe = FileUtils.getFFprobe(context.provide());
5247

53-
SharedPreferences settings = context.provide().getSharedPreferences("ffmpeg_prefs", Context.MODE_PRIVATE);
54-
int version = settings.getInt(KEY_PREF_VERSION, 0);
55-
56-
// check if ffprobe file exists
57-
if (!ffprobe.exists() || version < VERSION) {
58-
String prefix = "arm/";
59-
if (cpuArch == CpuArch.x86) {
60-
prefix = "x86/";
61-
}
62-
Log.d("file does not exist, creating it...");
63-
64-
try {
65-
InputStream inputStream = context.provide().getAssets().open(prefix + "ffprobe");
66-
if (!FileUtils.inputStreamToFile(inputStream, ffprobe)) {
67-
return false;
68-
}
69-
70-
Log.d("successfully wrote ffprobe file!");
71-
72-
settings.edit().putInt(KEY_PREF_VERSION, VERSION).apply();
73-
} catch (IOException e) {
74-
Log.e("error while opening assets", e);
75-
return false;
76-
}
77-
}
78-
7948
// check if ffprobe can be executed
8049
if (!ffprobe.canExecute()) {
81-
// try to make executable
82-
try {
83-
try {
84-
Runtime.getRuntime().exec("chmod -R 777 " + ffprobe.getAbsolutePath()).waitFor();
85-
} catch (InterruptedException e) {
86-
Log.e("interrupted exception", e);
87-
return false;
88-
} catch (IOException e) {
89-
Log.e("io exception", e);
90-
return false;
91-
}
92-
93-
if (!ffprobe.canExecute()) {
94-
// our last hope!
95-
if (!ffprobe.setExecutable(true)) {
96-
Log.e("unable to make executable");
97-
return false;
98-
}
99-
}
100-
} catch (SecurityException e) {
101-
Log.e("security exception", e);
102-
return false;
103-
}
50+
Log.e("ffprobe cannot execute");
51+
return false;
10452
}
10553

10654
Log.d("ffprobe is ready!");

android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FileUtils.java

+4-28
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,19 @@
22

33
import android.content.Context;
44

5-
import java.io.BufferedInputStream;
65
import java.io.File;
7-
import java.io.FileOutputStream;
8-
import java.io.IOException;
9-
import java.io.InputStream;
10-
import java.io.OutputStream;
116

127
class FileUtils {
13-
private static final String FFMPEG_FILE_NAME = "ffmpeg";
14-
private static final String FFPROBE_FILE_NAME = "ffprobe";
8+
private static final String FFMPEG_FILE_NAME = "lib..ffmpeg..so";
9+
private static final String FFPROBE_FILE_NAME = "lib..ffprobe.so";
1510

1611
static File getFFmpeg(Context context) {
17-
File folder = context.getFilesDir();
12+
File folder = new File(context.getApplicationInfo().nativeLibraryDir);
1813
return new File(folder, FFMPEG_FILE_NAME);
1914
}
2015

2116
static File getFFprobe(Context context) {
22-
File folder = context.getFilesDir();
17+
File folder = new File(context.getApplicationInfo().nativeLibraryDir);
2318
return new File(folder, FFPROBE_FILE_NAME);
2419
}
25-
26-
static boolean inputStreamToFile(InputStream stream, File file) {
27-
try {
28-
InputStream input = new BufferedInputStream(stream);
29-
OutputStream output = new FileOutputStream(file);
30-
byte[] buffer = new byte[1024];
31-
int bytesRead;
32-
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
33-
output.write(buffer, 0, bytesRead);
34-
}
35-
output.flush();
36-
output.close();
37-
input.close();
38-
return true;
39-
} catch (IOException e) {
40-
Log.e("error while writing ff binary file", e);
41-
}
42-
return false;
43-
}
4420
}

sample/build.gradle

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 28
4+
compileSdkVersion 29
55

66
defaultConfig {
77
applicationId "nl.bravobit.ffmpeg.example"
88
minSdkVersion 16
9-
targetSdkVersion 28
9+
targetSdkVersion 29
1010
versionCode 1
1111
versionName "1.0.1"
1212
}
@@ -25,7 +25,6 @@ android {
2525
}
2626

2727
dependencies {
28-
implementation fileTree(dir: 'libs', include: ['*.jar'])
2928

3029
// Android libraries
3130
implementation 'androidx.appcompat:appcompat:1.0.2'

0 commit comments

Comments
 (0)