前言
有时播放实时流的时候有截图的需求,需要将解码出来的图片保存本地或上传服务器,这时就需要将avframe中的数据编码成png、jpg等格式的图片,我们使用ffmpeg的相关编码器就可以实现功能。
一、如何实现
1、查找编码器
首先需要查找图片编码器,比如jpg为AV_CODEC_ID_MJPEG,png为AV_CODEC_ID_PNG
示例代码:
1
|
enum AVCodecID codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG); |
2、构造编码器上下文
有了编码器就可以构造编码器上下文了。
1
2
3
4
5
6
7
8
9
10
11
12
|
AVCodecContext*ctx = avcodec_alloc_context3(codec); ctx->bit_rate = 3000000; ctx->width = frame->width; //视频帧的宽 ctx->height = frame->height; //视频帧的高 ctx->time_base.num = 1; ctx->time_base.den = 25; ctx->gop_size = 10; ctx->max_b_frames = 0; ctx->thread_count = 1; ctx->pix_fmt = *codec->pix_fmts; //使用编码器适配的像素格式 //打开编码器 avcodec_open2(ctx, codec, NULL); |
3、像素格式转换
如果输入视频帧的像素和编码器的像素格式不相同则需要转换像素格式,我们采用SwsContext 转换即可
1
2
3
4
5
6
7
8
9
10
11
|
AVFrame*rgbFrame = av_frame_alloc(); //转换后的帧 swsContext = sws_getContext(frame->width, frame->height, ( enum AVPixelFormat)frame->format, frame->width, frame->height, ctx->pix_fmt, 1, NULL, NULL, NULL); int bufferSize = av_image_get_buffer_size(ctx->pix_fmt, frame->width, frame->height, 1) * 2; buffer = (unsigned char *)av_malloc(bufferSize); //构造帧的缓存 av_image_fill_arrays(rgbFrame->data, rgbFrame->linesize, buffer, ctx->pix_fmt, frame->width, frame->height, 1); sws_scale(swsContext, frame->data, frame->linesize, 0, frame->height, rgbFrame->data, rgbFrame->linesize); //构造必要的参数 rgbFrame->format = ctx->pix_fmt; rgbFrame->width = ctx->width; rgbFrame->height = ctx->height; |
4、编码
得到转后的帧就可以编码
1
|
ret = avcodec_send_frame(ctx, rgbFrame); |
5、获取图片数据
获取解码后的包即可得到图片数据。
1
2
3
4
5
6
7
8
9
|
uint8_t* outbuf; //输出图片的缓存 size_t outbufSize; //缓存大小 AVPacket pkt; av_init_packet(&pkt); //获取解码的包 avcodec_receive_packet(ctx, &pkt); //将图片数据拷贝到缓存 if (pkt.size > 0 && pkt.size <= outbufSize) memcpy (outbuf, pkt.data, pkt.size); |
6、销毁资源
将上述步骤使用的对象销毁。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
if (swsContext) { sws_freeContext(swsContext); } if (rgbFrame) { av_frame_unref(rgbFrame); av_frame_free(&rgbFrame); } if (buffer) { av_free(buffer); } av_packet_unref(&pkt); if (ctx) { avcodec_close(ctx); avcodec_free_context(&ctx); } |
二、完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/// <summary> /// 帧转图片 /// 如果外部提供的缓存长度不足则不会写入。 /// </summary> /// <param name="frame">[in]视频帧</param> /// <param name="codecID">[in]图片编码器ID,如jpg:AV_CODEC_ID_MJPEG,png:AV_CODEC_ID_PNG</param> /// <param name="outbuf">[out]图片缓存,由外部提供</param> /// <param name="outbufSize">[in]图片缓存长度</param> /// <returns>返回图片实际长度</returns> static int frameToImage(AVFrame* frame, enum AVCodecID codecID, uint8_t* outbuf, size_t outbufSize) { int ret = 0; AVPacket pkt; AVCodec* codec; AVCodecContext* ctx = NULL; AVFrame* rgbFrame = NULL; uint8_t* buffer = NULL; struct SwsContext* swsContext = NULL; av_init_packet(&pkt); codec = avcodec_find_encoder(codecID); if (!codec) { printf ( "avcodec_send_frame error %d" , codecID); goto end; } if (!codec->pix_fmts) { printf ( "unsupport pix format with codec %s" , codec->name); goto end; } ctx = avcodec_alloc_context3(codec); ctx->bit_rate = 3000000; ctx->width = frame->width; ctx->height = frame->height; ctx->time_base.num = 1; ctx->time_base.den = 25; ctx->gop_size = 10; ctx->max_b_frames = 0; ctx->thread_count = 1; ctx->pix_fmt = *codec->pix_fmts; ret = avcodec_open2(ctx, codec, NULL); if (ret < 0) { printf ( "avcodec_open2 error %d" , ret); goto end; } if (frame->format != ctx->pix_fmt) { rgbFrame = av_frame_alloc(); if (rgbFrame == NULL) { printf ( "av_frame_alloc fail:%d" ); goto end; } swsContext = sws_getContext(frame->width, frame->height, ( enum AVPixelFormat)frame->format, frame->width, frame->height, ctx->pix_fmt, 1, NULL, NULL, NULL); if (!swsContext) { printf ( "sws_getContext fail:%d" ); goto end; } int bufferSize = av_image_get_buffer_size(ctx->pix_fmt, frame->width, frame->height, 1) * 2; buffer = (unsigned char *)av_malloc(bufferSize); if (buffer == NULL) { printf ( "buffer alloc fail:%d" , bufferSize); goto end; } av_image_fill_arrays(rgbFrame->data, rgbFrame->linesize, buffer, ctx->pix_fmt, frame->width, frame->height, 1); if ((ret = sws_scale(swsContext, frame->data, frame->linesize, 0, frame->height, rgbFrame->data, rgbFrame->linesize)) < 0) { printf ( "sws_scale error %d" , ret); } rgbFrame->format = ctx->pix_fmt; rgbFrame->width = ctx->width; rgbFrame->height = ctx->height; ret = avcodec_send_frame(ctx, rgbFrame); } else { ret = avcodec_send_frame(ctx, frame); } if (ret < 0) { printf ( "avcodec_send_frame error %d" , ret); goto end; } ret = avcodec_receive_packet(ctx, &pkt); if (ret < 0) { printf ( "avcodec_receive_packet error %d" , ret); goto end; } if (pkt.size > 0 && pkt.size <= outbufSize) memcpy (outbuf, pkt.data, pkt.size); ret = pkt.size; end: if (swsContext) { sws_freeContext(swsContext); } if (rgbFrame) { av_frame_unref(rgbFrame); av_frame_free(&rgbFrame); } if (buffer) { av_free(buffer); } av_packet_unref(&pkt); if (ctx) { avcodec_close(ctx); avcodec_free_context(&ctx); } return ret; } |
三、使用示例
1、截取视频帧并保存文件
1
2
3
4
|
void main() { AVFrame* frame; //视频解码得到的帧 saveFrameToJpg(frame, "snapshot.jpg" ); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/// <summary> /// 将视频帧保存为jpg图片 /// </summary> /// <param name="frame">视频帧</param> /// <param name="path">保存的路径</param> void saveFrameToJpg(AVFrame*frame, const char *path) { //确保缓冲区长度大于图片,使用brga像素格式计算。如果是bmp或tiff依然可能超出长度,需要加一个头部长度,或直接乘以2。 int bufSize = av_image_get_buffer_size(AV_PIX_FMT_BGRA, frame->width, frame->height, 64); //申请缓冲区 uint8_t* buf = (uint8_t*)av_malloc(bufSize); //将视频帧转换成图片 int picSize = frameToImage(frame, AV_CODEC_ID_MJPEG, buf, bufSize); //写入文件 auto f = fopen (path, "wb+" ); if (f) { fwrite (buf, sizeof (uint8_t), bufSize, f); fclose (f); } //释放缓冲区 av_free(buf); } |
2、自定义数据构造AVFrame
1
2
3
4
5
6
|
void main() { uint8_t*frameData; //解码得到的视频数据 AVFrame* frame=allocFrame(frameData,640,360,AV_PIX_FMT_YUV420P); saveFrameToJpg(frame, "snapshot.jpg" ); //此方法定义在示例1中 av_frame_free(&frame); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/// <summary> /// 通过裸数据生成avframe /// </summary> /// <param name="frameData">帧数据</param> /// <param name="width">帧宽</param> /// <param name="height">帧高</param> /// <param name="format">像素格式</param> /// <returns>avframe,使用完成后需要调用av_frame_free释放</returns> AVFrame* allocFrame(uint8_t*frameData, int width, int height,AVPixelFormat format) { AVFrame* frame = av_frame_alloc(); frame->width = width; frame->height = height; frame->format = format; av_image_fill_arrays(frame->data, frame->linesize, frameData, format, frame->width, frame->height, 64); return frame; } |
总结
以上就是今天要讲的内容,总的来说整个流程和一般的视频编码是一致的,只是选择的编码器不同,拿到的图片数据在内存中,可以直接网络传输或保存到本地。可以很方便的在视频界面过程中截图,尤其是解码使用ffmpeg的情况下。实现也不算难,写成文章是为了以后能直接复用,毕竟时间久了一些细节还是会遗忘的。
到此这篇关于C++ ffmpeg实现将视频帧转换成jpg或png等图片的文章就介绍到这了,更多相关C++ ffmpeg视频帧转图片内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/u013113678/article/details/129802171