关闭

用ffmpeg转换图片、视频格式yuv, C语言读取YUV图像

标签: c语言ffmpeg图像读取
830人阅读 评论(0) 收藏 举报
分类:

1.用ffmpeg将视频转换为yuv格式

ffmpeg.exe -i src.avi -c:v rawvideo -pix_fmt yuv420p 1280x720.yuv

输出视频可以用yuvPlayer打开

2.用ffmpeg将jpg,png图像转换为yuv格式

建议图像命名为宽乘高 width*height,乘号用字母”x”。
使用ffmpeg的命令为:

ffmpeg.exe -i 1024x680.jpg -pix_fmt gray8 1024x680_gray8.yuv

ffmpeg.exe -i 1024x680.jpg -pix_fmt yuv420p 1024x680_420p.yuv

转换后的图像可以用软件pYUV查看,格式注意和转换的要一致,比如gray8的color space为黑白BW,subsampling选4:0:0; 而420p的颜色空间选yuv, subsampling选4:2:0

3.用C语言读取yuv图像

首先定义一个yuv图像的结构体

typedef struct yuvImage
    {
        int width;           /* Image width  */
        int height;          /* Image height */
        unsigned char *y;    /* y: data pointer */
        unsigned char *u;    /* u: data pointer */
        unsigned char *v;    /* v: data pointer */
    } YUV_IMAGE;

// read image
FILE*fp = NULL;
fp = fopen("E:/mycode/Datasets/1024x680_gray8.yuv", "rb");

yuv的格式为: y的大小为size, u,v各为size/4;
这里写图片描述

设置yuv的起始位置
y:0
u:size
v: size+size/4

YUV_IMAGE* yuvImage= (YUV_IMAGE*)malloc(sizeof(YUV_IMAGE));
yuvImage->height= 240;
yuvImage->width= 320;
yuvImage->y= (unsigned char*)malloc((size * 3 / 2)*sizeof(unsigned char));
memset(yuvImage->y, 0, size * sizeof(unsigned char));
yuvImage->u= yuvImage->y+ size;
yuvImage->v = yuvImage->u+ size / 4;

读取

buffer = (char *)malloc(size*sizeof(char));
fread(buffer, size, 1, fp);
memcpy(yuvImage->y, buffer, size);
fread(buffer, size/4, 1, fp);
memcpy(yuvImage->u, buffer, size);
fread(buffer, size/4, 1, fp);
memcpy(yuvImage->v, buffer, size);```

另一种格式

// 420p
    unsigned char*y = (unsigned char*)malloc(sizeof(unsigned char) * size);
    memset(y, 0, size);
    unsigned char*u = (unsigned char*)malloc(sizeof(unsigned char) * size/4);
    memset(u, 0, size/4);
    unsigned char*v = (unsigned char*)malloc(sizeof(unsigned char) * 1size/4);
    memset(v, 0, size/4);

    FILE*fp = NULL;
    fp = fopen("E:/mycode/Datasets/1024x680.yuv", "rb");
    fread(y, 1, size, fp);
    fread(u, 1, size/4, fp);
    fread(v, 1, size/4, fp);
    fclose(fp);
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27914次
    • 积分:1173
    • 等级:
    • 排名:千里之外
    • 原创:93篇
    • 转载:2篇
    • 译文:3篇
    • 评论:14条
    文章分类
    最新评论