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

安卓264和265编码器回调编码数据写入文件的方法

一、写入文件

1、变量定义

private FileOutputStream m265FileOutputStream = null;
private File m265File = null;
private static final String HEVC_265_FILE_NAME = "output.265"; // 或 .265
private static final String AVC_264_FILE_NAME = "output.264"; // 或 .265

2、初始化

try {// 获取系统临时目录(通常指向应用私有缓存)String tmpDirPath = System.getProperty("java.io.tmpdir"); // 如:/data/data/com.pkg/cacheFile tmpDir = new File(tmpDirPath, "test");if (!tmpDir.exists()) {tmpDir.mkdirs();}m265File = new File(tmpDirPath, isHevc ? HEVC_265_FILE_NAME : AVC_264_FILE_NAME);m265FileOutputStream = new FileOutputStream(m265File);Log.d(TAG, "开始录制" + (isHevc ? ".265" : ".264") + " 文件: " + m265File.getAbsolutePath());} catch (IOException e) {Log.e(TAG, "无法创建" + (isHevc ? ".265" : ".264") + " 输出文件", e);}

3、编码数据写入.265文件或者.264文件

编码数据一般的onOutputBufferAvailable方法中处理

private MediaCodec.Callback mCallback = new MediaCodec.Callback() {@Overridepublic void onOutputBufferAvailable(MediaCodec mediaCodec, int id, MediaCodec.BufferInfo bufferInfo) {.......if (frameLength > 0 && m265FileOutputStream != null) {try {m265FileOutputStream.write(h264Buff, 0, frameLength);Log.d(TAG, "写入" + (isHevc ? ".265" : ".264") + "文件成功,长度:" + frameLength + " 时间戳:" + alTimestamp);} catch (IOException e) {Log.e(TAG, "写入" + (isHevc ? ".265" : ".264") + " 文件失败", e);}}}
}

4、关闭流

if (mMediaCodec != null) {mMediaCodec.stop();if (m265FileOutputStream != null) {try {m265FileOutputStream.close();Log.d(TAG,  (isHevc ? ".265" : ".264") + " 文件保存完成: " + m265File.getAbsolutePath());} catch (IOException e) {Log.e(TAG, "关闭"+(isHevc ? ".265" : ".264") +"文件失败", e);} finally {m265FileOutputStream = null;}}mMediaCodec.release();mMediaCodec = null;}

二、ffplay分析编码数据

1、将文件导出到本地

1、确保已连接到手机,导出文件
265的

adb exec-out run-as com.qukan.qklive cat cache/output.265 > E:/output.265

264的

adb exec-out run-as com.qukan.qklive cat cache/output.264 > E:/output.264

2、ffplay分析编码数据

能看到画面且无报错就是正常的编码数据
265的

ffplay -f hevc -i E:/output.265

264的

ffplay -f h264 -i E:/output.264

示例:

3、查看编码信息

可以看到分辨率等信息

ffprobe -show_streams -show_format E:/output.265

三、后续待拓展

http://www.dtcms.com/a/314009.html

相关文章:

  • C++临时对象:来源与性能优化之道
  • 36.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--缓存Token
  • 【ECCV2024】AdaCLIP:基于混合可学习提示适配 CLIP 的零样本异常检测
  • Spring Security6.3.x使用指南
  • Postman:配置环境变量
  • Linux-Shell脚本基础用法
  • 采购全生命周期管理是什么?
  • 八股——Kafka相关
  • Linux 磁盘管理详解:分区、格式化与挂载全流程指南
  • leetcode_11 盛最多水的容器
  • LeetCode 135:分糖果
  • Go语言 逃 逸 分 析
  • JVM学习专题(四)对象创建过程
  • 【软考中级网络工程师】知识点之 BGP 协议
  • PHP多人实时聊天室源码 简单易用
  • 电力改造的 “加速引擎”:边缘计算网关如何让智能电网升级效率翻倍?
  • Deep learning based descriptor
  • 复现论文《A Fiber Bragg Grating Sensor System for Train Axle Counting》
  • Centos7.9安装Oracle11.2.0.1版本问题处理
  • gRPC C++ 从 0 到 1 → 到线上:**超详细** 环境搭建、编码范式、性能调优与 DevOps 全攻略
  • weapp-tailwindcss 已支持 uni-app x 多端构建
  • 【笔记】ROS1|5 ARP攻击Turtlebot3汉堡Burger并解析移动报文【旧文转载】
  • (2023ICML)BLIP-2:使用冻结图像编码器和大语言模型引导语言-图像预训练
  • Druid学习笔记 02、快速使用Druid的SqlParser解析
  • 【目标检测基础】——yolo学习
  • uniapp基础(四)性能优化
  • BM1684X平台:Qwen-2-5-VL图像/视频识别应用
  • 从医学视角深度解析微软医学 Agent 服务 MAI-DxO
  • 深入解析 Apache Tomcat 配置文件
  • 2025.08.04 移除元素