HowTo

使用 CameraX 1.5 进行高速拍摄和慢动作视频录制

阅读用时:6 分钟
查看 Charles Munger 的个人资料
Leo Huang 软件工程师

清晰捕捉快速移动的动作是现代相机应用的一项关键功能。这是通过高速拍摄实现的,即以 120 或 240 fps 等速率获取帧的过程。这种高保真捕捉技术可用于两种不同的用途:创建高帧速率视频,以便进行详细的逐帧分析;或生成慢动作视频,让动作在屏幕上以极具张力的方式展开。

以前,使用 Camera2 API 实现这些功能需要更多手动操作。现在,借助 CameraX 1.5 中全新的高速 API,整个流程得到了简化,让您能够灵活地制作真正的高帧速率视频或可直接播放的慢动作片段。本文将向您介绍如何掌握这两种方法。如果您刚开始接触 CameraX,可以参阅 CameraX 概览,快速了解相关信息。


慢动作背后的原理

慢动作的基本原理是以远高于播放帧速率的帧速率拍摄视频。例如,如果您以每秒帧数 (fps) 为 120 的速度录制一个时长为 1 秒的事件,然后以标准的 30 fps 播放该录制内容,则视频播放时长将为 4 秒。这种时间“拉伸”会产生戏剧性的慢动作效果,让您能够看到肉眼无法捕捉的快速细节。

为确保最终输出的视频流畅,通常应以至少 30 fps 的帧率进行渲染。这意味着,若要制作 4 倍慢动作视频,原始拍摄帧速率必须至少为 120 fps(120 拍摄 fps ÷ 4 = 30 播放 fps)。

拍摄高帧速率视频片段后,您可以通过以下两种主要方式达到预期效果:

  • 播放器处理的慢动作(高帧速率视频):高速录制(例如 120 FPS)会直接保存为高帧速率视频文件。然后,视频播放器负责降低播放速度。这样一来,用户就可以灵活地在正常播放和慢动作播放之间切换。
  • 可直接播放的慢动作视频(重新编码的视频):高速视频流经过处理并重新编码为具有标准帧速率(例如 30 fps)的文件。慢动作效果通过调整帧时间戳“嵌入”到视频中。生成的视频可在任何标准视频播放器中以慢动作播放,无需特殊处理。虽然视频默认以慢动作播放,但视频播放器仍可提供播放速度控件,以便用户提高速度并以原始速度观看视频。

CameraX API 简化了这一过程,为您提供了一种统一的方式来选择所需的方法,如下所示。


全新高速视频 API

新的 CameraX 解决方案基于两个主要组件构建:

  • Recorder#getHighSpeedVideoCapabilities(CameraInfo):此方法可让您检查相机是否可以高速录制,如果可以,则检查支持哪些分辨率(Quality 对象)。
  • HighSpeedVideoSessionConfig:这是一个特殊的配置对象,可将 VideoCapture 和 Preview 用例组合在一起,指示 CameraX 创建统一的高速相机拍摄会话。请注意,虽然 VideoCapture 流将以配置的高帧速率运行,但相机系统通常会将 Preview 流限制为至少 30 FPS 的标准帧速率,以确保屏幕上显示流畅。

使用入门

在开始之前,请确保您已将必要的 CameraX 依赖项添加到应用的 build.gradle.kts 文件中。您需要 camera-video 工件以及核心 CameraX 库。

// build.gradle.kts (Module: app)

dependencies {

    val camerax_version = "1.5.1"


    implementation("androidx.camera:camera-core:$camerax_version")

    implementation("androidx.camera:camera-camera2:$camerax_version")

    implementation("androidx.camera:camera-lifecycle:$camerax_version")

    implementation("androidx.camera:camera-video:$camerax_version")

    implementation("androidx.camera:camera-view:$camerax_version")

}

关于实验性 API 的说明

请务必注意,高速录制 API 目前处于实验阶段。这意味着,它们在未来版本中可能会发生变化。如需使用这些 API,您必须通过向代码中添加以下注解来选择启用:

