Android中的FFMPEG不会创建输出文件。

时间:2018-01-07 04:36:56

标签: android ffmpeg

我目前在Android项目中使用FFMPEG将视频文件转换为音频文件。

当我通过FFMPEG库执行转换时,不会发生错误。但是,输出文件不会在我已指定的文件夹中创建。

这是我生成音频文件的代码。

OnConvertButtonClickListener convertButtonClickListener = new OnConvertButtonClickListener() {
    @Override
    public void onClick(int position) {
        Converter.loadFFMpegBinary();
        String cmd = CMD_HEAD + videoItems.get(position).getTitle() + CMD_STRICT;

        String[] fileDir = videoItems.get(position).getTitle().split(File.separator);
        String fileName = fileDir[fileDir.length-1];
        String out_audio_file = FileManager.getHomeDir()+ File.separator+ fileName.substring(0, fileName.length()-3)+"aac";

        Log.d("tag1", out_audio_file);

        cmd = cmd+out_audio_file;
        Log.e("tag1", cmd);
        String[] command = cmd.split(" ");
        Converter.execFFmpegBinary(command);
    }
};

这是exeffmpegBinary方法代码,执行此方法后,我的日志窗口中会显示成功。

public static void execFFmpegBinary(final String[] command) {
    try {

        ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
            @Override
            public void onFailure(String s) {
                Log.d("execFFmpegBinary", "fail");
            }

            @Override
            public void onSuccess(String s) {
                Log.d("execFFmpegBinary", "success");
            }

            @Override
            public void onProgress(String s) {
                Log.d("execFFmpegBinary", "progress");
            }

            @Override
            public void onStart() {
                Log.d("execFFmpegBinary", "start");
            }

            @Override
            public void onFinish() {
                Log.d("execFFmpegBinary", "finish");
            }
        });

    } catch (FFmpegCommandAlreadyRunningException e) {
        // do nothing for now
        Log.d("execFFmpegBinary", "Exception");
    }
}

以下是我的cmd示例。

-version -y -i /storage/emulated/0/DCIM/Camera/20180104_031417.mp4 -f aac -ab 192000 -vn /storage/emulated/0/Memento/20180104_031417.aac

任何人都知道为什么我的输出文件没有被创建?

3 个答案:

答案 0 :(得分:1)

始终从ffmpeg检查日志(假设您的脚本正在运行且实际上正在执行)。你的作品应该显示错误:

Requested output format 'aac' is not a suitable output format

-f aac替换为-f adts,如果输出为扩展名为.aac的普通文件名,则省略-f选项。

或者,由于您的MP4输入很可能已经包含AAC音频,因此请考虑将其复制而不是重新编码。为此,请删除-ab 192000并添加-c:a copy

答案 1 :(得分:1)

我使用ffmpeg添加音频可能会对你有帮助。

class AddAudio {
    private Context context;
    private FFmpeg ffmpeg;
    private ProgressDialog progressDialog;
    private String videoPath;
    private String videoWithoutAudioPath;
    private String output;

    AddAudio(Context context, String soundPath, String videoPath, String videoWithoutAudioPath, ProgressDialog progressDialog, FFmpeg ffmpeg) {
        this.context = context;
        this.videoPath = videoPath;
        this.ffmpeg = ffmpeg;
        this.progressDialog = progressDialog;
        this.videoWithoutAudioPath = videoWithoutAudioPath;
        String currentMilliSecond = String.valueOf(System.currentTimeMillis());
        output = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + context.getResources().getString(R.string.app_name) + "/test/" + currentMilliSecond + ".mp4";
        //String cmd = "-y -i " + videoWithoutAudioPath + " -i " + soundPath + " -c:v copy -map 0:v:0 -map 1:a:0 -c:a aac -shortest " + output;
        String cmd = "-y -i " + videoWithoutAudioPath + " -i " + soundPath + " -c:v copy -map 0:v:0 -map 1:a:0 -c:a aac -shortest " + output;


        encodeAudio(cmd);
    }

    private void encodeAudio(String cmd) {
        String[] command = cmd.split(" ");
        if (command.length != 0) {
            execFFmpegBinary(command);
        } else {
            Toast.makeText(context, context.getString(R.string.empty_command_toast), Toast.LENGTH_LONG).show();
        }
    }

    private void execFFmpegBinary(final String[] command) {
        try {
            ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
                @Override
                public void onFailure(String s) {
                    Log.e("Failure", s);
                }

                @Override
                public void onSuccess(String s) {
                    Log.e("Success", s);
                }

                @Override
                public void onProgress(String s) {
                    progressDialog.setMessage("Adding audio....");
                }

                @Override
                public void onStart() {
                    progressDialog.setCancelable(false);
                    progressDialog.setCanceledOnTouchOutside(false);
                    progressDialog.show();
                }

                @Override
                public void onFinish() {
                    /**
                     * delete original video since new video is made with sound
                     */
                    if (deleteOriginalVideo() && deleteWorkingFolder(videoWithoutAudioPath)) {
                        progressDialog.dismiss();
                        Intent notificationIntent = new Intent(Utils.LOCAL_BROADCAST_CODE);
                        notificationIntent.putExtra(Utils.FILE_PATH, output);
                        LocalBroadcastManager.getInstance(context).sendBroadcast(notificationIntent);
                    }
                }
            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            e.printStackTrace();
        }
    }

    private boolean deleteOriginalVideo() {
        boolean success = false;
        File file = new File(videoPath);
        if (file.exists()) {
            success = file.delete();
        }
        return success;
    }

    private boolean deleteWorkingFolder(String deletePath) {
        File file = new File(deletePath);
        File folder = new File(file.getParent());
        if (folder.isDirectory()) {
            for (File child : folder.listFiles()) {
                //noinspection ResultOfMethodCallIgnored
                child.delete();
            }
        }
        return folder.delete();
    }
}

答案 2 :(得分:0)

(嗯,我没有足够的业力来发表评论,而且我对FFmpeg lib很陌生)

我现在能想到的是,始终在连接此FFmpeg库的命令中的字符串时,给出一个 SPACE(“”) ...

更改:

cmd = cmd + out_audio_file;

收件人:(标记cmd和out_audio_file之间的空格)

cmd = cmd + “” + out_audio_file;