FFmpeg Executing command error

2.9k Views Asked by At

When I conversion mp4 to mp3 using ffmpeg command
FFmpeg command Execution error- Working directory null IOEXception Executing below code.

File path=Environment.getExternalStorageDirectory();    
try {
            String ffmpegCommand="ffmpeg -i "+path.getAbsolutePath()+"/test/1.mp4 -f avi -acodec mp3 "+path.getAbsolutePath()+"/Songss.avi";
              Process ffmpegProcess = new ProcessBuilder(ffmpegCommand).redirectErrorStream(true).start();

              String line;
              BufferedReader reader = new BufferedReader(new InputStreamReader(ffmpegProcess.getInputStream()));
              Log.d("tag", "*******Starting FFMPEG");

              while((line = reader.readLine())!=null){

                  Log.d("tag", "***"+line+"***"); 
              }
              Log.d("tag","****ending FFMPEG****");

        } catch (IOException e) {
            e.printStackTrace(); }

LogCat

java.io.IOException: Error running exec(). Command: [/data/data/com.example.ffmpegcommandexecute/ffmpeg -i /mnt/sdcard/Songs.mp4 -vn -s 00:00:10 -acodec libmp3lame output.mp3]
 Working Directory: null Environment: [ANDROID_SOCKET_zygote=10, ANDROID_BOOTLOGO=1, EXTERNAL_STORAGE=/mnt/sdcard, ANDROID_ASSETS=/system/app, PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, ASEC_MOUNTPOINT=/mnt/asec, LOOP_MOUNTPOINT=/mnt/obb, BOOTCLASSPATH=/system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/filterfw.jar, EXTERNAL_STORAGE_SD=/mnt/ext_card, EXTERNAL_STORAGE_ALL=/mnt/sdcard:/mnt/ext_card, WTLE_VERSION=3.2.0.patch6.1, ANDROID_DATA=/data, LD_LIBRARY_PATH=/vendor/lib:/system/lib, ANDROID_ROOT=/system, ANDROID_PROPERTY_WORKSPACE=9,65536]
    at java.lang.ProcessManager.exec(ProcessManager.java:211)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:195)
    at com.example.ffmpegcommandexecute.MJPEGFFMPEGTest$ProcessVideo.doInBackground(MJPEGFFMPEGTest.java:301)
    at com.example.ffmpegcommandexecute.MJPEGFFMPEGTest$ProcessVideo.doInBackground(MJPEGFFMPEGTest.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:264)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    at java.lang.Thread.run(Thread.java:856)
 Caused by: java.io.IOException: No such file or directory
    at java.lang.ProcessManager.exec(Native Method)
    at java.lang.ProcessManager.exec(ProcessManager.java:209)
1

There are 1 best solutions below

2
On

If you only want to execute your command and you are not interested about the output of the command, use this code:

    try {
            Process process= Runtime.getRuntime().exec(YourStringCommand);
            process.waitFor();
            Log.d(TAG, "Process finished");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

And remember your string command must be something like that:

Your ffmpeg executable file path + all the rest of your command

The problem is that you need to have this executable file into your application folder (/data/data/yourPackage/) because permission problems. So you should be able to put your ffmpeg file into your package folder and change their permissions.

Hope it's useful!!