#include #include "ps.h" int ReadSourceStream(const char* infile, char **frame_data, int *frame_size) { static FILE *fp = NULL; if (fp == NULL) { // fp = fopen("es_stream.h264", "rb"); fp = fopen(infile, "rb"); if (fp == NULL) { printf("open file es_stream.h264 error\n"); return -1; } } //文件格式为: // 1 2 3 4 n //|----|----|----|----|--------- //| 帧长度 | 数据 do { if (4 != fread(frame_size, 1, 4, fp)) break; *frame_data = (char*)malloc(*frame_size); if (*frame_size != fread(*frame_data, 1, *frame_size, fp)) break; return 0; } while (0); printf("read file es_stream.h264 eof\n"); fclose(fp); fp = NULL; return -2; } int KeyFrame(uint8_t nalu) { int nalType = nalu & 0x1f; if (nalType == 0x07 || nalType == 0x05 || nalType == 0x08) return 1; return 0; } int main0() { const char* infile = "/mnt/f/fiss/test_data/test.h264"; char *frame_data; int frame_size, ps_size; uint32_t ts = 0; FILE *fp = fopen("test.ps", "wb"); while (0 == ReadSourceStream(infile, &frame_data, &frame_size)) { // 文件中只有h264视频,第4个参数固定填1,音频为2 ps_size = TransPSFrame(frame_data, frame_size, KeyFrame((uint8_t)frame_data[4]), 1, ts); fwrite(PSFrameBuffer, 1, ps_size, fp); ts += 3600; free(frame_data); } fclose(fp); // getchar(); return 0; }