Skip to content

Instantly share code, notes, and snippets.

@kachurovskiy
Created February 24, 2019 21:42
Show Gist options
  • Save kachurovskiy/8fc1b342da776c392057a7332d2f7e82 to your computer and use it in GitHub Desktop.
Save kachurovskiy/8fc1b342da776c392057a7332d2f7e82 to your computer and use it in GitHub Desktop.
package com.slowvideoc;
import com.arthenica.mobileffmpeg.Config;
import com.arthenica.mobileffmpeg.FFmpeg;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private static final int SELECT_VIDEO = 1001;
private static final int PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1002;
private static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1003;
private Timer timer;
public native void run(String[] argv);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(
new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI),
SELECT_VIDEO);
}
});
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == SELECT_VIDEO) {
String path = getPath(data.getData());
if (path != null) {
convert(path);
} else {
snack("Can't locate the file");
}
}
}
private String getPath(Uri uri) {
String[] projection = { MediaStore.Video.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
int index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(index);
}
return null;
}
private void snack(String message) {
Snackbar.make(findViewById(R.id.main_activity), message, Snackbar.LENGTH_LONG).show();
}
private void convert(String path) {
File file = new File(path);
if (!file.exists()) {
snack("File not found: " + path);
return;
}
int index = path.lastIndexOf(".");
String outputPath = new StringBuilder(path).replace(index, index + 1, "_.").toString();
int code = FFmpeg.execute(String.format(" -i \"%s\" -vcodec libx264 -preset slow -acodec aac \"%s\"", path, outputPath));
if (code != 0) {
snack("Failed with code " + code);
} else {
snack("Started, now wait, it's sloow...");
}
}
public void startTimer() {
timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
int rc = FFmpeg.getLastReturnCode();
String output = FFmpeg.getLastCommandOutput();
if (rc == FFmpeg.RETURN_CODE_SUCCESS) {
Log.i(Config.TAG, "Command execution completed successfully.");
} else if (rc == FFmpeg.RETURN_CODE_CANCEL) {
Log.i(Config.TAG, "Command execution cancelled by user.");
} else {
Log.i(Config.TAG, String.format("Command execution failed with rc=%d and output=%s.", rc, output));
}
}
});
}
};
timer.scheduleAtFixedRate(timerTask, 0, 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment