Android Media Muxer Not Working [Android/Java]

a 夏天 提交于 2019-12-10 18:28:26

问题


Media Muxer

Media Muxer Not Working In Lolipop

Testing on Android Emulator Api 22 (Lolipop)

here is my code for muxing audio and video file

public void muxer(File videoFile, File audioFile, long dowloadId) {
    String outputFile = "";
    String TAG = "MURGE";


    publishProgress(0, 0, dowloadId, 0, "Parsing");

    try {

        if (!new File(DataStore.getInstance(this).getPathDownload() + "/Final").exists()) {
            new File(DataStore.getInstance(this).getPathDownload() + "/Final").mkdirs();
        }

        File file = new File(DataStore.getInstance(this).getPathDownload() + "/Final/" + filename);
        file.createNewFile();

        outputFile = file.getAbsolutePath();

        if (videoFile.isFile()) {
            Log.e(TAG, "Video File Present");
        } else {
            Log.e(TAG, "Video File Not Present");
        }
        if (audioFile.isFile()) {
            Log.e(TAG, "Audio File Present");
        } else {
            Log.e(TAG, "Audio File Not Present");
        }

        MediaExtractor videoExtractor = new MediaExtractor();
        videoExtractor.setDataSource(videoFile.getPath());

        MediaExtractor audioExtractor = new MediaExtractor();
        audioExtractor.setDataSource(audioFile.getPath());

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

        MediaMuxer muxer = new MediaMuxer(outputFile, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

        videoExtractor.selectTrack(0);
        MediaFormat videoFormat = videoExtractor.getTrackFormat(0);
        int videoTrack = muxer.addTrack(videoFormat);

        audioExtractor.selectTrack(0);
        MediaFormat audioFormat = audioExtractor.getTrackFormat(0);
        int audioTrack = muxer.addTrack(audioFormat);

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

        boolean sawEOS = false;
        int frameCount = 0;
        int offset = 100;
        int sampleSize = 1024 * 1024;
        ByteBuffer videoBuf = ByteBuffer.allocate(sampleSize);
        ByteBuffer audioBuf = ByteBuffer.allocate(sampleSize);
        MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo();
        MediaCodec.BufferInfo audioBufferInfo = new MediaCodec.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();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    videoBufferInfo.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME;
                } else {
                    videoBufferInfo.flags = MediaCodec.BUFFER_FLAG_SYNC_FRAME;
                }
                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);

            }
        }
        publishProgress(50, 0, 0, 0, "Parsing");
        Log.e(TAG, "frame:" + frameCount);


        boolean sawEOS2 = false;
        int frameCount2 = 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();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    videoBufferInfo.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME;
                } else {
                    videoBufferInfo.flags = MediaCodec.BUFFER_FLAG_SYNC_FRAME;
                }
                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);

            }


        }
        publishProgress(100, 0, 0, 0, "Parsing");
        Log.e(TAG, "frame:" + frameCount2);

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

        videoFile.delete();
        audioFile.delete();


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

