Search code examples
androidvideomergeffmpegjavacv

Merging 2 videos using JavaCV, second video has no sound


This is the code I use:

public void stopRecording() throws Exception, com.googlecode.javacv.FrameRecorder.Exception {

    runAudioThread = false;

    if (recorder != null && recording) {
        recording = false;
        Log.v(LOG_TAG, "Finishing recording, calling stop and release on recorder");
        try {
            recorder.stop();
            recorder.release();
        } catch (FFmpegFrameRecorder.Exception e) {
            e.printStackTrace();
        }

    }
    Log.i(LOG_TAG, "filename = " + ffmpeg_link);
    if (i > 0) {
        Log.i(LOG_TAG, "!!!!!!!!!WILL CONCATENATE");
        FrameGrabber grabber1 = new FFmpegFrameGrabber(pathStart + "/JavaCV/stream0.mp4");
        grabber1.start();
        Log.i(LOG_TAG, "graber1.start");
        FrameGrabber grabber2 = new FFmpegFrameGrabber(pathStart + "/JavaCV/stream1.mp4");
        grabber2.start();
        Log.i(LOG_TAG, "graber2.start");
        FrameRecorder recorder2 = new FFmpegFrameRecorder(pathStart + "/JavaCV/output.mp4", grabber1.getImageWidth(), grabber1.getImageHeight(), grabber1.getAudioChannels());
        recorder2.setFrameRate(grabber1.getFrameRate());
        recorder2.setSampleFormat(grabber1.getSampleFormat());
        recorder2.setSampleRate(grabber1.getSampleRate());
        Log.i(LOG_TAG, "Recorder.start");
        recorder2.start();
        Frame frame;
        int j = 0;
        while ((frame = grabber1.grabFrame()) != null) {
            j++;
            recorder2.record(frame);
            Log.i(LOG_TAG, "while1 nr:" + j + "Frame number: " + grabber1.getFrameNumber());
        }
        Log.i(LOG_TAG, "after while1");
        while ((frame = grabber2.grabFrame()) != null) {
            recorder2.record(frame);
            Log.i(LOG_TAG, "while2");
        }
        Log.i(LOG_TAG, "Recorder.stop");
        recorder2.stop();
        grabber2.stop();
        grabber1.stop();
        Log.i(LOG_TAG, "end concatenate");
    }
    i++;
    ffmpeg_link = pathStart + "/JavaCV/stream" + i + ".mp4";
    Log.i(LOG_TAG, "next filename = " + ffmpeg_link);
}

It works great, it manages to merge the videos, the only issue I have is that it does not have sound for the second video. How can i put the audiochannel on the second video also?


Solution

  • runAudioThread = false; this line in the stopRecording, was putting the audiothread on false, and thats why it did not have sound the second video.