Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try {
- var mediaMuxer = MediaMuxer(outputFilePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
- var tracksAdded = 0
- var muxerStarted = false
- var indexMap = HashMap<Int, Int>()
- var count = 0
- val extractor = MediaExtractor()
- for (element in videoFiles) {
- extractor.setDataSource(element.absolutePath)
- val trackCount = extractor.trackCount
- for (i in 0 until trackCount) {
- val mediaFormat = extractor.getTrackFormat(i)
- val mime: String? = mediaFormat.getString(MediaFormat.KEY_MIME)
- extractor.selectTrack(i)
- val dstIndex = mediaMuxer.addTrack(mediaFormat)
- indexMap[i] = dstIndex
- tracksAdded++
- }
- // Toast.makeText(context, (count++).toString(), Toast.LENGTH_SHORT).show()
- mediaMuxer.start()
- muxerStarted = true
- var isFinished = false
- val bufferSize = MAX_SAMPLE_SIZE
- var frameCount = 0
- val offset = 0
- val dstBuffer = ByteBuffer.allocate(bufferSize)
- val bufferInfo = BufferInfo()
- var prevOutputPTSUs: Long = 0
- while (!isFinished) {
- bufferInfo.offset = offset
- bufferInfo.size = extractor.readSampleData(dstBuffer, offset)
- if (bufferInfo.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG !== 0) {
- bufferInfo.size = 0
- }
- if (bufferInfo.size < 0) {
- isFinished = true
- bufferInfo.size = 0
- } else {
- bufferInfo.flags = extractor.sampleFlags
- val trackIndex = extractor.sampleTrackIndex
- if (extractor.sampleTime < prevOutputPTSUs) {
- bufferInfo.presentationTimeUs = prevOutputPTSUs + 300
- } else {
- bufferInfo.presentationTimeUs = extractor.sampleTime
- }
- mediaMuxer.writeSampleData(
- indexMap[trackIndex] ?: 0,
- dstBuffer,
- bufferInfo
- )
- prevOutputPTSUs = bufferInfo.presentationTimeUs
- extractor.advance()
- frameCount++
- // Log.e(TAG, "Frame (" + frameCount + ") " +
- // "PresentationTimeUs:" + bufferInfo.presentationTimeUs +
- // " Flags:" + bufferInfo.flags +
- // " TrackIndex:" + trackIndex +
- // " Size(KB) " + bufferInfo.size / 1024)
- }
- }
- mediaMuxer.stop()
- mediaMuxer.release()
- }
- extractor.release()
- } catch (e: Exception) {
- Log.e(TAG, e.message.toString())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement