Skip to content

Commit

Permalink
extract the getCommandParameter() method to Config class
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcohn committed Jul 22, 2020
1 parent 20e3c76 commit 609d0b9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 65 deletions.
30 changes: 30 additions & 0 deletions android/app/src/main/java/com/arthenica/mobileffmpeg/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
package com.arthenica.mobileffmpeg;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;
import android.util.Log;

import java.io.File;
Expand Down Expand Up @@ -755,6 +759,32 @@ static List<FFmpegExecution> listFFmpegExecutions() {
*/
private native static void ignoreNativeSignal(final int signum);

public static String getCommandParameter(Context context, Uri uri) {

String displayName = "unknown";
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_DISPLAY_NAME));
}
} catch (Throwable ex) {
Log.e(TAG, "failed to get column", ex);
} finally {
if (cursor != null)
cursor.close();
}

int fd = -1;
try {
ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
fd = parcelFileDescriptor.detachFd();
} catch (Throwable e) {
Log.e(TAG, "obtaining ParcelFileDescriptor for " + uri, e);
}

return "file:/proc/self/" + fd + "/" + displayName;
}

/**
* <p>Run transcode_aac from doc/examples.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
package com.arthenica.mobileffmpeg.test;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;
import android.text.method.ScrollingMovementMethod;
import android.util.AndroidRuntimeException;
import android.util.Log;
Expand All @@ -50,7 +47,7 @@ public class ScopedStorageTabFragment extends Fragment {

private EditText commandText;
private TextView outputText;
private Uri videoUri;
private Uri inUri;
private Uri outUri;
private static final int REQUEST_SAF_FFPROBE = 11;
private static final int REQUEST_SAF_TRANSCODE_IN = 12;
Expand Down Expand Up @@ -132,30 +129,7 @@ public Object call() {
private void runFFprobe() {
clearLog();

String displayName = "unknown";
Cursor cursor = getContext().getContentResolver().query(videoUri, null, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_DISPLAY_NAME));
}
} catch (Throwable ex) {
Log.e(MainActivity.TAG, "failed to get column", ex);
} finally {
if (cursor != null)
cursor.close();
}

int fd = -1;
try {
ParcelFileDescriptor parcelFileDescriptor = getContext().getContentResolver().openFileDescriptor(videoUri, "r");

Log.d(MainActivity.TAG, videoUri.toString() + " size: " + parcelFileDescriptor.getStatSize() + " filename: " + displayName);
fd = parcelFileDescriptor.detachFd();
} catch (Throwable e) {
Log.e(MainActivity.TAG, "obtaining ParcelFileDescriptor for " + videoUri, e);
}

final String ffprobeCommand = "-hide_banner file:/proc/self/" + fd + "/" + displayName;
final String ffprobeCommand = "-hide_banner " + Config.getCommandParameter(getContext(), inUri);

Log.d(MainActivity.TAG, "Testing FFprobe COMMAND synchronously.");

Expand All @@ -168,53 +142,22 @@ private void runFFprobe() {
if (result != 0) {
Popup.show(requireContext(), "Command failed. Please check output for the details.");
}
videoUri = null;
inUri = null;
}

private void runTranscode() {
clearLog();

Log.d(MainActivity.TAG, "Testing transcode(" + videoUri + ", " + outUri + ")");

String displayName = "unknown";
Cursor cursor = getContext().getContentResolver().query(outUri, null, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_DISPLAY_NAME));
}
} catch (Throwable ex) {
Log.e(MainActivity.TAG, "failed to get column", ex);
} finally {
if (cursor != null)
cursor.close();
}

int fdin = -1;
try {
ParcelFileDescriptor parcelFileDescriptor = getContext().getContentResolver().openFileDescriptor(videoUri, "r");
Log.d(MainActivity.TAG, videoUri.toString() + " size: " + parcelFileDescriptor.getStatSize());
fdin = parcelFileDescriptor.detachFd();
} catch (Throwable e) {
Log.e(MainActivity.TAG, "obtaining ParcelFileDescriptor for " + videoUri, e);
}

int fdout = -1;
try {
ParcelFileDescriptor parcelFileDescriptor = getContext().getContentResolver().openFileDescriptor(outUri, "w");
Log.d(MainActivity.TAG, outUri.toString() + " size: " + parcelFileDescriptor.getStatSize());
fdout = parcelFileDescriptor.detachFd();
} catch (Throwable e) {
Log.e(MainActivity.TAG, "obtaining ParcelFileDescriptor for " + videoUri, e);
}
Log.d(MainActivity.TAG, "Testing transcode(" + inUri + ", " + outUri + ")");

int result = Config.runTranscode("file:/proc/self/" + fdin, "file:/proc/self/" + fdout + "/" + displayName);
int result = Config.runTranscode(Config.getCommandParameter(getContext(), inUri), Config.getCommandParameter(getContext(), outUri));
Log.d(MainActivity.TAG, String.format("Transcode exited with rc %d", result));

if (result != 0) {
Popup.show(requireContext(), "Command failed. Please check output for the details.");
}

videoUri = outUri;
inUri = outUri;
outUri = null;
if (result == 0) {
runFFprobe();
Expand All @@ -237,10 +180,10 @@ public void clearLog() {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SAF_FFPROBE && resultCode == RESULT_OK && data != null) {
videoUri = data.getData();
inUri = data.getData();
runFFprobe();
} else if (requestCode == REQUEST_SAF_TRANSCODE_IN && resultCode == RESULT_OK && data != null) {
videoUri = data.getData();
inUri = data.getData();
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT)
.setType("audio/*")
.putExtra(Intent.EXTRA_TITLE, "transcode.aac")
Expand Down

0 comments on commit 609d0b9

Please sign in to comment.