ffmpeg 把视频解码成jpg

这是一个h264视频解码的小demo,先把h264转成yuv,然后再把yuv转成jpg

#include <iostream>

int Frame2JPG(AVPacket packet, AVFrame* pFrame, unsigned int stream_index,
              int width, int height)
{
    // 输出文件路径
    char out_file[100] = { 0 };
    sprintf(out_file,"%s%d.jpg", "video/", packet.pts);

    // 分配AVFormatContext对象
    AVFormatContext* pFormatCtx = avformat_alloc_context();

    // 设置输出文件格式
    pFormatCtx->oformat = av_guess_format("mjpeg", NULL, NULL);
    // 创建并初始化一个和该url相关的AVIOContext
    if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0)
    {
        return -1;
    }

    // 构建一个新stream
    AVStream* pAVStream = avformat_new_stream(pFormatCtx, 0);
    if (pAVStream == NULL)
    {
        return -1;
    }

    // 设置该stream的信息
    AVCodecContext* pCodecCtx = pAVStream->codec;

    pCodecCtx->codec_id = pFormatCtx->oformat->video_codec;
    pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
    pCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ420P;
    pCodecCtx->width = width;
    pCodecCtx->height = height;
    pCodecCtx->time_base.num = 1;
    pCodecCtx->time_base.den = 25;

    // Begin Output some information
    // av_dump_format(pFormatCtx, 0, out_file, 1);
    // End Output some information

    // 查找解码器
    AVCodec* pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
    if (!pCodec)
    {
        return -1;
    }
    // 设置pCodecCtx的解码器为pCodec
    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
    {
        return -1;
    }

    //Write Header
    int ret = avformat_write_header(pFormatCtx, NULL);
    if (ret < 0)
    {
        return -1;
    }

    int y_size = pCodecCtx->width * pCodecCtx->height;

    //Encode
    // 给AVPacket分配足够大的空间
    AVPacket pkt;
    av_new_packet(&pkt, y_size * 3);

    int got_picture = 0;
    ret = avcodec_encode_video2(pCodecCtx, &pkt, pFrame, &got_picture);
    if (ret < 0)
    {
        return -1;
    }
    if (got_picture == 1)
    {
        //pkt.stream_index = pAVStream->index;
        ret = av_write_frame(pFormatCtx, &pkt);
    }

    av_free_packet(&pkt);

    //Write Trailer
    av_write_trailer(pFormatCtx);

    if (pAVStream)
    {
        avcodec_close(pAVStream->codec);
    }
    avio_close(pFormatCtx->pb);
    avformat_free_context(pFormatCtx);

    return 0;
}

int decode_h264(char *filepath)
{
    AVFormatContext    *pFormatCtx;
    AVCodecContext    *pCodecCtx;
    AVCodec            *pCodec;
    AVFrame    *pFrame, *pFrameYUV;
    AVPacket *packet;
    struct SwsContext *img_convert_ctx;
    uint8_t *out_buffer;

    int    videoindex = -1;
    int y_size;
    int ret, got_picture;

    FILE *fp_yuv = fopen("1.yuv", "wb+");

    av_register_all();
    //avformat_network_init();
    pFormatCtx = avformat_alloc_context();

    if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0)
    {
        printf("Couldn't open input stream.\n");
        return -1;
    }
    if (avformat_find_stream_info(pFormatCtx, NULL) <0)
    {
        printf("Couldn't find stream information.\n");
        return -1;
    }

    for (int i = 0; i < pFormatCtx->nb_streams; i++)
    {
        if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            videoindex = i;
            break;
        }
    }
    if (videoindex == -1)
    {
        printf("Didn't find a video stream.\n");
        return -1;
    }

    pCodecCtx = pFormatCtx->streams[videoindex]->codec;

    // 根据编码器的ID查找FFmpeg的解码器
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if (pCodec == NULL)
    {
        printf("Codec not found.\n");
        return -1;
    }
    // 初始化一个视音频编解码器的AVCodecContext
    if (avcodec_open2(pCodecCtx, pCodec, NULL)<0)
    {
        printf("Could not open codec.\n");
        return -1;
    }

    // 一些内存分配
    packet = (AVPacket *)av_malloc(sizeof(AVPacket));
    pFrame = av_frame_alloc();
    pFrameYUV = av_frame_alloc();
    out_buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
    // 为已经分配空间的结构体AVPicture挂上一段用于保存数据的空间
    // AVFrame/AVPicture有一个data[4]的数据字段,buffer里面存放的只是yuv这样排列的数据,
    // 而经过fill 之后,会把buffer中的yuv分别放到data[0],data[1],data[2]中。
    avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);

    //Output Info-----------------------------
    printf("--------------- File Information ----------------\n");
    av_dump_format(pFormatCtx, 0, filepath, 0);
    printf("-------------------------------------------------\n");

    // 初始化一个SwsContext
    // 参数:源图像的宽,源图像的高,源图像的像素格式,目标图像的宽,目标图像的高,目标图像的像素格式,设定图像拉伸使用的算法
    img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
                                     pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);

    while (av_read_frame(pFormatCtx, packet) >= 0)
    {
        if (packet->stream_index == videoindex)
        {
            // 解码一帧视频数据。输入一个压缩编码的结构体AVPacket,输出一个解码后的结构体AVFrame
            ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
            if (ret < 0)
            {
                printf("Decode Error.\n");
                return -1;
            }
            if (got_picture)
            {
                // 转换像素
                sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
                          pFrameYUV->data, pFrameYUV->linesize);

                y_size = pCodecCtx->width * pCodecCtx->height;
                // 向文件写入一个数据块
                fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv);        //Y
                fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv);  //U
                fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv);  //V

                Frame2JPG(*packet, pFrameYUV,packet->stream_index,pCodecCtx->width, pCodecCtx->height);
                //printf("Succeed to decode 1 frame!\n");
            }
        }
        av_free_packet(packet);
    }

    //flush decoder
    //FIX: Flush Frames remained in Codec
    while (1)
    {
        ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
        if (ret < 0)
            break;
        if (!got_picture)
            break;
        sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
                  pFrameYUV->data, pFrameYUV->linesize);

        int y_size = pCodecCtx->width*pCodecCtx->height;

        fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv);        //Y
        fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv);  //U
        fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv);  //V


        //printf("Flush Decoder: Succeed to decode 1 frame!\n");
    }

    sws_freeContext(img_convert_ctx);
    fclose(fp_yuv);
    av_frame_free(&pFrameYUV);
    av_frame_free(&pFrame);
    avcodec_close(pCodecCtx);
    avformat_close_input(&pFormatCtx);

    return 0;
}

