Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private fun mergeVideos(videoFiles: List<File>, outputFile: File) {
- val extractorFactory = DefaultExtractorsFactory()
- val mediaMuxer = MediaMuxer(outputFile.absolutePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
- var videoTrackIndex = -1
- var audioTrackIndex = -1
- videoFiles.forEachIndexed { index, videoFile ->
- val dataSourceFactory = DefaultDataSourceFactory(context, "exoplayer")
- val mediaSource = ProgressiveMediaSource.Factory(dataSourceFactory, extractorFactory).createMediaSource(Uri.fromFile(videoFile))
- val videoTrack = mediaMuxer.addTrack(mediaSource.getTrackInfo(0))
- val audioTrack = mediaMuxer.addTrack(mediaSource.getTrackInfo(1))
- if (index == 0) {
- mediaMuxer.start()
- }
- val bufferInfo = MediaCodec.BufferInfo()
- var isFinished = false
- var currentSampleTime: Long = 0
- while (!isFinished) {
- val sampleData = ByteArray(1024 * 1024)
- val sampleSize = mediaSource.readData(videoTrack, currentSampleTime, bufferInfo)
- if (sampleSize < 0) {
- isFinished = true
- break
- }
- bufferInfo.presentationTimeUs += 1000000L / 30
- bufferInfo.flags = mediaSource.getSampleFlags(videoTrack, bufferInfo.presentationTimeUs)
- mediaMuxer.writeSampleData(videoTrack, ByteBuffer.wrap(sampleData, 0, sampleSize), bufferInfo)
- currentSampleTime = bufferInfo.presentationTimeUs
- }
- isFinished = false
- currentSampleTime = 0
- while (!isFinished) {
- val sampleData = ByteArray(1024 * 1024)
- val sampleSize = mediaSource.readData(audioTrack, currentSampleTime, bufferInfo)
- if (sampleSize < 0) {
- isFinished = true
- break
- }
- bufferInfo.presentationTimeUs += 1000000L / 30
- bufferInfo.flags = mediaSource.getSampleFlags(audioTrack, bufferInfo.presentationTimeUs)
- mediaMuxer.writeSampleData(audioTrack, ByteBuffer.wrap(sampleData, 0, sampleSize), bufferInfo)
- currentSampleTime = bufferInfo.presentationTimeUs
- }
- mediaSource.release()
- }
- mediaMuxer.stop()
- mediaMuxer.release()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement