使用FFmpegFrameGrabber和FFmpegFrameRecorder在输出视频中不存在音频

时间:2013-10-23 06:47:09

标签: android javacv

我正在编写以下视频转换代码,但输出视频中没有音频。

public static void convert(File file) {

        FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file.getAbsolutePath());
        IplImage captured_frame = null;

        FFmpegFrameRecorder recorder = null;
        recorder = new FFmpegFrameRecorder(file.getAbsolutePath().replace(".mp4", "_edited.mp4"), FFMPEGRecorderActivity.VIDEO_WIDTH, FFMPEGRecorderActivity.VIDEO_HEIGHT);
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        recorder.setFormat("mp4");
        recorder.setFrameRate(FFMPEGRecorderActivity.VIDEO_FRAME_RATE);
        recorder.setAudioChannels(frameGrabber.getAudioChannels());
        recorder.setSampleRate(FFMPEGRecorderActivity.AUDIO_SAMPLE_RATE_HZ);

        try {
            recorder.start();
            frameGrabber.start();
            while (true) {
                try {
                    captured_frame = frameGrabber.grab();
                    if (captured_frame == null) {
                        System.out.println("!!! Failed cvQueryFrame");
                        break;

                    }
                    recorder.setTimestamp(frameGrabber.getTimestamp());
                    recorder.record(captured_frame);
                } catch (Exception e) {
                }
            }
            recorder.stop();
            recorder.release();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

如何从输入文件获取音频并使用输出文件输入。

1 个答案:

答案 0 :(得分:2)

在上面的代码中实际上,当我抓住captured_frame = frameGrabber.grab();

的帧时,我只抓住captured_frame = frameGrabber.grabFrame();问题解决了问题

新工作代码。

public static String convert(File file) {

    FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file.getAbsolutePath());
    String fileName = null;

    Frame captured_frame = null;

    FFmpegFrameRecorder recorder = null;


    try {
        frameGrabber.start();
        fileName = file.getAbsolutePath().replace(".mp4", "_edited.mp4"); 
        recorder = new FFmpegFrameRecorder(fileName, frameGrabber.getImageWidth(), frameGrabber.getImageHeight(),frameGrabber.getAudioChannels());
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        recorder.setFormat("mp4");
        recorder.setFrameRate(frameGrabber.getFrameRate());
        recorder.setSampleFormat(frameGrabber.getSampleFormat());
        recorder.setSampleRate(frameGrabber.getSampleRate()); 
        recorder.start();
        while (true) {
            try {
                captured_frame = frameGrabber.grabFrame();

                if (captured_frame == null) {
                    System.out.println("!!! Failed cvQueryFrame");
                    break;

                }
                recorder.setTimestamp(frameGrabber.getTimestamp());
                recorder.record(captured_frame);

            } catch (Exception e) {
            }
        }
        recorder.stop();
        recorder.release();
        frameGrabber.stop();
    } catch (Exception e) {
        e.printStackTrace();
    }
    file.delete();
    return fileName;
}