English 中文(简体)
• 如何在和海底演播室(otlin)播放带有媒体密码的视频。
原标题:How to compress video with Android Media Codec in android studio kotlin

下一个线造成错误轨道 指数为0:

mediaMuxer.writeSampleData(履历:压缩Buffer,缓冲Info>

这里我完全是:

private fun reduceVideoSize(inputDescriptor: FileDescriptor, outputDescriptor: FileDescriptor, compressionPercentage: Int) {
        lifecycleScope.launch {
            Log.d("reduceVideoSize", "STARTED")
            val mediaExtractor = MediaExtractor()
            mediaExtractor.setDataSource(inputDescriptor)
            val trackIndex = selectTrack(mediaExtractor, false)
            Log.d("TRACK INDEX", trackIndex.toString())
            mediaExtractor.selectTrack(trackIndex)
            //val mediaFormat = mediaExtractor.getTrackFormat(trackIndex)
            val mediaFormat = MediaFormat.createVideoFormat("video/avc", 1920, 1080)
            mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 2000000)
            mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 30)
            mediaFormat.setInteger(
                MediaFormat.KEY_COLOR_FORMAT,
                MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible
            )
            mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1)
            Log.d("MediaFormat", mediaFormat.toString())
            val mimeType = mediaFormat.getString(MediaFormat.KEY_MIME)
            Log.d("MIME TYPE", mimeType.toString())
            val mediaCodec = MediaCodec.createEncoderByType(mimeType!!)
            mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
            //val inputSurface = mediaCodec.createInputSurface()
            mediaCodec.start()
            val mediaMuxer = MediaMuxer(outputDescriptor, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
            val bufferInfo = MediaCodec.BufferInfo()
            var isEOS = false
            val timeoutUs = 10000L
            while (!isEOS) {
                val inputBufferIndex = mediaCodec.dequeueInputBuffer(timeoutUs)
                if (inputBufferIndex >= 0) {
                    val inputBuffer = mediaCodec.getInputBuffer(inputBufferIndex)
                    val sampleSize = mediaExtractor.readSampleData(inputBuffer!!, 0)
                    if (sampleSize < 0) {
                        mediaCodec.queueInputBuffer(inputBufferIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM)
                        isEOS = true
                    } else {
                        mediaCodec.queueInputBuffer(inputBufferIndex, 0, sampleSize, mediaExtractor.sampleTime, 0)
                        mediaExtractor.advance()
                    }
                }
                val outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, timeoutUs)
                if (outputBufferIndex >= 0) {
                    val outputBuffer = mediaCodec.getOutputBuffer(outputBufferIndex)
                    outputBuffer?.let {
                        val bufferData = ByteArray(bufferInfo.size)
                        it.get(bufferData)
                        val compressedSize = bufferData.size * compressionPercentage / 100
                        val compressedBuffer = ByteBuffer.allocate(compressedSize)
                        compressedBuffer.put(bufferData, 0, compressedSize)
                        Log.d("TRACK INDEX", trackIndex.toString())
                        //next row makes an error trackIndex is invalid (0)
                        mediaMuxer.writeSampleData(trackIndex, compressedBuffer, bufferInfo)
                    }
                    mediaCodec.releaseOutputBuffer(outputBufferIndex, false)
                }
                if (bufferInfo.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM != 0) {
                    isEOS = true
                }
            }
            mediaMuxer.stop()
            mediaMuxer.release()
            mediaExtractor.release()
            mediaCodec.stop()
            mediaCodec.release()

        }

    }

    private fun selectTrack(extractor: MediaExtractor, audio: Boolean): Int {
        val numTracks = extractor.trackCount
        Log.d("NUMTRACKS", numTracks.toString())
        for (i in 0 until numTracks) {
            val format = extractor.getTrackFormat(i)
            val mime = format.getString(MediaFormat.KEY_MIME)
            if (audio) {
                if (mime.let{it!!.startsWith("audio/")}) {
                    Log.d("TRACK $i", "got audiotrack")
                    return i
                }
            } else {
                if (mime.let{it!!.startsWith("video/")}) {
                    Log.d("TRACK $i", "got videotrack")
                    return i
                }
            }
        }
        return -1
    }
问题回答




相关问题
SOS with the development of a job application with firabase

I want to create an application where users can register and create their resumes. Which firebase tool to use to store job data, what to show to other users. freelance will also be created in the ...

How to solve Admobi error cannot find symbol?

In my Gradel build I use ` dependencies {implementation com.google.android.gms:play-services-ads:20.4.0 implementation com.google.firebase:firebase-auth:21.0.3 ...

TextView going out of bound in grid layout

Two text view is not visible some ways to fix it is giving end margin, remove the icon or wrap it with frame layout. I m not able to understand what is causing this issue and which is the better way ...

热门标签