一、寫入文件
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
三、后續待拓展
…