int main()
{
    char filepath[] = "1.mp4";
    decode_h264(filepath);
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ffmpeg是一个广泛使用的开源多媒体处理库,支持多种音视频解码格式。在使用ffmpeg进行JPG解码时,它会将JPG图像转换为YUV格式。YUV是一种用于存储和传输彩色图像的格式,它将亮度(Y)和色度(U和V)分离开来。 首先,ffmpeg会读取JPG图像文件,并将其解析为像素数据。然后,它会根据JPG中的颜色信息,将像素数据转换为YUV格式。Y分量表示图像的亮度信息,U和V分量表示图像的色度信息。 在进行YUV转换时,ffmpeg会根据JPG图像的色彩空间信息进行自适应处理。常见的色彩空间包括RGB和YCbCr,其中YCbCr就是一种YUV的变体。通过将RGB图像转换为YCbCr,可以更有效地压缩图像数据,并减少存储或传输所需的空间和带宽。 因此,当使用ffmpeg解码JPG图像时,它会将输入的JPG转换为YUV格式,并提供Y,U和V三个分量的像素数据。这些像素数据可以根据需要进行进一步的处理或者用于其他操作,如视频编码、图像处理等。 总之,使用ffmpeg解码JPG图像时,它会将图像转换为YUV格式,将亮度和色度信息分离开来,以便于后续的处理和应用。 ### 回答2: FFmpeg是一个开源的多媒体处理工具集,可以实现音视频的编解码、转码、处理等功能。在FFmpeg中,可以通过使用libjpeg库来实现JPG格式的解码JPG是一种常见的图片压缩格式,其编码方式使用了基于离散余弦变换(DCT)的算法。要解码JPG图片到YUV格式,需要以下几个步骤: 1. 打开JPG文件:通过FFmpeg提供的接口,可以打开并读取JPG文件。 2. 解码JPG图像:使用libjpeg库提供的函数,可以将JPG图像解码为RGB(红绿蓝)格式。 3. 转换为YUV格式:在FFmpeg中,可以使用swscale库提供的函数将RGB图像转换为YUV格式。YUV是一种常用的视频颜色空间,其中包括亮度(Y)和色度(U、V)分量。 4. 保存YUV图像:将转换后的YUV图像保存到文件中,或者用于后续的视频处理。 需要注意的是,解码JPG到YUV格式是一个比较复杂的过程,需要对图像编码和颜色空间转换等进行处理。在使用FFmpeg进行解码时,可以针对具体的需求选择不同的设置和参数,以获得所需的解码效果。 总之,使用FFmpeg可以方便地实现JPG图像解码为YUV格式的功能,通过调用FFmpeg提供的接口和库函数,可以完文件的打开、图像的解码和颜色空间的转换等操作。这为后续的视频处理提供了基础。 ### 回答3: ffmpeg是一个开源的多媒体处理工具,可以在命令行中使用。jpg是一种常见的图片格式,而yuv则是一种常见的视频像素格式。 使用ffmpeg解码jpg图片到yuv像素格式的过程可以分为以下几步: 1. 安装ffmpeg:在电脑上安装ffmpeg软件,可以从官方网站或其他途径下载并安装。 2. 打开命令行界面:在电脑上打开命令行界面,可以通过搜索或者运行cmd命令打开。 3. 编写解码命令:在命令行中输入ffmpeg解码命令。命令的基本格式如下所示: `ffmpeg -i input.jpg -pix_fmt yuv420p output.yuv` 这条命令中,`-i input.jpg`表示输入的jpg图片文件,`-pix_fmt yuv420p`表示输出的像素格式为yuv420p,`output.yuv`表示输出的yuv文件名。 4. 执行命令:在命令行中按下回车键执行解码命令。此时,ffmpeg会加载输入的jpg文件,将其解码yuv420p格式,并输出到指定的文件中。 通过以上步骤,我们可以使用ffmpegjpg图片解码为yuv像素格式。这样的操作对于一些视频处理任务或者需要将图片转换为视频的应用场景非常有用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值