gpt4 book ai didi

无法将 ffmpeg 中的 YUV 帧写入 .yuv 文件

转载 作者:行者123 更新时间:2023-11-30 16:53:28 26 4
gpt4 key购买 nike

我的目标是将我解码的帧写入文件中。我知道我捕捉得很好,因为它显示在我的 SDL 播放中,并且我随后对其进行编码,没有任何问题。但似乎我无法将框架正确写入文件。这是代码:

#define PIXFMT AV_PIX_FMT_YUV420P
#define WIDTH 1280
#define HEIGHT 720
// initialize SWS context for software scaling

sws_ctx = sws_getContext(pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
WIDTH,
HEIGHT,
PIXFMT,
SWS_LANCZOS,
NULL,
NULL,
NULL
);

FfmpegEncoder enc("rtsp://127.0.0.1:1935/live/myStream", pParser);
//SetPixelArray();

i = 0;
enc.FillYuvImage(pFrameRGB, 0, this->pCodecCtx->width, this->pCodecCtx->height);
FILE *pFile;
while (av_read_frame(pFormatCtx, &packet) >= 0 && !exit) {
if (packet.stream_index == videoindex) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);

if (frameFinished) {

i++;
sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
if (i < 500)
{
pFile = fopen(std::string("./screenshots/screen.yuv").c_str(), "a+");
for (int y = 0; y < pCodecCtx->height; y++)
{
fwrite(pFrame->data[0] + y * pFrame->linesize[0], 1, pCodecCtx->width * pCodecCtx->height, pFile);
fwrite(pFrame->data[1] + y * pFrame->linesize[1], 1, pCodecCtx->width * pCodecCtx->height / 4, pFile);
fwrite(pFrame->data[2] + y * pFrame->linesize[2], 1, pCodecCtx->width * pCodecCtx->height / 4, pFile);
}
fclose(pFile);
}
enc.encodeFrame(pFrameRGB);
}
}
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}

程序在尝试写入帧时崩溃。

最佳答案

正如我在评论中提到的,您需要检查 fopen 是否成功。

但我认为问题是你写的内容比缓冲区中存在的内容多。我对 yuv 的了解有限。我认为你应该做以下事情。这是基于我对this page中的单帧YUV420图片的理解。 .

abcfor (int y = 0; y < pCodecCtx->height; y++) //plane 0: same width and height
{
fwrite(pFrame->data[0] + y * pFrame->linesize[0], 1, pCodecCtx->width, pFile);
}


for (int y = 0; y < pCodecCtx->height/4; y++) //plane1: Same width and quarter height.
{
fwrite(pFrame->data[1] + y * pFrame->linesize[1], 1, pCodecCtx->width, pFile);
}

for (int y = 0; y < pCodecCtx->height/4; y++) //plane2: Same width and quarter height.
{
fwrite(pFrame->data[2] + y * pFrame->linesize[2], 1, pCodecCtx->width, pFile);
}

请注意,写入顺序取决于颜色格式。我希望您重点关注每次迭代中的循环迭代次数和 fwrite() 中的字节数。

关于无法将 ffmpeg 中的 YUV 帧写入 .yuv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40850403/

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