关于 bevy-capture
bevy-capture 是面向 Bevy 0.19 开发者的摄录技能,适用于制作游戏、过场动画或渲染测试素材。它讲解无头摄像机捕获、编码器选择及常见录制问题。
主要功能
- 01无头 Camera2d 和 Camera3d 捕获
- 02输出 MP4、GIF 和 PNG 序列
- 03选择 OpenH264 或 ffmpeg 编码器
- 04确定性批量捕获指南
- 05排查编码器与输出问题
适用场景
- 01录制游戏或过场动画
- 02捕获渲染测试证据
- 03为离线或流式录制选择编码器
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ chrisgliddon/ bevy-skills --skill bevy-capture安装后在 Claude Code 中输入/bevy-capture确认已加载。
附带文件references/compatibility.mdreferences/encoders.md
详细介绍
bevy-capture — 录制 Bevy 0.19 摄像机
何时使用此技能
- 录制确定性的游戏玩法、电影片段或渲染测试证据。
- 将无头
Camera2d/Camera3d捕获为 MP4、GIF 或编号 PNG。 - 在进程内 OpenH264、ffmpeg 和无损帧输出之间进行选择。
- 排查缺少编码器模块、输出为空或录制未完成刷新的问题。
bevy_capture = "0.6" 原生面向 Bevy 0.19。不要沿用适用于 Bevy 旧版本的
0.4.x,也不要使用旧版 CameraTargetHeadless API。
标准模式
[dependencies]
bevy = "0.19"
bevy_capture = { version = "0.6", features = ["mp4_openh264"] }
use bevy::{
app::{RunMode, ScheduleRunnerPlugin},
camera::RenderTarget,
prelude::*,
render::RenderPlugin,
winit::WinitPlugin,
};
use bevy_capture::{
encoder::mp4_openh264::Mp4Openh264Encoder,
Capture, CaptureBundle, CapturePlugin, RenderTargetHeadless,
};
use std::fs::{self, File};
const WIDTH: u16 = 1280;
const HEIGHT: u16 = 720;
fn main() -> AppExit {
App::new()
.add_plugins((
DefaultPlugins.build().disable::<WinitPlugin>().set(RenderPlugin {
synchronous_pipeline_compilation: true,
..default()
}),
ScheduleRunnerPlugin {
run_mode: RunMode::Loop { wait: None },
},
CapturePlugin,
))
.add_systems(Startup, setup)
.add_systems(Update, record)
.run()
}
fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
commands.spawn((
Camera2d,
RenderTarget::target_headless(WIDTH.into(), HEIGHT.into(), &mut images),
CaptureBundle::default(),
));
}
fn record(
mut captures: Query<&mut Capture>,
mut waited_for_pipeline: Local<bool>,
mut frame: Local<u32>,
) {
// Allow the render pipeline one frame to become ready.
if !*waited_for_pipeline {
*waited_for_pipeline = true;
return;
}
let Ok(mut capture) = captures.single_mut() else { return };
if !capture.is_capturing() && *frame == 0 {
fs::create_dir_all("captures").expect("create captures directory");
let encoder = Mp4Openh264Encoder::new(
File::create("captures/run.mp4").expect("create output"),
WIDTH,
HEIGHT,
)
.expect("initialize OpenH264");
capture.start(encoder);
}
*frame += 1;
if *frame == 300 {
capture.stop(); // drops the encoder and finalizes the MP4
}
}
编码器选择
| 编码器 | 功能 | 外部程序 | 最适用途 |
|---|---|---|---|
Mp4Openh264Encoder | mp4_openh264 | 否 | 进程内 H.264 |
Mp4FfmpegCliEncoder | mp4_ffmpeg_cli | ffmpeg | 短片段/离线片段 |
Mp4FfmpegCliPipeEncoder | mp4_ffmpeg_cli_pipe | ffmpeg | 长时间流式捕获 |
GifEncoder | gif | 否 | 小型预览 |
FramesEncoder | 无 | 否 | 无损 PNG 序列 |
选择生产环境编解码器或分发模式前,请参阅编码器详情。
注意事项
RenderTargetHeadless为RenderTarget实现;请调用RenderTarget::target_headless(...)。Camera上的旧辅助方法已过时。- 将
Camera2d/Camera3d、无头RenderTarget和CaptureBundle挂载到同一实体。 - 至少等待一帧再开始。同步编译管线可提升批量捕获的确定性,但可能会延长启动时间。
- 编码器模块受功能门控。请仅启用你导入的功能。
- 调用
Capture::stop()或丢弃Capture会完成编码器的收尾工作。进程突然终止可能导致 MP4 或 GIF 无效。 - ffmpeg 编码器要求
PATH中存在ffmpeg;构造函数会返回Result。 - 确保编码器尺寸与无头目标匹配。OpenH264 使用
u16。 - 浏览器 WASM 没有常规文件系统或子进程。请明确设计 JS 下载/流式传输路径,而不要假定这些编码器可以正常工作。
另请参阅
bevy-cameras— 摄像机和渲染目标。bevy-cargo-features— 功能选择。bevy-wasm-webgpu— 浏览器限制。- 编码器对比。
- 0.19 兼容性。
评论
支持 Markdown