ffmpeg yuv数据存储方式_ffmpeg yuv420保存jpg文件

int yuv420_to_jpg(void *data,int w,int h,char *file)

{

av_register_all();

AVFormatContext *pFormatCtx = avformat_alloc_context();

AVOutputFormat *fmt = av_guess_format("mjpeg", NULL, NULL);

pFormatCtx->oformat = fmt;

if (avio_open(&pFormatCtx->pb,file, AVIO_FLAG_READ_WRITE) < 0){

printf("Couldn‘t open output file.");

return -1;

}

AVStream *video_st = avformat_new_stream(pFormatCtx, 0);

if (video_st==NULL){

return -1;

}

AVCodecContext *pCodecCtx = video_st->codec;

pCodecCtx->codec_id = fmt->video_codec;

pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;

pCodecCtx->pix_fmt = PIX_FMT_YUVJ420P;

pCodecCtx->width = w;

pCodecCtx->height = h;

pCodecCtx->time_base.num = 1;

pCodecCtx->time_base.den = 25;

AVCodec *pCodec = avcodec_find_encoder(pCodecCtx->codec_id);

if (!pCodec){

return -1;

}

if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0){

return -1;

}

AVFrame *picture = av_frame_alloc();

int size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);

uint8_t *picture_buf = (uint8_t *)av_malloc(size);

if (!picture_buf){

return -1;

}

avpicture_fill((AVPicture *)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);

avformat_write_header(pFormatCtx,NULL);

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

AVPacket pkt;

av_new_packet(&pkt,y_size*3);

memcpy(picture_buf,data,y_size*3/2);

picture->data[0] = picture_buf;

picture->data[1] = picture_buf+ y_size;

picture->data[2] = picture_buf+ y_size*5/4;

int got_picture = 0;

int ret = avcodec_encode_video2(pCodecCtx, &pkt,picture, &got_picture);

if(ret < 0){

printf("Encode Error.\n");

return -1;

}

if (got_picture==1){

pkt.stream_index = video_st->index;

ret = av_write_frame(pFormatCtx, &pkt);

}

av_free_packet(&pkt);

av_write_trailer(pFormatCtx);

if (video_st){

avcodec_close(video_st->codec);

av_free(picture);

av_free(picture_buf);

}

avio_close(pFormatCtx->pb);

avformat_free_context(pFormatCtx);

return 1;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值