我在OpenCV中有一个图像,我可以正确查看和保存,我想拍摄这张图像并将其传递给FFMPEG,这样我就可以编码了,但是当我从ffpmeg保存jpg输出时,我得到一个空图像,可能意味着我没有正确地将数据复制到AVFrame。
我做错了什么......
非常感谢任何帮助。
这就是我设置final_frame和yuv422帧的方式
final_frame = avcodec_alloc_frame();
int num_bytes = avpicture_get_size(PIX_FMT_BGR24, 1600, 720);
final_frame1_buffer = (uint8_t *)av_malloc(num_bytes*sizeof(uint8_t));
avpicture_fill((AVPicture*)final_frame, final_frame1_buffer, PIX_FMT_BGR24, 1600, 720);
yuv422_final_frame = avcodec_alloc_frame();
int yuv422_num_bytes = avpicture_get_size( PIX_FMT_YUV422P, 1600, 720 );
final_frame2_buffer = (uint8_t *)av_malloc(yuv422_num_bytes*sizeof(uint8_t));
avpicture_fill((AVPicture*)yuv422_final_frame, final_frame2_buffer, PIX_FMT_YUVJ422P, 1600, 720);
我附上了以下代码
cv::imshow("output image", im3); <---------- Image shows correctly
cv::Mat rgb_frame;
cv::cvtColor( im3 , rgb_frame, CV_BGR2RGB ) ;
if (final_sws_ctx == NULL)
{
final_sws_ctx = sws_getContext(1600, 720,
AV_PIX_FMT_BGR24, 1600, 720,
AV_PIX_FMT_YUVJ422P, SWS_FAST_BILINEAR, 0, 0, 0);
}
imwrite( "rgbjpeg.jpg", rgb_frame ); <----- Image Saves correctly here too
avpicture_fill((AVPicture*)final_frame, rgb_frame.data, PIX_FMT_RGB24, 1600, 720);
sws_scale(final_sws_ctx, final_frame->data,
final_frame->linesize,
0, 720,
yuv422_final_frame->data,
yuv422_final_frame->linesize);
AVPacket encode_packet;
int got_output = 0;
av_init_packet(&encode_packet);
encode_packet.data = NULL;
encode_packet.size = 0;
int ret = avcodec_encode_video2(final_codec_context,
&encode_packet,
yuv422_final_frame,
&got_output);
if (got_output ) {
CString temp;
temp.Format( "test%u.jpg", counter);
FILE* outputFile = fopen(temp, "wb");
printf("Write frame (size=%5d)\n", encode_packet.size);
fwrite(encode_packet.data, 1, encode_packet.size, outputFile);
av_free_packet(&encode_packet);
fclose(outputFile);
counter++;
}
答案 0 :(得分:-1)
尝试明确设置final_frame
的步幅值:
final_frame = avcodec_alloc_frame();
avcodec_get_frame_defaults(final_frame);
final_frame->format = PIX_FMT_RGB24;
final_frame->width = 1600;
final_frame->height = 720;
final_frame->data[0] = rgb_frame.data;
final_frame->linesize[0] = rgb_frame.step; // <<< stride
sws_scale(final_sws_ctx, final_frame->data, // etc...