Skip to content Skip to sidebar Skip to footer

How To Add Audio To Video While Recording [continuouscaptureactivity] [grafika]

I implement Video recording using ContinuousCaptureActivity.java. it's work perfectly. Now i want to add Audio in this video. I know using MediaMuxer it is possible to add audio i

Solution 1:

Merging Audio File and Video File

privatevoidmuxing() {

StringoutputFile="";

try {

Filefile=newFile(Environment.getExternalStorageDirectory() + File.separator + "final2.mp4");
file.createNewFile();
outputFile = file.getAbsolutePath();

MediaExtractorvideoExtractor=newMediaExtractor();
AssetFileDescriptorafdd= getAssets().openFd("Produce.MP4");
videoExtractor.setDataSource(afdd.getFileDescriptor() ,afdd.getStartOffset(),afdd.getLength());

MediaExtractoraudioExtractor=newMediaExtractor();
audioExtractor.setDataSource(audioFilePath);

Log.d(TAG, "Video Extractor Track Count " + videoExtractor.getTrackCount() );
Log.d(TAG, "Audio Extractor Track Count " + audioExtractor.getTrackCount() );

MediaMuxermuxer=newMediaMuxer(outputFile, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

videoExtractor.selectTrack(0);
MediaFormatvideoFormat= videoExtractor.getTrackFormat(0);
intvideoTrack= muxer.addTrack(videoFormat);

audioExtractor.selectTrack(0);
MediaFormataudioFormat= audioExtractor.getTrackFormat(0);
intaudioTrack= muxer.addTrack(audioFormat);

Log.d(TAG, "Video Format " + videoFormat.toString() );
Log.d(TAG, "Audio Format " + audioFormat.toString() );

booleansawEOS=false;
intframeCount=0;
intoffset=100;
intsampleSize=256 * 1024;
ByteBuffervideoBuf= ByteBuffer.allocate(sampleSize);
ByteBufferaudioBuf= ByteBuffer.allocate(sampleSize);
MediaCodec.BufferInfovideoBufferInfo=newMediaCodec.BufferInfo();
MediaCodec.BufferInfoaudioBufferInfo=newMediaCodec.BufferInfo();


videoExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
audioExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);

muxer.start();

while (!sawEOS)
{
    videoBufferInfo.offset = offset;
    videoBufferInfo.size = videoExtractor.readSampleData(videoBuf, offset);


    if (videoBufferInfo.size < 0 || audioBufferInfo.size < 0)
    {
        Log.d(TAG, "saw input EOS.");
        sawEOS = true;
        videoBufferInfo.size = 0;

    }
    else
    {
        videoBufferInfo.presentationTimeUs = videoExtractor.getSampleTime();
        videoBufferInfo.flags = videoExtractor.getSampleFlags();
        muxer.writeSampleData(videoTrack, videoBuf, videoBufferInfo);
        videoExtractor.advance();


        frameCount++;
        Log.d(TAG, "Frame (" + frameCount + ") Video PresentationTimeUs:" + videoBufferInfo.presentationTimeUs +" Flags:" + videoBufferInfo.flags +" Size(KB) " + videoBufferInfo.size / 1024);
        Log.d(TAG, "Frame (" + frameCount + ") Audio PresentationTimeUs:" + audioBufferInfo.presentationTimeUs +" Flags:" + audioBufferInfo.flags +" Size(KB) " + audioBufferInfo.size / 1024);

    }
}

Toast.makeText(getApplicationContext() , "frame:" + frameCount , Toast.LENGTH_SHORT).show();



booleansawEOS2=false;
intframeCount2=0;
while (!sawEOS2)
{
    frameCount2++;

    audioBufferInfo.offset = offset;
    audioBufferInfo.size = audioExtractor.readSampleData(audioBuf, offset);

    if (videoBufferInfo.size < 0 || audioBufferInfo.size < 0)
    {
        Log.d(TAG, "saw input EOS.");
        sawEOS2 = true;
        audioBufferInfo.size = 0;
    }
    else
    {
        audioBufferInfo.presentationTimeUs = audioExtractor.getSampleTime();
        audioBufferInfo.flags = audioExtractor.getSampleFlags();
        muxer.writeSampleData(audioTrack, audioBuf, audioBufferInfo);
        audioExtractor.advance();


        Log.d(TAG, "Frame (" + frameCount + ") Video PresentationTimeUs:" + videoBufferInfo.presentationTimeUs +" Flags:" + videoBufferInfo.flags +" Size(KB) " + videoBufferInfo.size / 1024);
        Log.d(TAG, "Frame (" + frameCount + ") Audio PresentationTimeUs:" + audioBufferInfo.presentationTimeUs +" Flags:" + audioBufferInfo.flags +" Size(KB) " + audioBufferInfo.size / 1024);

    }
}

Toast.makeText(getApplicationContext() , "frame:" + frameCount2 , Toast.LENGTH_SHORT).show();

muxer.stop();
muxer.release();


} catch (IOException e) {
Log.d(TAG, "Mixer Error 1 " + e.getMessage());
} catch (Exception e) {
Log.d(TAG, "Mixer Error 2 " + e.getMessage());
}

For Samples Visithere

Solution 2:

Sorry, I am late. this is what you want.

https://github.com/Kickflip/kickflip-android-sdk

It also implemented encoding with MediaCodec and upload video stream with ffmpeg.

Post a Comment for "How To Add Audio To Video While Recording [continuouscaptureactivity] [grafika]"