Log cat

    02-19 17:15:11.292 5012-5207/packagename D/MURGE: Video Extractor Track Count 1
    02-19 17:15:11.292 5012-5207/packagename D/MURGE: Audio Extractor Track Count 1
    02-19 17:15:11.301 5012-5207/packagename D/MURGE: Video Format {height=720, width=1280, csd-1=java.nio.ByteArrayBuffer[position=0,limit=8,capacity=8], max-input-size=691200, mime=video/avc, durationUs=209040000, csd-0=java.nio.ByteArrayBuffer[position=0,limit=32,capacity=32]}
    02-19 17:15:11.301 5012-5207/packagename D/MURGE: Audio Format {max-input-size=3110400, aac-profile=2, mime=audio/mp4a-latm, durationUs=209095691, csd-0=java.nio.ByteArrayBuffer[position=0,limit=2,capacity=2], channel-count=2, sample-rate=44100}
    02-19 17:15:11.318 5012-5207/packagename I/MPEG4Writer: limits: 4294967295/0 bytes/us, bit rate: -1 bps and the estimated moov size 3072 bytes
    02-19 17:15:11.320 5012-5207/packagename D/MURGE: Frame (1) Video PresentationTimeUs:40000 Flags:1 Size(KB) 1
    02-19 17:15:11.320 5012-5207/packagename D/MURGE: Frame (1) Audio PresentationTimeUs:0 Flags:0 Size(KB) 0
    02-19 17:15:11.322 5012-5225/packagename I/MPEG4Writer: setStartTimestampUs: 40000
    02-19 17:15:11.322 5012-5225/packagename I/MPEG4Writer: Earliest track starting time: 40000
    02-19 17:15:11.323 5012-5207/packagename D/MURGE: Frame (2) Video PresentationTimeUs:160000 Flags:1 Size(KB) 0
    02-19 17:15:11.323 5012-5207/packagename D/MURGE: Frame (2) Audio PresentationTimeUs:0 Flags:0 Size(KB) 0
    02-19 17:15:11.325 5012-5207/packagename D/MURGE: Frame (3) Video PresentationTimeUs:80000 Flags:1 Size(KB) 0
    02-19 17:15:11.325 5012-5207/packagename D/MURGE: Frame (3) Audio PresentationTimeUs:0 Flags:0 Size(KB) 0
    02-19 17:15:11.326 5012-5225/packagename E/MPEG4Writer: timestampUs 40000 < lastTimestampUs 120000 for Video track
    02-19 17:15:11.394 5012-5025/packagename I/art: Background sticky concurrent mark sweep GC freed 22027(1475KB) AllocSpace objects, 0(0B) LOS objects, 17% free, 6MB/8MB, paused 14.223ms total 92.193ms
    02-19 17:15:38.534 5012-5025/packagename I/art: Background sticky concurrent mark sweep GC freed 1599(1338KB) AllocSpace objects, 0(0B) LOS objects, 17% free, 6MB/8MB, paused 6.730ms total 24.344ms
    02-19 17:16:00.019 1759-1814/system_process I/ProcessStatsService: Prepared write state in 1ms
    02-19 17:16:16.766 1156-1565/? I/AudioFlinger: BUFFER TIMEOUT: remove(4096) from active list on thread 0xb58ac000
    02-19 17:16:22.314 2659-3143/com.google.android.gms.persistent E/NetworkScheduler.ATC: Called cancelTask for already completed task com.google.android.gms/.chimera.container.ConfigService{u=0 tag="ChimeraConfigService_OneOffRetry" trigger=window{start=3300s,end=3900s,earliest=-292s,latest=307s} requirements=[NET_CONNECTED] attributes=[PERSISTED] scheduled=-3592s last_run=-245s jid=N/A status=ACTIVE retries=0 client_lib=MANCHEGO_GCM-13280000} :TIMED_OUT
    02-19 17:16:26.214 5012-5012/packagename E/WV: URL :: https://m.youtube.com/watch?v=IssysxAisfo
    02-19 17:16:26.267 1759-2555/system_process I/MediaFocusControl:  AudioFocus  abandonAudioFocus() from android.media.AudioManager@2b9bc651com.android.org.chromium.media.MediaPlayerListener@3b664eb6
    02-19 17:16:27.457 1759-3533/system_process I/MediaFocusControl:  AudioFocus  requestAudioFocus() from android.media.AudioManager@2b9bc651com.android.org.chromium.media.MediaPlayerListener@16d77914 req=3flags=0x0
    02-19 17:16:27.469 5012-5235/packagename I/OMXClient: Using client-side OMX mux.
    02-19 17:16:27.483 5012-5236/packagename I/SoftAAC2: limiting to stereo output
    02-19 17:16:27.487 5012-5236/packagename I/SoftAAC2: Reconfiguring decoder: 0->44100 Hz, 0->2 channels
    02-19 17:16:27.488 5012-5240/packagename I/OMXClient: Using client-side OMX mux.
    02-19 17:16:27.525 5012-5240/packagename W/OMXNodeInstance: [1:google.vp9.decoder] component does not support metadata mode; using fallback
    02-19 17:16:27.525 5012-5240/packagename E/ACodec: [OMX.google.vp9.decoder] storeMetaDataInBuffers failed w/ err -1010
    02-19 17:16:27.525 5012-5240/packagename E/OMXNodeInstance: getParameter(1:google.vp9.decoder, ParamVideoAndroidVp8Encoder(0x6f600007)) ERROR: UnsupportedIndex(0x8000101a)
    02-19 17:16:27.541 1143-1196/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 462848
    02-19 17:16:27.566 1143-2252/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 462848
    02-19 17:16:33.209 5012-5012/packagename E/Service: E/MPEG4Writer: timestampUs 40000 < lastTimestampUs 120000 for Video track
    02-19 17:16:33.324 5012-5012/packagename E/Service: E/MPEG4Writer: timestampUs 40000 < lastTimestampUs 120000 for Video track
    02-19 17:16:33.571 5012-5012/packagename E/Service: E/MPEG4Writer: timestampUs 40000 < lastTimestampUs 120000 for Video track
    02-19 17:16:53.291 1759-1813/system_process I/UsageStatsService: User[0] Flushing usage stats to disk
    02-19 17:17:18.407 5012-5025/packagename I/art: Background sticky concurrent mark sweep GC freed 1371(1112KB) AllocSpace objects, 0(0B) LOS objects, 14% free, 7MB/8MB, paused 5.788ms total 12.171ms
    02-19 17:17:59.364 5012-5012/packagename E/Service: lastTimestampUs
    02-19 17:18:46.471 5012-5012/packagename E/Service: timestampUs
    02-19 17:19:13.298 5012-5012/packagename E/Service: timestampUs
    02-19 17:19:13.940 1156-1565/? I/AudioFlinger: BUFFER TIMEOUT: remove(4096) from active list on thread 0xb58ac000
    02-19 17:19:17.531 5012-5012/packagename E/Service: MPEG4Writer
    02-19 17:19:23.377 5012-5012/packagename E/WV: URL :: https://m.youtube.com/watch?v=dZ0fwJojhrs
    02-19 17:19:23.431 1759-2555/system_process I/MediaFocusControl:  AudioFocus  abandonAudioFocus() from android.media.AudioManager@2b9bc651com.android.org.chromium.media.MediaPlayerListener@16d77914
    02-19 17:19:24.428 1759-1805/system_process I/MediaFocusControl:  AudioFocus  requestAudioFocus() from android.media.AudioManager@2b9bc651com.android.org.chromium.media.MediaPlayerListener@17bac7b2 req=3flags=0x0
    02-19 17:19:24.433 5012-5247/packagename I/OMXClient: Using client-side OMX mux.
    02-19 17:19:24.449 5012-5248/packagename I/SoftAAC2: limiting to stereo output
    02-19 17:19:24.449 5012-5248/packagename I/SoftAAC2: Reconfiguring decoder: 0->44100 Hz, 0->2 channels
    02-19 17:19:24.454 5012-5252/packagename I/OMXClient: Using client-side OMX mux.
    02-19 17:19:24.458 5012-5252/packagename W/OMXNodeInstance: [1:google.vp9.decoder] component does not support metadata mode; using fallback
    02-19 17:19:24.458 5012-5252/packagename E/ACodec: [OMX.google.vp9.decoder] storeMetaDataInBuffers failed w/ err -1010
    02-19 17:19:24.458 5012-5252/packagename E/OMXNodeInstance: getParameter(1:google.vp9.decoder, ParamVideoAndroidVp8Encoder(0x6f600007)) ERROR: UnsupportedIndex(0x8000101a)
    02-19 17:19:24.484 1143-1190/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 462848
    02-19 17:19:24.554 1143-3796/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 462848
    02-19 17:19:43.077 5012-5025/packagename I/art: Background sticky concurrent mark sweep GC freed 858(693KB) AllocSpace objects, 0(0B) LOS objects, 9% free, 7MB/8MB, paused 9.216ms total 21.841ms
    02-19 17:19:46.978 5012-5012/packagename E/Service: lastTimestampUs
    02-19 17:20:04.939 5012-5025/packagename I/art: Background sticky concurrent mark sweep GC freed 705(591KB) AllocSpace objects, 0(0B) LOS objects, 8% free, 7MB/8MB, paused 7.366ms total 18.008ms
    02-19 17:20:26.415 5012-5025/packagename I/art: Background sticky concurrent mark sweep GC freed 592(479KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 7MB/8MB, paused 6.232ms total 15.288ms
    02-19 17:20:46.467 5012-5025/packagename I/art: Background sticky concurrent mark sweep GC freed 409(344KB) AllocSpace objects, 0(0B) LOS objects, 5% free, 7MB/8MB, paused 5.358ms total 10.100ms
    02-19 17:20:49.359 5012-5025/packagename I/art: Background sticky concurrent mark sweep GC freed 387(320KB) AllocSpace objects, 0(0B) LOS objects, 5% free, 7MB/8MB, paused 9.664ms total 33.219ms
    02-19 17:21:51.433 5012-5012/packagename E/Service: Muxing Single Video/Audio Track
    02-19 17:22:45.690 5012-5012/packagename E/Service: MediaMuxer
    02-19 17:23:19.857 1156-1565/? I/AudioFlinger: BUFFER TIMEOUT: remove(4096) from active list on thread 0xb58ac000
    02-19 17:23:29.771 5012-5012/packagename E/WV: URL :: https://m.youtube.com/watch?v=pnMQLrS5sTE
    02-19 17:23:29.825 1759-2592/system_process I/MediaFocusControl:  AudioFocus  abandonAudioFocus() from android.media.AudioManager@2b9bc651com.android.org.chromium.media.MediaPlayerListener@17bac7b2
    02-19 17:23:31.800 1759-2618/system_process I/MediaFocusControl:  AudioFocus  requestAudioFocus() from android.media.AudioManager@2b9bc651com.android.org.chromium.media.MediaPlayerListener@2e3d1280 req=3flags=0x0
    02-19 17:23:31.808 5012-5262/packagename I/OMXClient: Using client-side OMX mux.
    02-19 17:23:31.843 5012-5263/packagename I/SoftAAC2: limiting to stereo output
    02-19 17:23:31.844 5012-5263/packagename I/SoftAAC2: Reconfiguring decoder: 0->44100 Hz, 0->2 channels
    02-19 17:23:31.850 5012-5267/packagename I/OMXClient: Using client-side OMX mux.
    02-19 17:23:31.858 5012-5267/packagename W/OMXNodeInstance: [1:google.vp9.decoder] component does not support metadata mode; using fallback
    02-19 17:23:31.858 5012-5267/packagename E/ACodec: [OMX.google.vp9.decoder] storeMetaDataInBuffers failed w/ err -1010
    02-19 17:23:31.859 5012-5267/packagename E/OMXNodeInstance: getParameter(1:google.vp9.decoder, ParamVideoAndroidVp8Encoder(0x6f600007)) ERROR: UnsupportedIndex(0x8000101a)
    02-19 17:23:31.875 1143-3796/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 462848
    02-19 17:23:31.892 1143-1818/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 462848
    02-19 17:23:41.652 5012-5025/packagename I/art: Background sticky concurrent mark sweep GC freed 2063(1675KB) AllocSpace objects, 0(0B) LOS objects, 20% free, 6MB/8MB, paused 5.382ms total 15.473ms
    02-19 17:24:12.950 1759-3533/system_process I/MediaFocusControl:  AudioFocus  abandonAudioFocus() from android.media.AudioManager@2b9bc651com.android.org.chromium.media.MediaPlayerListener@2e3d1280
    02-19 17:24:13.276 1759-2592/system_process I/MediaFocusControl:  AudioFocus  requestAudioFocus() from android.media.AudioManager@2b9bc651com.android.org.chromium.media.MediaPlayerListener@3d09acfe req=3flags=0x0
    02-19 17:24:13.294 5012-5272/packagename I/OMXClient: Using client-side OMX mux.
    02-19 17:24:13.308 5012-5273/packagename I/SoftAAC2: limiting to stereo output
    02-19 17:24:13.310 5012-5273/packagename I/SoftAAC2: Reconfiguring decoder: 0->44100 Hz, 0->2 channels
    02-19 17:24:13.312 5012-5277/packagename I/OMXClient: Using client-side OMX mux.
    02-19 17:24:13.319 5012-5277/packagename W/OMXNodeInstance: [1:google.vp9.decoder] component does not support metadata mode; using fallback
    02-19 17:24:13.319 5012-5277/packagename E/ACodec: [OMX.google.vp9.decoder] storeMetaDataInBuffers failed w/ err -1010
    02-19 17:24:13.319 5012-5277/packagename E/OMXNodeInstance: getParameter(1:google.vp9.decoder, ParamVideoAndroidVp8Encoder(0x6f600007)) ERROR: UnsupportedIndex(0x8000101a)
    02-19 17:24:13.342 1143-1190/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 462848
    02-19 17:24:13.377 1143-3796/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 462848
    02-19 17:24:53.642 2372-2372/com.android.systemui D/PhoneStatusBar: disable: < expand ICONS* alerts SYSTEM_INFO* back home recent clock search >
    02-19 17:24:54.628 2372-2372/com.android.systemui D/PhoneStatusBar: disable: < expand icons* alerts system_info* back home recent clock search >
    02-19 17:24:54.787 1759-2703/system_process W/InputMethodManagerService: Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@d13dc35 attribute=null, token = android.os.BinderProxy@2cf788e6
    02-19 17:25:37.031 5012-5025/packagename I/art: Background sticky concurrent mark sweep GC freed 1736(1453KB) AllocSpace objects, 0(0B) LOS objects, 18% free, 6MB/8MB, paused 7.965ms total 16.716ms

I cant find where i am doing wrong its works on 8.1.0

last log msg is

    02-19 17:15:11.325 5012-5207/packagename D/MURGE: Frame (3) Video PresentationTimeUs:80000 Flags:1 Size(KB) 0
    02-19 17:15:11.325 5012-5207/packagename D/MURGE: Frame (3) Audio PresentationTimeUs:0 Flags:0 Size(KB) 0

then

    02-19 17:15:11.326 5012-5225/packagename E/MPEG4Writer: timestampUs 40000 < lastTimestampUs 120000 for Video track

I dont know what is timestampUs 40000 < lastTimestampUs 120000 for Video track any one help please

This is working in

  1. Api 25 (Nougat) Emulator
  2. Api 27 (Oreo) Emulator and Real Device
  3. Api 28 (Pie) Emulator

回答1:


Try to refactor your extracting logic to using videoExtractor.advance(); as a condition of sawEOS

      do {
            videoBufferInfo.offset = offset;
            videoBufferInfo.size = videoExtractor.readSampleData(videoBuf, offset);


            videoBufferInfo.presentationTimeUs = videoExtractor.getSampleTime();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                videoBufferInfo.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME;
            } else {
                videoBufferInfo.flags = MediaCodec.BUFFER_FLAG_SYNC_FRAME;
            }
            muxer.writeSampleData(videoTrack, videoBuf, videoBufferInfo);

         } while (videoExtractor.advance());


来源:https://stackoverflow.com/questions/54765921/android-media-muxer-not-working-android-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!