Sakib Syed Sakib Syed - 2 months ago 18
Android Question

ffmpeg command not working with white space of directory path in Android

I am working with

FFMPEG
library in which I want to rotate video using this library which works fine if I am
file path
has no any
white space
. But in my case I have white space of video directory (you can see full path in
String commandStr
onPreExecute()
method of
asynctask
) path then it is not working at all. I have also seen same question like this and some more but not get any idea how to resolved it properly. Below is my code of MainActivity.class

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

String workFolder = null;
String demoVideoFolder = null;
String vkLogPath = null;
private boolean commandValidationFailedFlag = false;

private Button btnRun;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

GeneralUtils.checkForPermissionsMAndAbove(MainActivity.this, true);


setIds();
setListner();


demoVideoFolder = Environment.getExternalStorageDirectory().getAbsolutePath() + "/videokit/";

Log.i(Prefs.TAG, getString(R.string.app_name) + " version: " + GeneralUtils.getVersionName(getApplicationContext()) );
workFolder = getApplicationContext().getFilesDir().getAbsolutePath() + "/";
vkLogPath = workFolder + "vk.log";

GeneralUtils.copyLicenseFromAssetsToSDIfNeeded(this, workFolder);
GeneralUtils.copyDemoVideoFromAssetsToSDIfNeeded(this, demoVideoFolder);


int rc = GeneralUtils.isLicenseValid(getApplicationContext(), workFolder);
Log.i(Prefs.TAG, "License check RC: " + rc);
}

private void setListner() {
btnRun.setOnClickListener(this);
}

private void setIds() {
try {
btnRun = (Button)findViewById(R.id.btnRun);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnRun:

Log.i(Prefs.TAG, "run clicked.");
if (GeneralUtils.checkIfFileExistAndNotEmpty(workFolder)) {
new TranscdingBackground(MainActivity.this).execute();
}
else {
Toast.makeText(getApplicationContext(), workFolder + " not found", Toast.LENGTH_LONG).show();
}
break;
}
}

public class TranscdingBackground extends AsyncTask<String, Integer, Integer>
{

ProgressDialog progressDialog;
Activity _act;
String commandStr;

public TranscdingBackground (Activity act) {
_act = act;
}



@Override
protected void onPreExecute() {

// commandStr = "ffmpeg -y -i /storage/emulated/0/WhatsApp/Media/in.mp4 -vf rotate=270*(PI/180) /sdcard/videokit/out.mp4";
commandStr = "ffmpeg -y -i /storage/emulated/0/WhatsApp/Media/WhatsApp Video/in.mp4 -vf rotate=270*(PI/180) /sdcard/videokit/out.mp4";


progressDialog = new ProgressDialog(_act);
progressDialog.setMessage("FFmpeg4Android Transcoding in progress...");
progressDialog.show();

}

protected Integer doInBackground(String... paths) {
Log.i(Prefs.TAG, "doInBackground started...");

// delete previous log
boolean isDeleted = GeneralUtils.deleteFileUtil(workFolder + "/vk.log");
Log.i(Prefs.TAG, "vk deleted: " + isDeleted);

PowerManager powerManager = (PowerManager)_act.getSystemService(Activity.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "VK_LOCK");
Log.d(Prefs.TAG, "Acquire wake lock");
wakeLock.acquire();



LoadJNI vk = new LoadJNI();
try {

vk.run(GeneralUtils.utilConvertToComplex(commandStr), workFolder, getApplicationContext());

// copying vk.log (internal native log) to the videokit folder
GeneralUtils.copyFileToFolder(vkLogPath, demoVideoFolder);

} catch (CommandValidationException e) {
Log.e(Prefs.TAG, "vk run exeption.", e);
commandValidationFailedFlag = true;

} catch (Throwable e) {
Log.e(Prefs.TAG, "vk run exeption.", e);
}
finally {
if (wakeLock.isHeld())
wakeLock.release();
else{
Log.i(Prefs.TAG, "Wake lock is already released, doing nothing");
}
}
Log.i(Prefs.TAG, "doInBackground finished");
return Integer.valueOf(0);
}

protected void onProgressUpdate(Integer... progress) {
}

@Override
protected void onCancelled() {
Log.i(Prefs.TAG, "onCancelled");
//progressDialog.dismiss();
super.onCancelled();
}


@Override
protected void onPostExecute(Integer result) {
Log.i(Prefs.TAG, "onPostExecute");
progressDialog.dismiss();
super.onPostExecute(result);

// finished Toast
String rc = null;
if (commandValidationFailedFlag) {
rc = "Command Vaidation Failed";
}
else {
rc = GeneralUtils.getReturnCodeFromLog(vkLogPath);
}
final String status = rc;
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, status, Toast.LENGTH_LONG).show();
if (status.equals("Transcoding Status: Failed")) {
Toast.makeText(MainActivity.this, "Check: " + vkLogPath + " for more information.", Toast.LENGTH_LONG).show();
}
}
});
}
}

}


Here
onPreExecute()
method I have given video file path.

Answer

Just convert your command from String to ArrayList as like below:

commandStr = "ffmpeg -y -i /storage/emulated/0/WhatsApp/Media/WhatsApp Video/in.mp4 -vf rotate=270*(PI/180) /sdcard/videokit/out.mp4";

ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("ffmpeg");
            arrayList.add("-y");
            arrayList.add("-i");
            arrayList.add("/storage/emulated/0/WhatsApp/Media/WhatsApp Video/in.mp4");
            arrayList.add("-vf");
            arrayList.add("rotate=270*(PI/180)");
            arrayList.add("/sdcard/videokit/out.mp4");

and change your vk.run(); method as like below :

vk.run(GeneralUtils.utilConvertToComplex(commandStr), workFolder, getApplicationContext());

 vk.run(arrayList.toArray(new String[arrayList.size()]), workFolder, getApplicationContext());