当前位置: 首页 > 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.lryc.cn/news/609674.html

相关文章:

  • Android进程基础:Zygote
  • 2025-08-04-零成本搭建 AI 应用!Hugging Face 免费 CPU 资源实战指南
  • Android Telephony 框架与横向支撑层
  • 如何选择一个容易被搜索引擎发现的域名?
  • 计算机网络:详解网络地址的计算步骤
  • 2.4- WPF中非 UI 线程上安全地更新 UI 控件方法
  • JVM学习日记(十六)Day16——性能监控与调优(三)
  • SpringBoot格式化数据库表格字段时间戳
  • vcpkg在vs/vscode下用法
  • 子词分词器(Byte Pair Encoding + WordPiece)
  • 深入解析SmolVLA:VLM与动作专家间的注意力机制交互
  • 深入剖析通用目标跟踪:一项综述
  • [自动化Adapt] 父子事件| 冗余过滤 | SQLite | SQLAlchemy | 会话工厂 | Alembic
  • RLCraft开服踩坑记录
  • 补:《每日AI-人工智能-编程日报》--2025年7月30日
  • AWS 可靠性工程深度实践: 从 Well-Architected 到“零失误”VPC 落地
  • 使用AWS for PHP SDK实现Minio文件上传
  • 音视频学习笔记
  • vue3入门-概览讲解
  • 使用 IntelliJ IDEA + Spring JdbcTemplate 操作 MySQL 指南
  • 基于Java的AI/机器学习库(Smile、Weka、DeepLearning4J)的实用
  • Go语言流式输出技术实现-服务器推送事件(Server-Sent Events, SSE)
  • 【银河麒麟服务器系统】自定义ISO镜像更新内核版本
  • Linux 文件与目录属性管理总结
  • Android 区块链 + CleanArchitecture + MVI 架构实践
  • IDA9.1使用技巧(安装、中文字符串显示、IDA MCP服务器详细部署和MCP API函数修改开发经验)
  • Android工程命令行打包并自动生成签名Apk
  • 服务器突然之间特别卡,什么原因?
  • ffmpeg下载windows教程
  • clickhouse 中文数据的正则匹配