美文网首页
FFmpeg实现YUV420P转jpg图片

FFmpeg实现YUV420P转jpg图片

作者: Goning | 来源:发表于2018-07-21 20:35 被阅读351次

本文介绍使用FFmpeg实现YUV420P的数据编码为JPEG图片。

/*
* 函数名称: Frame2JPG
* 功能描述: 将AVFrame(YUV420格式)保存为JPEG格式的图片
* 参    数: AVPacket packet av_read_frame读取的一包数据
* 参    数: AVFrame *pFrame 解码完的帧
* 参    数: stream_index 流下标,标记是视频流还是音频流
* 参    数: int width YUV420的宽
* 参    数: int height YUV420的高
* 返 回 值: int 0 代表成功,其他失败
*/
int Frame2JPG(AVPacket packet, AVFrame *pFrame, unsigned int stream_index, 
    int width, int height)
{
    // 输出文件路径  
    char out_file[MAX_PATH] = { 0 };
    sprintf_s(out_file, sizeof(out_file), "%s%d.jpg", "f:\\", 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) 
    {
        gDebug().Write("Couldn't open output file.");
        return -1;
    }
 
    // 构建一个新stream  
    AVStream *pAVStream = avformat_new_stream(pFormatCtx, 0);
    if (pAVStream == NULL) 
    {
        gDebug().Write("Frame2JPG::avformat_new_stream error.");
        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) 
    {
        gDebug().Write("找不到图片编码器.");
        return -1;
    }
    // 设置pCodecCtx的解码器为pCodec  
    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) 
    {
        gDebug().Write("Could not open codec.");
        return -1;
    }
 
    //Write Header  
    int ret = avformat_write_header(pFormatCtx, NULL);
    if (ret < 0)
    {
        gDebug().Write("Frame2JPG::avformat_write_header.\n");
        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) 
    {
        gDebug().Write("Encode Error.\n");
        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;
}

参考:
https://blog.csdn.net/leixiaohua1020/article/details/25346147
https://blog.csdn.net/m0_37684310/article/details/77950390

相关文章

  • FFmpeg实现YUV420P转jpg图片

    本文介绍使用FFmpeg实现YUV420P的数据编码为JPEG图片。 参考:https://blog.csdn.n...

  • FFmpeg代码实现视频转jpg图片

    代码实现视频转图片主要是使用了FFmpeg视频编解码相关的知识,所以首先了解下FFmpeg中的编解码相关函数以及流...

  • 图像转视频

    ffmpeg -loop 1 -i xx.jpg -pix_fmt yuv420p -t 10 -r 2 xx.mp4

  • FFmpeg解码至Surface实现原生播放

    跟上篇FFmpeg 将MP4转为YUV420P大致一样,直播过将解码的流放到surface实现原生播放。 一、代码...

  • 转码的四个案例

    一,swcale实现rgb24转yuv420p 二,swcale实现YUV转RGB

  • SDL2常用代码

    SDL2渲染YUV420P数据 提取YUV420P:ffmpeg -i a.mp4 -pix_fmt yuv420...

  • ffmpeg视频拼接

    ffmpeg4路视频拼接ffmpeg -s 1920x960 -pix_fmt yuv420p -r 25 -i ...

  • 在线图片转jpg如何转换?

    大部分图片使用的都是jpg格式,因此我们要把图片转换成jpg格式,在线图片转jpg如何转换呢?这时候我们可以借助迅...

  • 视频命令ffmpge

    视频转为图片帧 ffmpeg -i input.mp4 -r 10 -f image2 ./out/%d.jpg ...

  • pdf转jpg实现

    通过代码将pdf转jpg,找到一个很好用的工具ImageMagick,实现过程记录。 到http:/...

网友评论

      本文标题:FFmpeg实现YUV420P转jpg图片

      本文链接:https://www.haomeiwen.com/subject/vxbhmftx.html