Skip to content

Commit

Permalink
let it run on Android Q
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcohn committed Aug 11, 2019
1 parent 4332b54 commit 777c9b4
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 143 deletions.
7 changes: 2 additions & 5 deletions android-ffmpeg/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ ext {
}

android {
compileSdkVersion 28
compileSdkVersion 29

defaultConfig {
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode 17
versionName "1.1.7"
}
Expand All @@ -46,8 +46,6 @@ android {
}

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

implementation 'androidx.appcompat:appcompat:1.0.2'
}

Expand Down Expand Up @@ -98,7 +96,6 @@ version = libraryVersion

task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}

task javadoc(type: Javadoc) {
Expand Down
55 changes: 2 additions & 53 deletions android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FFmpeg.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package nl.bravobit.ffmpeg;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.Map;

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

private final FFbinaryContextProvider context;

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

SharedPreferences settings = context.provide().getSharedPreferences("ffmpeg_prefs", Context.MODE_PRIVATE);
int version = settings.getInt(KEY_PREF_VERSION, 0);

// check if ffmpeg file exists
if (!ffmpeg.exists() || version < VERSION) {
String prefix = "arm/";
if (cpuArch == CpuArch.x86) {
prefix = "x86/";
}
Log.d("file does not exist, creating it...");

try {
InputStream inputStream = context.provide().getAssets().open(prefix + "ffmpeg");
if (!FileUtils.inputStreamToFile(inputStream, ffmpeg)) {
return false;
}

Log.d("successfully wrote ffmpeg file!");

settings.edit().putInt(KEY_PREF_VERSION, VERSION).apply();
} catch (IOException e) {
Log.e("error while opening assets", e);
return false;
}
}

// check if ffmpeg can be executed
if (!ffmpeg.canExecute()) {
// try to make executable
try {
try {
Runtime.getRuntime().exec("chmod -R 777 " + ffmpeg.getAbsolutePath()).waitFor();
} catch (InterruptedException e) {
Log.e("interrupted exception", e);
return false;
} catch (IOException e) {
Log.e("io exception", e);
return false;
}

if (!ffmpeg.canExecute()) {
// our last hope!
if (!ffmpeg.setExecutable(true)) {
Log.e("unable to make executable");
return false;
}
}
} catch (SecurityException e) {
Log.e("security exception", e);
return false;
}
Log.e("ffmpeg cannot execute");
return false;
}

Log.d("ffmpeg is ready!");
Expand Down
56 changes: 2 additions & 54 deletions android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FFprobe.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package nl.bravobit.ffmpeg;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.Map;

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

private final FFbinaryContextProvider context;

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

SharedPreferences settings = context.provide().getSharedPreferences("ffmpeg_prefs", Context.MODE_PRIVATE);
int version = settings.getInt(KEY_PREF_VERSION, 0);

// check if ffprobe file exists
if (!ffprobe.exists() || version < VERSION) {
String prefix = "arm/";
if (cpuArch == CpuArch.x86) {
prefix = "x86/";
}
Log.d("file does not exist, creating it...");

try {
InputStream inputStream = context.provide().getAssets().open(prefix + "ffprobe");
if (!FileUtils.inputStreamToFile(inputStream, ffprobe)) {
return false;
}

Log.d("successfully wrote ffprobe file!");

settings.edit().putInt(KEY_PREF_VERSION, VERSION).apply();
} catch (IOException e) {
Log.e("error while opening assets", e);
return false;
}
}

// check if ffprobe can be executed
if (!ffprobe.canExecute()) {
// try to make executable
try {
try {
Runtime.getRuntime().exec("chmod -R 777 " + ffprobe.getAbsolutePath()).waitFor();
} catch (InterruptedException e) {
Log.e("interrupted exception", e);
return false;
} catch (IOException e) {
Log.e("io exception", e);
return false;
}

if (!ffprobe.canExecute()) {
// our last hope!
if (!ffprobe.setExecutable(true)) {
Log.e("unable to make executable");
return false;
}
}
} catch (SecurityException e) {
Log.e("security exception", e);
return false;
}
Log.e("ffprobe cannot execute");
return false;
}

Log.d("ffprobe is ready!");
Expand Down
32 changes: 4 additions & 28 deletions android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,19 @@

import android.content.Context;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

class FileUtils {
private static final String FFMPEG_FILE_NAME = "ffmpeg";
private static final String FFPROBE_FILE_NAME = "ffprobe";
private static final String FFMPEG_FILE_NAME = "lib..ffmpeg..so";
private static final String FFPROBE_FILE_NAME = "lib..ffprobe.so";

static File getFFmpeg(Context context) {
File folder = context.getFilesDir();
File folder = new File(context.getApplicationInfo().nativeLibraryDir);
return new File(folder, FFMPEG_FILE_NAME);
}

static File getFFprobe(Context context) {
File folder = context.getFilesDir();
File folder = new File(context.getApplicationInfo().nativeLibraryDir);
return new File(folder, FFPROBE_FILE_NAME);
}

static boolean inputStreamToFile(InputStream stream, File file) {
try {
InputStream input = new BufferedInputStream(stream);
OutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
output.flush();
output.close();
input.close();
return true;
} catch (IOException e) {
Log.e("error while writing ff binary file", e);
}
return false;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 2 additions & 3 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
compileSdkVersion 29

defaultConfig {
applicationId "nl.bravobit.ffmpeg.example"
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0.1"
}
Expand All @@ -25,7 +25,6 @@ android {
}

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

// Android libraries
implementation 'androidx.appcompat:appcompat:1.0.2'
Expand Down

0 comments on commit 777c9b4

Please sign in to comment.