• c 从avi 视频中提取图片


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

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

    1. #include <stdio.h>
    2. #include <sys/types.h>
    3. #include <sys/stat.h>
    4. #include <fcntl.h>
    5. #include <stdlib.h>
    6. #include <unistd.h>
    7. #include <sys/ioctl.h>
    8. #include <linux/videodev2.h>
    9. #include <string.h>
    10. #include <sys/mman.h>
    11. static int zz=1;
    12. int main(void){
    13. struct strf{
    14. unsigned char id[4]; //块ID,固定为strf
    15. unsigned int size; //块大小,等于struct avi_strf_chunk去掉id和size的大小
    16. unsigned int size1; //size1含义和值同size一样
    17. unsigned int width; //视频主窗口宽度(单位:像素)
    18. unsigned int height; //视频主窗口高度(单位:像素)
    19. unsigned short planes; //始终为1
    20. unsigned short bitcount; //每个像素占的位数,只能是148162432中的一个
    21. unsigned char compression[4]; //视频流编码格式,如JPEG、MJPG等
    22. unsigned int image_size; //视频图像大小,等于width * height * bitcount / 8
    23. unsigned int x_pixels_per_meter; //显示设备的水平分辨率,设为0即可
    24. unsigned int y_pixels_per_meter; //显示设备的垂直分辨率,设为0即可
    25. unsigned int num_colors; //含义不清楚,设为0即可
    26. unsigned int imp_colors; //含义不清楚,设为0即可
    27. }str;
    28. FILE *f=fopen("/home/wzpc/Videos/tra_mjpg.avi","rb"); //必须是JPEG,MJPG格式的avi
    29. if(f==NULL){
    30. puts("file error");
    31. exit(-1);
    32. }
    33. fseek(f,0,SEEK_END);
    34. int fsize=ftell(f);
    35. fseek(f,0,SEEK_SET);
    36. int fd=fileno(f);
    37. char *m=mmap(NULL,fsize,PROT_READ,MAP_SHARED,fd,0);
    38. for(int t=0;t<fsize;t++){
    39. if((m[t]=='s')&&(m[t+1]=='t')&&(m[t+2]=='r')&&(m[t+3]=='f')){
    40. memcpy(&str,&m[t],sizeof(str));
    41. printf("%d\n",str.bitcount);
    42. printf("%s\n",str.compression);
    43. printf("%d*%d\n",str.width,str.height);
    44. char r[]={'M','J','P','G'}; //avi 编码必须是jpeg,mjpg
    45. char r1[]={'J','P','E','G'};
    46. int bj=memcmp(str.compression,r,4);
    47. int bj1=memcmp(str.compression,r1,4);
    48. if((bj==0)||(bj1==0))
    49. {
    50. zz=0;
    51. }
    52. }
    53. }
    54. if(zz!=0){
    55. puts("no zc");
    56. exit(-1);
    57. }
    58. for(int t=0;t<fsize;t++){
    59. if((m[t]=='0')&&(m[t+1]=='0')&&(m[t+2]=='d')&&(m[t+3]=='c')){
    60. char file[10]={};
    61. sprintf(file,"%d",t);
    62. chdir("/home/wzpc/Pictures/pic_avi"); //存储图片的目录
    63. FILE * fo=fopen(file,"w+b");
    64. if(fo==NULL){
    65. puts("fo error");
    66. exit(-1);
    67. }
    68. int k;
    69. memcpy(&k,&m[t+4],4);
    70. fwrite(&m[t+8],k,1,fo); //直接从mmap中读数据到文件
    71. fclose(fo);
    72. }
    73. }
    74. munmap(m,fsize);
    75. return 0;
    76. }

     

     

  • 相关阅读:
    记录wisemodel上传失败
    android应用开发基础知识,安卓面试2020
    【线性代数/机器学习】矩阵的奇异值与奇异值分解(SVD)
    第二十二章 alibaba sentinel详解-各种规则的应用
    python设计模式(三)--结构类设计模式
    数据结构 线性表部分代码
    Julia绘图初步:Plots
    java中json转list和map的嵌套
    UML组件图综合指南:设计清晰、可维护的软件系统
    L8.linux命令每日一练 -- 第二章 文件和目录操作命令 -- rm和rmdir命令
  • 原文地址:https://blog.csdn.net/m0_59802969/article/details/134083963