c 从avi 视频中提取图片

avi 视频的视频流编码必须是jpeg,或者mjpg

直接用摄像头录取的视频都是这两种格式,不能用ffmpeg转成avi的视频。

复制代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <string.h>
#include <sys/mman.h>
static int zz=1;

int main(void){
	
	struct strf{
		unsigned char id[4];             //块ID,固定为strf
		unsigned int size;               //块大小,等于struct avi_strf_chunk去掉id和size的大小
		unsigned int size1;              //size1含义和值同size一样
		unsigned int width;              //视频主窗口宽度(单位:像素)
		unsigned int height;             //视频主窗口高度(单位:像素)
		unsigned short planes;           //始终为1
		unsigned short bitcount;         //每个像素占的位数,只能是1、4、8、16、24和32中的一个
		unsigned char compression[4];    //视频流编码格式,如JPEG、MJPG等
		unsigned int image_size;         //视频图像大小,等于width * height * bitcount / 8
		unsigned int x_pixels_per_meter; //显示设备的水平分辨率,设为0即可
		unsigned int y_pixels_per_meter; //显示设备的垂直分辨率,设为0即可
		unsigned int num_colors;         //含义不清楚,设为0即可
		unsigned int imp_colors;         //含义不清楚,设为0即可
	}str;
	FILE *f=fopen("/home/wzpc/Videos/tra_mjpg.avi","rb");            //必须是JPEG,MJPG格式的avi
	if(f==NULL){
		puts("file error");
		exit(-1);
	}
	fseek(f,0,SEEK_END);
	int fsize=ftell(f);
	fseek(f,0,SEEK_SET);
	
	int fd=fileno(f);
	char *m=mmap(NULL,fsize,PROT_READ,MAP_SHARED,fd,0);
	
	
	
	for(int t=0;t<fsize;t++){
		if((m[t]=='s')&&(m[t+1]=='t')&&(m[t+2]=='r')&&(m[t+3]=='f')){
			
			memcpy(&str,&m[t],sizeof(str));
			printf("%d\n",str.bitcount);
			printf("%s\n",str.compression);
			printf("%d*%d\n",str.width,str.height);
			
			char r[]={'M','J','P','G'};                 //avi 编码必须是jpeg,mjpg
			char r1[]={'J','P','E','G'};
			int bj=memcmp(str.compression,r,4);
			int bj1=memcmp(str.compression,r1,4);
            if((bj==0)||(bj1==0))
			{
			   zz=0;
			}
		    
		}
	}
	
	if(zz!=0){
		puts("no zc");
		exit(-1);
	}
	
	for(int t=0;t<fsize;t++){
		if((m[t]=='0')&&(m[t+1]=='0')&&(m[t+2]=='d')&&(m[t+3]=='c')){
		    char file[10]={};
			sprintf(file,"%d",t);
			
			chdir("/home/wzpc/Pictures/pic_avi");    //存储图片的目录
			FILE * fo=fopen(file,"w+b");
			if(fo==NULL){
				puts("fo error");
				exit(-1);
			}
			
			int k;
			memcpy(&k,&m[t+4],4);
			fwrite(&m[t+8],k,1,fo);                //直接从mmap中读数据到文件
			fclose(fo);
		
		}
  }
	
	munmap(m,fsize);
	return 0;
}
相关推荐
每次的天空22 分钟前
Android-自定义View的实战学习总结
android·学习·kotlin·音视频
苦夏木禾23 分钟前
js请求避免缓存的三种方式
开发语言·javascript·缓存
开-悟29 分钟前
嵌入式编程-使用AI查找BUG的启发
c语言·人工智能·嵌入式硬件·bug
超级土豆粉31 分钟前
Turndown.js: 优雅地将 HTML 转换为 Markdown
开发语言·javascript·html
wei_shuo2 小时前
飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
java·开发语言·飞算javaai
熊猫钓鱼>_>2 小时前
用Python解锁图像处理之力:从基础到智能应用的深度探索
开发语言·图像处理·python
GO兔2 小时前
开篇:GORM入门——Go语言的ORM王者
开发语言·后端·golang·go
好开心啊没烦恼2 小时前
Python 数据分析:numpy,抽提,整数数组索引与基本索引扩展(元组传参)。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy·pandas
爱分享的飘哥2 小时前
【V6.0 - 听觉篇】当AI学会“听”:用声音特征捕捉视频的“情绪爽点”
人工智能·音视频
future14123 小时前
C#学习日记
开发语言·学习·c#