Untitled

mail@pastecode.io avatar
unknown
plain_text
9 days ago
5.0 kB
1
Indexable
Never
package com.example.mergevideos;

import android.media.MediaCodec;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.util.Log;
import java.io.IOException;
import java.nio.ByteBuffer;

public class VideoMerger {

    private static final String TAG = "VideoMerger";

    public static void mergeVideos(String video1Path, String video2Path, String outputPath) throws IOException {
        MediaMuxer mediaMuxer = new MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

        MediaExtractor extractor1 = new MediaExtractor();
        extractor1.setDataSource(video1Path);
        MediaExtractor extractor2 = new MediaExtractor();
        extractor2.setDataSource(video2Path);

        int videoTrackIndex = -1;
        int audioTrackIndex = -1;

        // Add video track from the first video
        for (int i = 0; i < extractor1.getTrackCount(); i++) {
            MediaFormat format = extractor1.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            if (mime.startsWith("video/")) {
                extractor1.selectTrack(i);
                videoTrackIndex = mediaMuxer.addTrack(format);
                break;
            }
        }

        // Add audio track from the first video
        for (int i = 0; i < extractor1.getTrackCount(); i++) {
            MediaFormat format = extractor1.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            if (mime.startsWith("audio/")) {
                extractor1.selectTrack(i);
                audioTrackIndex = mediaMuxer.addTrack(format);
                break;
            }
        }

        mediaMuxer.start();

        // Write video samples from the first video
        ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
        MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();

        while (true) {
            int sampleSize = extractor1.readSampleData(buffer, 0);
            if (sampleSize < 0) {
                break;
            }
            bufferInfo.offset = 0;
            bufferInfo.size = sampleSize;
            bufferInfo.presentationTimeUs = extractor1.getSampleTime();
            bufferInfo.flags = extractor1.getSampleFlags();
            mediaMuxer.writeSampleData(videoTrackIndex, buffer, bufferInfo);
            extractor1.advance();
        }

        // Write audio samples from the first video
        while (true) {
            int sampleSize = extractor1.readSampleData(buffer, 0);
            if (sampleSize < 0) {
                break;
            }
            bufferInfo.offset = 0;
            bufferInfo.size = sampleSize;
            bufferInfo.presentationTimeUs = extractor1.getSampleTime();
            bufferInfo.flags = extractor1.getSampleFlags();
            mediaMuxer.writeSampleData(audioTrackIndex, buffer, bufferInfo);
            extractor1.advance();
        }

        extractor1.release();

        // Add video track from the second video
        for (int i = 0; i < extractor2.getTrackCount(); i++) {
            MediaFormat format = extractor2.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            if (mime.startsWith("video/")) {
                extractor2.selectTrack(i);
                break;
            }
        }

        // Add audio track from the second video
        for (int i = 0; i < extractor2.getTrackCount(); i++) {
            MediaFormat format = extractor2.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            if (mime.startsWith("audio/")) {
                extractor2.selectTrack(i);
                break;
            }
        }

        // Write video samples from the second video
        while (true) {
            int sampleSize = extractor2.readSampleData(buffer, 0);
            if (sampleSize < 0) {
                break;
            }
            bufferInfo.offset = 0;
            bufferInfo.size = sampleSize;
            bufferInfo.presentationTimeUs = extractor2.getSampleTime();
            bufferInfo.flags = extractor2.getSampleFlags();
            mediaMuxer.writeSampleData(videoTrackIndex, buffer, bufferInfo);
            extractor2.advance();
        }

        // Write audio samples from the second video
        while (true) {
            int sampleSize = extractor2.readSampleData(buffer, 0);
            if (sampleSize < 0) {
                break;
            }
            bufferInfo.offset = 0;
            bufferInfo.size = sampleSize;
            bufferInfo.presentationTimeUs = extractor2.getSampleTime();
            bufferInfo.flags = extractor2.getSampleFlags();
            mediaMuxer.writeSampleData(audioTrackIndex, buffer, bufferInfo);
            extractor2.advance();
        }

        extractor2.release();
        mediaMuxer.stop();
        mediaMuxer.release();
        Log.i(TAG, "Videos merged successfully!");
    }
}
Leave a Comment