#include <windows.h>
#include <iostream>
#include <string>
/* 在指定的文件中查找所有与str内容相同的内容,并将内容在文件的位置记录在arr中。
* @fp 指定查找的文件指针
* @str 要查找的内容
* @strLen 要查找的内容的长度
* @arr 存放位置的数组,要求数组足够大
* @len 两种含义,传入时len表示数组长度,函数结束后len表示数组中有效数据的个数
**/
int getAllContent(FILE* fp, char* str, int strLen, unsigned* arr, unsigned* len)
{
if (!fp || !arr || !len) return -1;
unsigned arrLen = *len;
long pos = 0;
long posEnd = 0;
char* buf = (char*)malloc(sizeof(char) * strLen);;
if (!buf) return -2;
fseek(fp, 0L, SEEK_END);
posEnd = ftell(fp) - strLen;
*len = 0;
int res = 0;
while (pos <= posEnd && *len < arrLen)
{
fseek(fp, pos, SEEK_SET);
res = fread(buf, sizeof(char), strLen, fp);
if (res != strLen) break;
if (memcmp(str, buf, strLen * sizeof(char)) == 0)
{
arr[*len] = pos;
(*len)++;
}
pos++;
}
fseek(fp, 0L, SEEK_SET);
free(buf);
return 0;
}
/*根据arr中保持的各帧的位置读取文件
* @fp 需要读取的文件
* @arr 保持指定内容在文件中的位置的数组
* @len 数组中可用数据的个数
**/
int SendAvcStream(FILE* fp, unsigned* arr, unsigned len)
{
static unsigned char* bufI = NULL;
static unsigned char* buf2 = NULL;
if (!bufI)
{
bufI = (unsigned char*)malloc(640 * 360);//视频的分辨率
if (!bufI) return -1;
}
if (!buf2)
{
buf2 = (unsigned char*)malloc(640 * 360);
if (!buf2) return -1;
}
unsigned i = 1;
int res = 0;
unsigned frameSize = 0;
std::string sFile("");
while (i < len)
{
unsigned size = arr[i] - arr[i - 1];
if (size == 0) { i++; continue; }
res = fread(buf2, 1, size, fp);
if (res != (signed)size) break;
int type = buf2[4] & 0x1f;
printf("frame type is %d\n", type);
if (type == 1)
{
if (frameSize)// get a whole IDR frame which maybe include dual slices
{
// TODO: to do something
sFile = "D:\\AudioPcm\\H264\\N\\" + std::to_string(i) + ".h264";
FILE* fpW = fopen(sFile.c_str(), "ab");
if (!fpW) continue;
fwrite(bufI, 1, frameSize, fpW);
fclose(fpW);
frameSize = 0;
}
// get a whole P frame
// TODO: to do something
}
else if(5 == type || 7 == type || 8 == type) // 7 8 6 5
{
// stroe 7 8 6 5 into an buffer
memcpy(bufI + frameSize, buf2, size);
frameSize += size;
}
i++;
}
fseek(fp, 0L, SEEK_SET);
return 0;
}
int main() {
//测试h264文件逐帧拆分
char cRead[1024] = { 0 };
static int nCnt = 1;
std::string sFile("");
char arrHeader[4] = { 0x00,0x00,0x00,0x01 };//筛查0x00 0x00 0x00 0x01开头的数据
FILE* fp = fopen("D:\\AudioPcm\\H264\\test10.h264", "rb");
if (!fp) return -1;
unsigned arrIndex[100] = { 0 };
unsigned int nLen = 100;
getAllContent(fp, arrHeader, 4, arrIndex, &nLen);
SendAvcStream(fp, arrIndex, nLen);
return 0;
}
c++ H264文件逐帧提取 每帧可单独解码使用
106 浏览量
2024-05-16
14:07:03
上传
评论
收藏 3KB ZIP 举报
冰激凌zz
- 粉丝: 128
- 资源: 6
最新资源
- 基于python后端开发框架
- seata-server 1.4.0和2.0.0
- signal-and-system信号与系统
- career.it.signals-systems信号与系统
- 基于TCP-IP实现的斗地主游戏(ARM-LINUX)C源码-95分以上课程设计
- Audio声卡驱动是一款系统音频处理的声卡驱动
- 面向计算机视觉的基础库,支持Linux、Windows及MacOS平台 提供了众多功能,包括基于PyTorch的通用训练框架等
- 基于LQR实现的车辆轨迹跟踪matlab源码+项目说明+超详细注释(高分项目)
- 视2.css
- Windows 10远程桌面服务配置文件rdpwrap10.0.22621.3593.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