当前位置: 首页 > news >正文

一个用于记录和存储 H.264 视频帧的工具类

CarPlayPacketDumper 是一个用于记录和存储 CarPlay 数据包(特别是 H.264 视频帧)的工具类。它将接收到的数据帧按顺序写入文件,并提供开始、停止和写入操作。

public class CarPlayPacketDumper {private static final String TAG = "CarPlayPacketDumper";private final String filePath;private FileOutputStream fos; // 文件输出流private FileChannel channel; // 文件通道,提供更高效的文件操作public boolean isRecording = false; // 记录状态标志private final AtomicLong writtenBytes = new AtomicLong(0); // 线程安全的计数器public CarPlayPacketDumper(String filePath) {this.filePath = filePath;}public synchronized void start() throws IOException {Log.d(TAG, "启动H.264顺序录制...");stop(); // 确保先关闭现有资源// 追加模式(false表示覆盖原有文件)fos = new FileOutputStream(filePath, false);channel = fos.getChannel(); // 获取文件通道isRecording = true;writtenBytes.set(0);Log.d(TAG, "已清空并准备写入: " + filePath);}public synchronized void writeFrame(ByteBuffer frame) throws IOException {if (!isRecording) {Log.e(TAG, "当前未在录制状态!");throw new IllegalStateException("Not recording");}final int frameSize = frame.remaining(); // 获取剩余字节数// 调试信息:采样前16字节byte[] sample = new byte[Math.min(16, frameSize)];frame.get(sample); // 读取数据到字节数组Log.d(TAG, String.format("写入帧 [大小:%,d字节] [头16字节:%s]",frameSize,bytesToHex(sample)));frame.rewind(); // 重置position// 关键点:直接顺序写入channel.write(frame);writtenBytes.addAndGet(frameSize);}public synchronized void stop() {if (isRecording) {isRecording = false;Log.d(TAG, "停止录制,总写入大小: " + formatFileSize(writtenBytes.get()));try {if (channel != null) {channel.force(true); // 强制刷盘channel.close();}if (fos != null) fos.close();} catch (IOException e) {Log.e(TAG, "关闭资源失败", e);} finally {channel = null;fos = null;}}}// 辅助方法:字节转十六进制private static String bytesToHex(byte[] bytes) {StringBuilder sb = new StringBuilder();for (byte b : bytes) {sb.append(String.format("%02X ", b));}return sb.toString();}// 辅助方法:格式化文件大小private static String formatFileSize(long bytes) {if (bytes < 1024) return bytes + "B";return String.format("%.2fMB", bytes / (1024.0 * 1024.0));}
}
//使用示例//1.初始化
private final CarPlayPacketDumper dumper = new CarPlayPacketDumper("/storage/emulated/0/Movies/video.h264");//2.开始录制 
dumper.start();//3.写入帧数据
// 在CarPlay视频流处理中的典型用法
public void onVideoFrameReceived(ByteBuffer frame) {if (dumper != null && dumper.isRecording) {try {dumper.writeFrame(frame);} catch (IOException e) {Log.e(TAG, "写入帧失败", e);dumper.stop();}}
}//4.停止录制
dumper.stop();
//5.异常处理:必须处理IOException,调用stop()确保资源释放

http://www.lryc.cn/news/572713.html

相关文章:

  • 【精选】基于SpringBoot的宠物互助服务小程序平台开发 微信小程序宠物互助系统 宠物互助小程序平台设计与实现 支持救助发布+领养申请+交流互动功能
  • 基于微信小程序的美食点餐订餐系统
  • OPENGLPG第九版学习 - 纹理与帧缓存 part1
  • .docx 和 .doc 都是 Word 文档格式的区别
  • el-table复杂表头(多级表头行或列的合并)
  • Mac电脑 窗口分屏管理 Magnet Pro
  • 4、做中学 | 二年级下期 Golang整型和浮点型
  • react扩展
  • Excel批量计算时间差
  • 【笔记】解决部署国产AI Agent 开源项目 MiniMax-M1时 Hugging Face 模型下载缓存占满 C 盘问题:更改缓存位置全流程
  • ElSelect 多选远程搜索选项丢失问题
  • 甘肃安全员A证考试备考题库含答案2025年
  • WIFI原因造成ESP8266不断重启的解决办法
  • 【同声传译】RealtimeSTT:超低延迟语音转文字,支持唤醒词与中译英
  • npm 更新包名,本地导入
  • vue2通过leaflet实现图片点位回显功能
  • Fiddler抓包工具使用技巧:如何结合Charles和Wireshark提升开发调试效率
  • OpenCV C++ 边缘检测与图像分割
  • NY339NY341美光固态闪存NW841NW843
  • 【VUE】某时间某空间占用情况效果展示,vue2+element ui实现。场景:会议室占用、教室占用等。
  • PVE使用ubuntu-cloud-24.img创建虚拟机并制作模板
  • NVIDIA开源Fast-dLLM!解析分块KV缓存与置信度感知并行解码技术
  • 旋转图像C++
  • json.Unmarshal精度丢失问题分析
  • vue3组件式开发示例
  • 大模型与搜索引擎的技术博弈及未来智能范式演进
  • MySQL查询语句的通配符*
  • 组态王工程运行时间显示
  • 【案例拆解】米客方德 SD NAND 在车联网中(有方模块)的应用:破解传统 TF 卡振动脱落与寿命短板
  • 在VTK中捕捉体绘制图像进阶(同步操作)