简体   繁体   English

camera2 api -> mediaCodec -> mediaMuxer -> mp4 文件

[英]camera2 api -> mediaCodec -> mediaMuxer -> mp4 file

I'm trying to get an mp4 file.我正在尝试获取 mp4 文件。 I shoot video using the camera2 api and can save this as an avc file using MediaCodec.我使用 camera2 api 拍摄视频,并且可以使用 MediaCodec 将其保存为 avc 文件。 But I do not understand how I can redo this code, for encoding into an mp4 file using MediaMuxer.但我不明白如何重做这段代码,以便使用 MediaMuxer 编码成 mp4 文件。 Sorry for my English, this is translated through a translator对不起我的英语,这是通过翻译翻译的

private class EncoderCallback : MediaCodec.Callback() {

      override fun onInputBufferAvailable(codec: MediaCodec, index: Int) {
      }
      override fun onOutputBufferAvailable(
          codec: MediaCodec,
          index2: Int,
          info: MediaCodec.BufferInfo
      ) {
          outPutByteBuffer = mCodec!!.getOutputBuffer(index2)
          val outDate = ByteArray(info.size)
           outPutByteBuffer!![outDate]
          try {
              Log.i("EncoderCallBack", " outDate.length : " + outDate.size)

              outputStream!!.write(outDate, 0, outDate.size) 
          } catch (e: IOException) {
              e.printStackTrace()
          }
          mCodec!!.releaseOutputBuffer(index2, false)
      }

      override fun onError(codec: MediaCodec, e: MediaCodec.CodecException) {
          Log.i("EncoderCallBack", "Error: $e")
      }

      override fun onOutputFormatChanged(codec: MediaCodec, format: MediaFormat) {
          Log.i("EncoderCallBack", "encoder output format changed: $format")
      }

  }

after initializing MediaCodec, I record the video:初始化 MediaCodec 后,我录制视频:

var texture: SurfaceTexture = textureViewOver
        texture.setDefaultBufferSize(320, 240)
     surface = Surface(texture)   
builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
          builder.addTarget(surface)
          builder.addTarget(mEncoderSurface!!)

          mCameraDevice.createCaptureSession(
              mutableListOf(surface, mEncoderSurface),
              object : CameraCaptureSession.StateCallback() {...

the muxer code is missing:缺少复用器代码:

 override fun onOutputBufferAvailable(
      codec: MediaCodec,
      index2: Int,
      info: MediaCodec.BufferInfo
  ) {
      outPutByteBuffer = mCodec!!.getOutputBuffer(index2)
      mediaMuxer?.writeSampleData(trackIndex, outPutByteBuffer, info)
      mCodec!!.releaseOutputBuffer(index2, false)
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android使用mediamuxer mediacodec和mediaextractor(没有mp4parser)合并mp4和aac文件 - Android merging mp4 and aac file using mediamuxer mediacodec and mediaextractor (without mp4parser) 使用MediaMuxer和MediaCodec将wmv视频文件转换为mp4视频文件 - Converting wmv video file to mp4 video file using MediaMuxer and MediaCodec Android Camera2 + MediaCodec + MediaMuxer 使用持久表面? - Android Camera2 + MediaCodec + MediaMuxer using persistent surface? 使用带有 Camera2 API 的 MediaCodec 录制视频 - Recording video using MediaCodec with Camera2 API 使用MediaCodec API编辑MP4元数据 - Edit MP4 metadata using MediaCodec API Android:MediaCodec + MediaMuxer编码的音频MP4无法播放 - Android: MediaCodec+MediaMuxer encoded audio MP4 won't play mp4文件使用mediacodec和mediamuxer时的音频和视频轨道同步问题 - audio and video track synchronization issue when using mediacodec and mediamuxer for mp4 files 为什么新创建的Android Camera2 MP4文件在某些​​播放器上无法播放,而在其他播放器上却无法播放 - Why does a newly created Android Camera2 MP4 File fail to play on some players, but not others 在没有MediaMuxer的情况下将PCM转换为AAC或MP4文件 - Convert PCM to AAC or MP4 file without MediaMuxer 使用MediaMuxer而不是FileOutputStream将帧保存到mp4文件中 - Using MediaMuxer instead of FileOutputStream to save frames into mp4 file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM