我使用 ffmpeg 在 C++ 中提取视频帧。我想得到array<unsigned char>
c++ 中的框架,但我得到 AVFrame
从这一行代码。
avcodec_decode_video2(codecContext, DecodedFrame, &gotPicture, Packet);
所以我用
sws_scale
转换 AVFrame
至AVPicture
但我也得不到array<unsigned char>
从帧。sws_scale(convertContext, DecodedFrame->data, DecodedFrame->linesize, 0, (codecContext)->height, convertedFrame->data, convertedFrame->linesize);
所以谁能帮我转换
AVFrame
或 AVPicture
至array<unsigned char>
?
最佳答案
AVPicture
已弃用。转换成它是没有意义的,因为 AVFrame
是它的替代品。
如果我正确理解了这个问题,您正在尝试将原始图片像素值设置为 std::array
.如果是这样,只需转储 data AVFrame
的字段进去。
avcodec_decode_video2(codecContext, DecodedFrame, &gotPicture, Packet);
// If you need rgb, create a swscontext to convert from video pixel format
sws_ctx = sws_getContext(DecodedFrame->width, DecodedFrame->height, codecContext->pix_fmt, DecodedFrame->width, DecodedFrame->height, AV_PIX_FMT_RGB24, 0, 0, 0, 0);
uint8_t* rgb_data[4]; int rgb_linesize[4];
av_image_alloc(rgb_data, rgb_linesize, DecodedFrame->width, DecodedFrame->height, AV_PIX_FMT_RGB24, 32);
sws_scale(sws_ctx, DecodedFrame->data, DecodedFrame->linesize, 0, DecodedFrame->height, rgb_data, rgb_linesize);
// RGB24 is a packed format. It means there is only one plane and all data in it.
size_t rgb_size = DecodedFrame->width * DecodedFrame->height * 3;
std::array<uint8_t, rgb_size> rgb_arr;
std::copy_n(rgb_data[0], rgb_size, rgb_arr);
关于arrays - 将 AVPicture 转换为数组<unsigned char>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40233362/