FFmpeg android 执行

FFmpeg android execute

在 windows 我可以使用 ffmpeg.exe

使用以下代码剪切视频

无法在 android 中使用 ffmpeg。 我使用 gradle 在我的应用程序中获取 ffmpeg。

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.github.hiteshsondhi88.libffmpeg:FFmpegAndroid:0.2.5'
}

我的方法中有这些行

VideoIn = getInternalDirectoryPath() + "/Download/Ab.mp4";
VideoOut = getInternalDirectoryPath() + "/Download/Ab1.mp4";

try {
ffmpeg.execute("ffmpeg -i " + VideoIn + " -ss 00:00:03 -c:v libx264 -crf 17 -t 00:00:5 " + VideoOut + " -y",null);
}
catch (FFmpegCommandAlreadyRunningException e) { 
e.printStackTrace();
}

显示此错误:错误 运行 exec()。命令:[/data/data/com.videoeditor.myname.myapp/files/ffmpeg, ffmpeg, -i, /storage/emulated/0/Download/Ab.mp4, -ss, 00:00:03, -c:v, libx264, -crf, 17, -t, 00:00:5, /storage/emulated/0/Download/Ab1.mp4, -y] 工作目录:null 环境:null

这个方法有什么问题?感谢您的帮助

您需要阅读进程的错误流来找出问题所在:

InputStreamReader errReader = new InputStreamReader(process.getErrorStream());
BufferedReader bufReader = new BufferedReader(errReader);
while ((String line = bufReader.readLine()) != null) {
    Log.d("MyApp", line);
}

但我想问题出在 exec 方法的错误参数上,如果你需要 运行 同时 chmodffmpeg,你应该使用 exec(String[] progArray, String[] envp)

好的。您的问题是您没有加载 ffmpeg 二进制文件。您必须在执行命令之前加载它。尝试这样的事情:

private FFmpeg ffmpeg;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FFmpeg ffmpeg = FFmpeg.getInstance(this);
    try {
        //Load the binary
        ffmpeg.loadBinary(new LoadBinaryResponseHandler() {

            @Override
            public void onStart() {}

            @Override
            public void onFailure() {}

            @Override
            public void onSuccess() {}

            @Override
            public void onFinish() {}
        });
    } catch (FFmpegNotSupportedException e) {
        // Handle if FFmpeg is not supported by device
    }
    try {
        // to execute "ffmpeg -version" command you just need to pass "-version" 
        // Now, you can execute your command here
        ffmpeg.execute("ffmpeg -version", new ExecuteBinaryResponseHandler() {

            @Override
            public void onStart() {}

            @Override
            public void onProgress(String message) {}

            @Override
            public void onFailure(String message) {}

            @Override
            public void onSuccess(String message) {
                Log.i("SUCCESS", message);
            }

            @Override
            public void onFinish() {}
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        // Handle if FFmpeg is already running
    }
}

有关 http://hiteshsondhi88.github.io/ffmpeg-android-java/

的更多信息

好的,我找到了答案: 这种使用ffmpeg

的方法在cmd中不需要"ffmpeg"

简单的视频剪辑示例:

 /**
 * Returns the path to internal storage ex:- /storage/emulated/0
 *
 * @return
 */
private String getInternalDirectoryPath() {
    return Environment.getExternalStorageDirectory().getAbsolutePath();
}

VideoIn = getInternalDirectoryPath() + "/Download/Ab.mp4";
VideoOut = getInternalDirectoryPath() + "/Download/Ab1.mp4";

private void CutVideo(){
try {
     ffmpeg.execute("-i "+VideoIn+" -ss 00:01:00 -to 00:02:00 -c copy "+VideoOut ,
new ExecuteBinaryResponseHandler() {

                @Override
                public void onStart() {
                //for logcat
                    Log.w(null,"Cut started");
                }

                @Override
                public void onProgress(String message) {
                //for logcat
                    Log.w(null,message.toString());
                }

                @Override
                public void onFailure(String message) {

                    Log.w(null,message.toString());
                }

                @Override
                public void onSuccess(String message) {

                    Log.w(null,message.toString());
                }

                @Override
                public void onFinish() {

                    Log.w(null,"Cutting video finished");
                }
            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            // Handle if FFmpeg is already running
            e.printStackTrace();
            Log.w(null,e.toString());
        }
}

朋友们,提醒一下,如果你的sdk版本等于或高于23(Marshmelow),不要忘记检查

的运行时权限

android.permission.WRITE_EXTERNAL_STORAGE

那是我的问题。一切都很好,但日志没有显示 onSuccess 结果。
android 运行时权限文档