android:使用FFMPEG

时间:2019-03-27 06:28:04

标签: android ffmpeg

过去两天我一直处于困境,无法找出问题所在。

Log.d(TAG, "doInBackground: wee");
String s = "-i ["+fileName+"] -i ["+fileName3+"] -filter_complex amerge -ac 2 -c:a libmp3lame -q:a 4 ["+finalAudio+"]";
String[] cmd=s.split(" ");
mergeMediaFiles(RecordActivity.this,cmd);

我收到以下错误。

2019-03-27 11:38:03.525 12912-13042/com.encrypt.audiorecord E/FFmpeg: Exception while trying to run: [Ljava.lang.String;@4bfef23
    java.io.IOException: Cannot run program "/data/user/0/com.encrypt.audiorecord/files/ffmpeg": error=2, No such file or directory
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1050)
        at java.lang.Runtime.exec(Runtime.java:695)
        at java.lang.Runtime.exec(Runtime.java:560)
        at com.github.hiteshsondhi88.libffmpeg.ShellCommand.run(ShellCommand.java:10)
        at com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteAsyncTask.doInBackground(FFmpegExecuteAsyncTask.java:38)
        at com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteAsyncTask.doInBackground(FFmpegExecuteAsyncTask.java:10)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.io.IOException: error=2, No such file or directory
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:133)
        at java.lang.ProcessImpl.start(ProcessImpl.java:132)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
        at java.lang.Runtime.exec(Runtime.java:695) 
        at java.lang.Runtime.exec(Runtime.java:560) 
        at com.github.hiteshsondhi88.libffmpeg.ShellCommand.run(ShellCommand.java:10) 
        at com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteAsyncTask.doInBackground(FFmpegExecuteAsyncTask.java:38) 
        at com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteAsyncTask.doInBackground(FFmpegExecuteAsyncTask.java:10) 
        at android.os.AsyncTask$2.call(AsyncTask.java:333) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 

然后我按照link的要求将FFMPEG集成到了Android应用中。

下面是我的mergeMediaFiles()方法:

public void mergeMediaFiles(Context context,String[] cmd) {
        FFmpeg ffmpeg = FFmpeg.getInstance(context);
        try {
            // to execute "ffmpeg -version" command you just need to pass "-version"
            ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

                @Override
                public void onStart() {
                }

                @Override
                public void onProgress(String message) {
                }

                @Override
                public void onFailure(String message) {
                }

                @Override
                public void onSuccess(String message) {
                }

                @Override
                public void onFinish() {
                }
            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            //
        }
    }

我所有文件的路径都来自cacheDir

fileName = getExternalCacheDir().getAbsolutePath();
        fileName += "/audiorecordtest.wav";

1 个答案:

答案 0 :(得分:0)

在执行任何操作之前,我需要加载二进制文件:

public void mergeMediaFiles(Context context,String[] cmd) {
            FFmpeg ffmpeg = FFmpeg.getInstance(context);
            try {
                try {
                    //Load the binary
                    ffmpeg.loadBinary(new LoadBinaryResponseHandler() {

                        @Override
                        public void onStart() {
                            Log.d("ttt", "onStart:loaded ");
                        }

                        @Override
                        public void onFailure() {
                            Log.d("ttt", "onFailure: load");
                        }

                        @Override
                        public void onSuccess() {
                            Log.d("ttt", "onSuccess: load");
                        }

                        @Override
                        public void onFinish() {
                            Log.d("ttt", "onFinish: load");
                        }
                    });
                } catch (FFmpegNotSupportedException e) {
                    // Handle if FFmpeg is not supported by device
                }
                // to execute "ffmpeg -version" command you just need to pass "-version"
                ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

                    @Override
                    public void onStart() {
                        Log.d("ttt", "onStart:merge ");
                    }

                    @Override
                    public void onProgress(String message) {
                        Log.d("ttt", "onProgress: merge");
                    }

                    @Override
                    public void onFailure(String message) {
                        Log.d("ttt", "onFailure: stock down");
                    }

                    @Override
                    public void onSuccess(String message) {
                        Log.d("ttt", "onSuccess: stock up");
                        File file= new File(finalAudio);
                        if (file.exists()) {
                            Log.d(TAG, "onPostExecute: mila bhai");
                            startPlaying(file.getPath());
                        }
                    }

                    @Override
                    public void onFinish() {
                        Log.d("ttt", "onFinish: merge ");
                    }
                });
            } catch (FFmpegCommandAlreadyRunningException e) {
                //
            }
        }

    }