一、写入文件
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/cache
File 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() {
@Override
public 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、adb将文件导出到本地
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
详细日志在命令后面加上-loglevel trace
ffplay -f hevc -i E:/output.265 -loglevel trace
3、ffprobe查看编码信息
可以看到分辨率等信息
ffprobe -show_streams -show_format E:/output.265
4、ffprobe查看编码错误信息
ffprobe -v warning -i E:/output.265
5、ffmpeg查看编码错误信息
ffmpeg -v error -i E:/output.265 -f null -
三、其他命令
1、将一个 .265 的纯 HEVC 码流文件转换并封装成一个标准的 .mp4 视频文件(无音频)
ffmpeg -i E:/output.265 -c:v libx265 -c:a copy E:/output.mp4