Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
2.0 kB
2
Indexable
Never
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 void mergeVideos(String inputFile1, String inputFile2, String outputFile) {
        MediaMuxer mediaMuxer = null;
        MediaExtractor videoExtractor1 = new MediaExtractor();
        MediaExtractor videoExtractor2 = new MediaExtractor();
        try {
            mediaMuxer = new MediaMuxer(outputFile, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

            // Extract and write video track from the first video
            videoExtractor1.setDataSource(inputFile1);
            int videoTrackIndex1 = selectTrack(videoExtractor1, "video/");
            MediaFormat videoFormat1 = videoExtractor1.getTrackFormat(videoTrackIndex1);
            videoExtractor1.selectTrack(videoTrackIndex1);
            int muxerVideoTrackIndex = mediaMuxer.addTrack(videoFormat1);

            // Extract and write audio track from the first video
            int audioTrackIndex1 = selectTrack(videoExtractor1, "audio/");
            int muxerAudioTrackIndex1 = -1;
            if (audioTrackIndex1 >= 0) {
                MediaFormat audioFormat1 = videoExtractor1.getTrackFormat(audioTrackIndex1);
                videoExtractor1.selectTrack(audioTrackIndex1);
                muxerAudioTrackIndex1 = mediaMuxer.addTrack(audioFormat1);
            }

            mediaMuxer.start();

            // Write video and audio data from the first video
            writeSampleData(videoExtractor1, mediaMuxer, muxerVideoTrackIndex, muxerAudioTrackIndex1);

            // Extract and write video track from the second video
            videoExtractor2.setDataSource(inputFile2);
            int videoTrackIndex2 = selectTrack(videoExtractor2, "video/");
            videoExtractor2.selectTrack(videoTrackIndex2);

     
Leave a Comment