ffmpeg从AVFrame取出yuv数据到保存到char*中

ffmpeg从AVFrame取出yuv数据到保存到char*中


很多人一直不知道怎么利用ffmpeg从AVFrame取出yuv数据到保存到char*中,下面代码将yuv420p和yuv422p的数据取出并保存到char*buf中。
其他格式可以自己去扩展,前提先看戏yuv的各种格式,yuv的各种格式链接:http://blog.csdn.net/zhuweigangzwg/article/details/17222535

	//如果是视频
	else if (pstream_info[i].dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
	{
		int new_videosize = pkt.size;
		int video_decode_size = avpicture_get_size(pstream_info->dec_ctx->pix_fmt, Zoom_Width,Zoom_Height);
		uint8_t * video_decode_buf =( uint8_t *)calloc(1,video_decode_size * 3 * sizeof(char)); //最大分配的空间,能满足yuv的各种格式

		// Decode video frame
		avcodec_decode_video2(pstream_info->dec_ctx, pDecodeFrame, &frameFinished,&pkt);
		if(frameFinished) 
		{
			if (pstream_info->dec_ctx->pix_fmt == AV_PIX_FMT_YUV420P) //如果是yuv420p的
			{
				for(i = 0 ; i <
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将内存YUV数据混合成一个视频,可以使用FFmpeg库进行处理。以下是一个基本的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <math.h> #include <libavcodec/avcodec.h> #include <libavutil/imgutils.h> #include <libavutil/opt.h> #include <libavutil/mathematics.h> #define WIDTH 640 #define HEIGHT 480 #define FPS 25 int main(int argc, char *argv[]) { int ret; AVCodec *codec; AVCodecContext *codec_context; AVFrame *frame; AVPacket packet; uint8_t *buffer; int buffer_size; int frame_count = 0; /* Allocate frame */ frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Error allocating frame\n"); exit(1); } /* Set frame properties */ frame->format = AV_PIX_FMT_YUV420P; frame->width = WIDTH; frame->height = HEIGHT; /* Allocate buffer for frame */ buffer_size = av_image_get_buffer_size(frame->format, frame->width, frame->height, 1); buffer = av_malloc(buffer_size); av_image_fill_arrays(frame->data, frame->linesize, buffer, frame->format, frame->width, frame->height, 1); /* Open codec */ codec = avcodec_find_encoder(AV_CODEC_ID_H264); if (!codec) { fprintf(stderr, "Codec not found\n"); exit(1); } codec_context = avcodec_alloc_context3(codec); if (!codec_context) { fprintf(stderr, "Error allocating codec context\n"); exit(1); } /* Set codec properties */ codec_context->width = WIDTH; codec_context->height = HEIGHT; codec_context->pix_fmt = AV_PIX_FMT_YUV420P; codec_context->time_base = (AVRational){1, FPS}; codec_context->bit_rate = 400000; /* Open codec */ ret = avcodec_open2(codec_context, codec, NULL); if (ret < 0) { fprintf(stderr, "Error opening codec: %s\n", av_err2str(ret)); exit(1); } /* Encode frames */ while (frame_count < FPS * 10) { /* Generate YUV data */ uint8_t *y_data = malloc(WIDTH * HEIGHT); uint8_t *u_data = malloc(WIDTH * HEIGHT / 4); uint8_t *v_data = malloc(WIDTH * HEIGHT / 4); // fill y_data, u_data, v_data with your desired YUV data /* Convert YUV data to frame */ int y_size = WIDTH * HEIGHT; int u_size = y_size / 4; int v_size = y_size / 4; memcpy(frame->data[0], y_data, y_size); memcpy(frame->data[1], u_data, u_size); memcpy(frame->data[2], v_data, v_size); /* Set frame properties */ frame->pts = frame_count++; /* Encode frame */ ret = avcodec_send_frame(codec_context, frame); if (ret < 0) { fprintf(stderr, "Error sending frame to codec: %s\n", av_err2str(ret)); exit(1); } while (ret >= 0) { ret = avcodec_receive_packet(codec_context, &packet); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; else if (ret < 0) { fprintf(stderr, "Error receiving packet from codec: %s\n", av_err2str(ret)); exit(1); } /* Write packet to file or stream */ // fwrite(packet.data, 1, packet.size, outfile); av_packet_unref(&packet); } free(y_data); free(u_data); free(v_data); } /* Flush encoder */ ret = avcodec_send_frame(codec_context, NULL); if (ret < 0) { fprintf(stderr, "Error sending flush frame to codec: %s\n", av_err2str(ret)); exit(1); } while (ret >= 0) { ret = avcodec_receive_packet(codec_context, &packet); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; else if (ret < 0) { fprintf(stderr, "Error receiving packet from codec: %s\n", av_err2str(ret)); exit(1); } /* Write packet to file or stream */ // fwrite(packet.data, 1, packet.size, outfile); av_packet_unref(&packet); } /* Free resources */ avcodec_free_context(&codec_context); av_frame_free(&frame); av_free(buffer); return 0; } ``` 在此示例,我们使用`av_image_fill_arrays()`函数分配了一个YUV420P格式的AVFrame,并将其用作编码器的输入。我们使用`avcodec_send_frame()`函数将帧发送到编码器进行编码,然后使用`avcodec_receive_packet()`函数从编码器接收编码数据包。最后,我们使用`av_packet_unref()`函数释放数据包并清除任何剩余的缓存数据。 要生成要混合的YUV数据,您可以从文件读取数据,也可以在内存生成数据。无论哪种方法,您都需要将其复制到AVFrame的data数组。请注意,AVFrameYUV数据顺序是YUV420P,即先是所有的Y分量,然后是U和V分量交错。因此,在将YUV数据复制到AVFrame之前,请确保按正确的顺序复制它们。 希望这可以帮助您开始使用FFmpeg处理内存YUV数据

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值