一、寫入文件
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、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 -
三、MP4相關命令
1、將一個 .265 的純 HEVC 碼流文件轉換并封裝成一個標準的 .mp4 視頻文件(無音頻)
ffmpeg -i E:/output.265 -c:v libx265 -c:a copy E:/output.mp4
2、查看Mp4信息
查看部分信息
ffprobe -v quiet -show_entries stream=width,height,codec_name,rotate -show_entries format_tags=rotate -print_format flat your_video.mp4
示例結果:
streams.stream.0.width=1920
streams.stream.0.height=1080
streams.stream.0.codec_name=h264
streams.stream.0.rotate=90
format_tags.rotate=90
查看詳細信息
ffprobe -v quiet -print_format json -show_streams -show_format E:/output.mp4
示例結果,是一個json:
{"streams": [{"index": 0,"codec_name": "hevc","codec_long_name": "H.265 / HEVC (High Efficiency Video Coding)","profile": "Main","codec_type": "video","codec_tag_string": "hev1","codec_tag": "0x31766568","width": 1280,"height": 720,"coded_width": 1280,"coded_height": 720,"closed_captions": 0,"film_grain": 0,"has_b_frames": 2,"pix_fmt": "yuv420p","level": 93,"color_range": "tv","color_space": "smpte170m","color_transfer": "smpte170m","color_primaries": "bt470bg","chroma_location": "left","field_order": "progressive","refs": 1,"view_ids_available": "","view_pos_available": "","id": "0x1","r_frame_rate": "25/1","avg_frame_rate": "25/1","time_base": "1/12800","start_pts": 0,"start_time": "0.000000","duration_ts": 64000,"duration": "5.000000","bit_rate": "39062","nb_frames": "125","extradata_size": 2488,"disposition": {"default": 1,"dub": 0,"original": 0,"comment": 0,"lyrics": 0,"karaoke": 0,"forced": 0,"hearing_impaired": 0,"visual_impaired": 0,"clean_effects": 0,"attached_pic": 0,"timed_thumbnails": 0,"non_diegetic": 0,"captions": 0,"descriptions": 0,"metadata": 0,"dependent": 0,"still_image": 0,"multilayer": 0},"tags": {"language": "und","handler_name": "VideoHandler","vendor_id": "[0][0][0][0]","encoder": "Lavc61.26.100 libx265"}}],"format": {"filename": "E:/output.mp4","nb_streams": 1,"nb_programs": 0,"nb_stream_groups": 0,"format_name": "mov,mp4,m4a,3gp,3g2,mj2","format_long_name": "QuickTime / MOV","start_time": "0.000000","duration": "5.000000","size": "29200","bit_rate": "46720","probe_score": 100,"tags": {"major_brand": "isom","minor_version": "512","compatible_brands": "isomiso2mp41","encoder": "Lavf61.9.100"}}
}
3、ffmpeg查看Mp4錯誤信息
ffmpeg -v error -i E:/output.mp4 -f null -
4、ffmpeg強制旋轉MP4視頻90度
ffmpeg -i E:/output.mp4 -vf "rotate=90*PI/180" -c:a copy E:/output_rotated.mp4