Skip to content

Commit a1276b7

Browse files
author
xufuji456
committed
Feature: add MediaMuxController
1 parent c63132e commit a1276b7

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.frank.androidmedia.controller
2+
3+
import android.media.MediaCodec
4+
import android.media.MediaExtractor
5+
import android.media.MediaFormat
6+
import android.media.MediaMuxer
7+
import android.util.Log
8+
import java.lang.Exception
9+
import java.nio.ByteBuffer
10+
11+
/**
12+
* Using MediaExtractor to demux media format.
13+
* Using MediaMuxer to mux media format again.
14+
* @author frank
15+
* @date 2022/3/21
16+
*/
17+
open class MediaMuxController {
18+
19+
fun muxMediaFile(inputPath: String, outputPath: String): Boolean {
20+
if (inputPath.isEmpty() || outputPath.isEmpty()) {
21+
return false
22+
}
23+
var happenError = false
24+
val mediaMuxer = MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
25+
val mediaExtractor = MediaExtractor()
26+
try {
27+
var videoIndex = 0
28+
var audioIndex = 0
29+
var audioFormat: MediaFormat? = null
30+
var videoFormat: MediaFormat? = null
31+
mediaExtractor.setDataSource(inputPath)
32+
for (i in 0 until mediaExtractor.trackCount) {
33+
val mediaFormat = mediaExtractor.getTrackFormat(i)
34+
val mimeType = mediaFormat.getString(MediaFormat.KEY_MIME)
35+
if (mimeType != null && mimeType.startsWith("video")) {
36+
videoIndex = i
37+
videoFormat = mediaFormat
38+
mediaExtractor.selectTrack(i)
39+
} else if (mimeType != null && mimeType.startsWith("audio") && audioFormat == null) {
40+
audioIndex = i
41+
audioFormat = mediaFormat
42+
mediaExtractor.selectTrack(i)
43+
}
44+
}
45+
if (videoFormat != null) {
46+
mediaMuxer.addTrack(videoFormat)
47+
}
48+
if (audioFormat != null) {
49+
mediaMuxer.addTrack(audioFormat)
50+
}
51+
52+
var finished = false
53+
val bufferInfo = MediaCodec.BufferInfo()
54+
val inputBuffer = ByteBuffer.allocate(2 * 1024 * 1024)
55+
56+
mediaMuxer.start()
57+
58+
while (!finished) {
59+
val sampleSize = mediaExtractor.readSampleData(inputBuffer, 0)
60+
Log.i("MediaMuxerController", "sampleIndex=" + mediaExtractor.sampleTrackIndex + ",sampleSize=" + sampleSize)
61+
if (sampleSize > 0) {
62+
bufferInfo.size = sampleSize
63+
bufferInfo.flags = mediaExtractor.sampleFlags
64+
bufferInfo.presentationTimeUs = mediaExtractor.sampleTime
65+
if (mediaExtractor.sampleTrackIndex == videoIndex) {
66+
mediaMuxer.writeSampleData(videoIndex, inputBuffer, bufferInfo)
67+
} else if (mediaExtractor.sampleTrackIndex == audioIndex) {
68+
mediaMuxer.writeSampleData(audioIndex, inputBuffer, bufferInfo)
69+
}
70+
inputBuffer.flip()
71+
mediaExtractor.advance()
72+
} else if (sampleSize < 0) {
73+
finished = true
74+
}
75+
}
76+
77+
} catch (e: Exception) {
78+
Log.e("MediaMuxerController", "mux error=$e")
79+
happenError = true
80+
} finally {
81+
try {
82+
mediaMuxer.release()
83+
mediaExtractor.release()
84+
} catch (e1: Exception) {
85+
Log.e("MediaMuxerController", "release error=$e1")
86+
}
87+
return !happenError
88+
}
89+
}
90+
91+
}

0 commit comments

Comments
 (0)