gpt4 book ai didi

c - 使用 libavformat 和 libswscale 提取视频帧图像给出翻转图像

转载 作者:太空宇宙 更新时间:2023-11-04 02:39:17 24 4
gpt4 key购买 nike

我正在使用以下代码将 avcodec_decode_video2() 输出的图像帧提取到 BMP 文件中:

// Convert the image from its native format to RGB
int height = sws_scale(state.sws_ctx,
(uint8_t const * const *)state.frame->data,
state.frame->linesize,
0,
state.video_codec_ctx->height,
state.picture.data,
state.picture.linesize);

// ..... here BMP header initialization goes ...

// Extract pixels
{
int y = 0;
size_t frame_line_size = state.picture.linesize[0];
uint8_t* bmp_data = &bmp->data[0];
uint8_t* picture_data = state.picture.data[0];
while(y < height)
{
memcpy(bmp_data, picture_data, image_line_size);
bmp_data += image_line_size_with_padding;
picture_data += frame_line_size;
++y;
}
}

但它的结果是翻转图像:

Resulting BMP image

任何人都可以帮助我理解我在这里做错了什么吗?

视频流信息:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'testdata/video3.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 1970-01-01 00:00:00
title : video3.mp4
encoder : Lavf52.78.3
Duration: 00:00:07.93, start: 0.000000, bitrate: 1467 kb/s
Stream #0.0(und): Video: h264 (High), yuv420p, 640x320 [PAR 1:1 DAR 2:1], 1331 kb/s, 29.73 fps, 29.73 tbr, 29734 tbn, 59.47 tbc
Metadata:
creation_time : 1970-01-01 00:00:00
Stream #0.1(und): Audio: aac, 48000 Hz, stereo, fltp, 127 kb/s
Metadata:
creation_time : 1970-01-01 00:00:00
Video file 'testdata/video3.mp4' has 236 frames.

我有使用 libopencv 的替代代码,它从同一视频文件中给出了正确的图像,但我需要直接使用 ffmpeg 库来达到正确的效果。

Correct image established from the same video file via libopencv

最佳答案

下面是工作代码:

// Extract pixels
{
int y = 0;
size_t frame_line_size = state.picture.linesize[0];
uint8_t* bmp_data = &bmp->data[0] + (image_line_size_with_padding * (height - 1));
uint8_t* picture_data = state.picture.data[0];
while(y < height)
{
memcpy(bmp_data, picture_data, image_line_size);
bmp_data -= image_line_size_with_padding;
picture_data += frame_line_size;
++y;
}
}

关于c - 使用 libavformat 和 libswscale 提取视频帧图像给出翻转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33790619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com