gpt4 book ai didi

Android 相机视频为空白,带有白线

转载 作者:行者123 更新时间:2023-12-04 13:29:59 34 4
gpt4 key购买 nike

我已经使用 MediaRecorder 实现了 Android 的 Camera2 API从相机录制视频。相机预览工作正常,视频文件也成功创建,没有任何错误。但是,输出视频是空白的并且有白色闪烁的线条。
这是我得到的输出视频。
Video output from MediaRecorder
准备代码MediaRecorder

val surface = MediaCodec.createPersistentInputSurface()
surface.release()

val outputFile = createFile(applicationContext,"mp4")
val mMediaRecorder = MediaRecorder()
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC)
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE)
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
mMediaRecorder.setOutputFile(outputFile.absolutePath)
mMediaRecorder.setVideoEncodingBitRate(10_000_000)
mMediaRecorder.setVideoFrameRate(30)
mMediaRecorder.setVideoSize(matchedResolution.width, matchedResolution.height)
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
mMediaRecorder.setInputSurface(surface)
创建 session 。
val previewSurface = surfaceView2.holder.surface

val captureCallbackVideo =
object : CameraCaptureSession.StateCallback() {
override fun onConfigureFailed(session: CameraCaptureSession) {}
override fun onConfigured(session: CameraCaptureSession) {
// session configured
val previewRequestBuilder =
cameraDevice.createCaptureRequest(TEMPLATE_RECORD)
.apply {
addTarget(previewSurface)
addTarget(surface)
set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, Range(30,30))
}
session.setRepeatingRequest(
previewRequestBuilder.build(),
null,
Handler { true }
)
}
}

cameraDevice.createCaptureSession(mutableListOf(previewSurface, surface), captureCallbackVideo, Handler { true })
用于开始录制
mMediaRecorder.prepare()
mMediaRecorder.start()
为了停止
mMediaRecorder.stop()
mMediaRecorder.release()

最佳答案

我已经想通了。问题出在 MediaRecorder 的输入表面上。 .显然,谷歌的代码camera2的方式在 GitHub 上设置 Surface 是不对的对于 MediaRecorder正确。
解决方案是我们不应该提供 Surface作为 MediaRecorder 的输入,但 prepare MediaRecorder没有 Surface然后使用其内部Surface对于我们的CaptureRequest

mMediaRecorder = MediaRecorder()
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE)
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
mMediaRecorder.setVideoEncodingBitRate(10_000_000)
//Uncomment on physical device, comment on emulator
//mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
mMediaRecorder.setVideoSize(matchedResolution.width, matchedResolution.height)
mMediaRecorder.setVideoFrameRate(30)
mMediaRecorder.setOutputFile(outputFile.absolutePath)
try {
mMediaRecorder.prepare()
} catch (e: java.lang.Exception) {
e.printStackTrace()
return
}
用于创建 session
val previewSurface = surfaceView2.holder.surface

val captureCallbackVideo =
object : CameraCaptureSession.StateCallback() {
override fun onConfigureFailed(session: CameraCaptureSession) {}
override fun onConfigured(session: CameraCaptureSession) {
// session configured
val previewRequestBuilder =
cameraDevice.createCaptureRequest(TEMPLATE_RECORD)
.apply {
addTarget(previewSurface)
addTarget(mMediaRecorder.surface)
set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, Range(30,30))
}
session.setRepeatingRequest(
previewRequestBuilder.build(),
null,
Handler { true }
)
}
}

cameraDevice.createCaptureSession(mutableListOf(previewSurface, mMediaRecorder.surface), captureCallbackVideo, Handler { true })
开始录制:
mMediaRecorder.start()
停止录制:
mMediaRecorder.stop()
mMediaRecorder.release()

关于Android 相机视频为空白,带有白线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65594342/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com