@kotlin.OptIn(ExperimentalSessionConfig::class, ExperimentalHighSpeedVideo::class)

实现

这两种结果的实现都从相同的设置步骤开始。只需调整一个设置,即可在高帧率视频和慢动作视频之间切换。

1. 设置高速拍摄

首先,无论您的目标是什么,都需要获取 ProcessCameraProvider、检查设备功能并创建用例。

以下代码块展示了挂起函数中的完整设置流程。您可以从协程作用域(例如 lifecycleScope.launch)调用此函数。

// Add the OptIn annotation at the top of your function or class

@kotlin.OptIn(ExperimentalSessionConfig::class, ExperimentalHighSpeedVideo::class)

private suspend fun setupCamera() {

    // Asynchronously get the CameraProvider

    val cameraProvider = ProcessCameraProvider.awaitInstance(this)



    // -- CHECK CAPABILITIES --

    val cameraInfo = cameraProvider.getCameraInfo(CameraSelector.DEFAULT_BACK_CAMERA)

    val videoCapabilities = Recorder.getHighSpeedVideoCapabilities(cameraInfo)

    if (videoCapabilities == null) {

        // This camera device does not support high-speed video.

        return

    }




    // -- CREATE USE CASES --

    val preview = Preview.Builder().build()    


    // You can create a Recorder with default settings.

    // CameraX will automatically select a suitable quality.

    val recorder = Recorder.Builder().build()


    // Alternatively, to use a specific resolution, you can configure the
    // Recorder with a QualitySelector. This is useful if your app has
    // specific resolution requirements or you want to offer user
    // preferences. 

    // To use a specific quality, you can uncomment the following lines.

    // Get the list of qualities supported for high-speed video. 

    // val supportedQualities = videoCapabilities.getSupportedQualities(DynamicRange.SDR)

    // Build the Recorder using the quality from the supported list.

    // val recorderWithQuality = Recorder.Builder()

    //     .setQualitySelector(QualitySelector.from(supportedQualities.first()))

    //     .build()



    // Create the VideoCapture use case, using either recorder or recorderWithQuality

    val videoCapture = VideoCapture.withOutput(recorder)

    // Now you are ready to configure the session for your desired output...

}

2. 选择输出

现在,您可以决定要制作哪种类型的视频。此代码将在上面显示的 setupCamera() suspend 函数中运行。

选项 A:制作高帧率视频

如果您希望最终文件具有高帧速率(例如 120fps 视频),请选择此选项。

// Create a builder for the high-speed session

val sessionConfigBuilder = HighSpeedVideoSessionConfig.Builder(videoCapture)

    .setPreview(preview)


// Query and apply a supported frame rate. Common supported frame rates include 120 and 240 fps.

val supportedFrameRateRanges =

    cameraInfo.getSupportedFrameRateRanges(sessionConfigBuilder.build())


sessionConfigBuilder.setFrameRateRange(supportedFrameRateRanges.first())

选项 B:创建可直接播放的慢动作视频

如果您希望视频在任何标准视频播放器中自动以慢动作播放,请选择此选项。

// Create a builder for the high-speed session

val sessionConfigBuilder = HighSpeedVideoSessionConfig.Builder(videoCapture)

    .setPreview(preview)



// This is the key: enable automatic slow-motion!

sessionConfigBuilder.setSlowMotionEnabled(true)



// Query and apply a supported frame rate. Common supported frame rates include 120, 240, and 480 fps.

val supportedFrameRateRanges =

   cameraInfo.getSupportedFrameRateRanges(sessionConfigBuilder.build())

sessionConfigBuilder.setFrameRateRange(supportedFrameRateRanges.first())

这个单一标志是创建可播放的慢动作视频的关键。当 setSlowMotionEnabled 为 true 时,CameraX 会处理高速视频流,并将其保存为标准的 30 fps 视频文件。慢动作速度由拍摄帧速率与此标准播放速度的比率决定。

例如:

  • 以 120 fps 的帧率录制视频,会生成以 1/4 倍速播放的视频 (120 ÷ 30 = 4)。
  • 以 240 fps 的帧率录制视频,会生成以 1/8 倍速播放的视频 (240 ÷ 30 = 8)。

