一、写入文件
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