综合应用:录制视频

配置好 HighSpeedVideoSessionConfig 并将其绑定到生命周期后,最后一步就是开始录制。准备输出选项、开始录制和处理视频事件的过程与标准视频拍摄的过程相同。

本文重点介绍高速配置,因此我们不会详细介绍录制过程。如需查看从准备 FileOutputOptions 或 MediaStoreOutputOptions 对象到处理 VideoRecordEvent 回调等所有内容的综合指南,请参阅 VideoCapture 文档。

// Bind the session config to the lifecycle

cameraProvider.bindToLifecycle(

    this as LifecycleOwner,

    CameraSelector.DEFAULT_BACK_CAMERA,

    sessionConfigBuilder.build() // Bind the config object from Option A or B

)



// Start the recording using the VideoCapture use case

val recording = videoCapture.output

    .prepareRecording(context, outputOptions) // See docs for creating outputOptions

    .start(ContextCompat.getMainExecutor(context)) { recordEvent ->

        // Handle recording events (e.g., Start, Pause, Finalize)

    }

Google 相册对慢动作视频的支持

在 CameraX 中启用 setSlowMotionEnabled(true) 后,生成的视频文件可立即被标准视频播放器和相册应用识别并以慢动作播放。当拍摄帧速率为 120、240、360、480 或 960fps 时,Google 相册会为这些慢动作视频提供增强功能:

  • 在缩略图中显示独特的界面:在 Google 相册媒体库中,慢动作视频会显示特定的界面元素,以便与普通视频区分开来。
normal.png
slowmotion.png
常规视频缩略图慢动作视频缩略图
  • 播放期间可调整速度片段:播放慢动作视频时,Google 相册会提供控件,让用户可以调整视频的哪些部分以慢速播放,哪些部分以正常速度播放,从而赋予用户创意控制权。然后,您可以使用分享按钮将编辑后的视频导出为新的视频文件,并保留您定义的慢动作片段。
normal2.png
slowmotion2.png
常规视频播放慢动作视频
播放,带有编辑
控件

关于设备支持的说明

CameraX 的高速 API 依赖于底层的 Android CamcorderProfile 系统来确定设备支持的高速分辨率和帧速率。CamcorderProfiles 由 Android 兼容性测试套件 (CTS) 进行验证,这意味着您可以确信设备报告的视频录制功能是准确的。

这意味着,即使设备能够使用其内置相机应用录制慢动作视频,也无法保证 CameraX 高速 API 能够正常运行。出现这种差异的原因是,设备制造商负责在其设备的固件中填充 CamcorderProfile 条目,有时必要的 CamcorderProfile.QUALITY_HIGH_SPEED_1080P 和 CamcorderProfile.QUALITY_HIGH_SPEED_720P 等高速配置文件未包含在内。如果缺少这些配置文件,Recorder.getHighSpeedVideoCapabilities() 将返回 null。

因此,请务必始终使用 Recorder.getHighSpeedVideoCapabilities() 以编程方式检查支持的功能,因为这是确保在不同设备上提供一致体验的最可靠方法。如果您尝试在 Recorder.getHighSpeedVideoCapabilities() 返回 null 的设备上绑定 HighSpeedVideoSessionConfig,操作将失败并返回 IllegalArgumentException。您可以确认 Google Pixel 设备是否支持这些高速配置文件,因为这些设备始终包含这些配置文件。此外,其他制造商的各种设备(例如 Motorola Edge 30、OPPO Find N2 Flip 和 Sony Xperia 1 V)也支持高速视频功能。


总结

CameraX 高速视频 API 既强大又灵活。无论您是需要真正的高帧速率视频来进行技术分析,还是想为应用添加电影级的慢动作特效,HighSpeedVideoSessionConfig 都能提供统一且简单的解决方案。通过了解 setSlowMotionEnabled 标志的作用,您可以轻松支持这两种用例,并为用户提供更多创意控制权。

继续阅读