avframe保存到文件

https://stackoverflow.com/questions/35797904/writing-decoded-yuv420p-data-into-a-file-with-ffmpeg

void SaveAvFrame(AVFrame *avFrame)
{
    FILE *fDump = fopen("...", "ab");

    uint32_t pitchY = avFrame->linesize[0];
    uint32_t pitchU = avFrame->linesize[1];
    uint32_t pitchV = avFrame->linesize[2];

    uint8_t *avY = avFrame->data[0];
    uint8_t *avU = avFrame->data[1];
    uint8_t *avV = avFrame->data[2];

    for (uint32_t i = 0; i < avFrame->height; i++) {
        fwrite(avY, avFrame->width, 1, fDump);
        avY += pitchY;
    }

    for (uint32_t i = 0; i < avFrame->height/2; i++) {
        fwrite(avU, avFrame->width/2, 1, fDump);
        avU += pitchU;
    }

    for (uint32_t i = 0; i < avFrame->height/2; i++) {
        fwrite(avV, avFrame->width/2, 1, fDump);
        avV += pitchV;
    }

    fclose(fDump);
}
int saveYUVFrameToFile(AVFrame* frame, int width, int height)
{
    FILE* fileHandle;
    int y, writeError;
    char filename[32];
    static int frameNumber = 0;

    sprintf(filename, "frame%d.yuv", frameNumber);

    fileHandle = fopen(filename, "wb");
    if (fileHandle == NULL)
    {
        printf("Unable to open %s...\n", filename);
        return ERROR;
    }

    /*Writing Y plane data to file.*/
    for (y = 0; y < height; y++)
    {
        writeError = fwrite(frame->data[0] + y*frame->linesize[0], 1, width, fileHandle);
        if (writeError != width)
        {
            printf("Unable to write Y plane data!\n");
            return ERROR;
        }
    }

    /*Dividing by 2.*/
    height >>= 1;
    width >>= 1;

    /*Writing U plane data to file.*/
    for (y = 0; y < height; y++)
    {
        writeError = fwrite(frame->data[1] + y*frame->linesize[1], 1, width, fileHandle);
        if (writeError != width)
        {
            printf("Unable to write U plane data!\n");
            return ERROR;
        }
    }

    /*Writing V plane data to file.*/
    for (y = 0; y < height; y++)
    {
        writeError = fwrite(frame->data[2] + y*frame->linesize[2], 1, width, fileHandle);
        if (writeError != width)
        {
            printf("Unable to write V plane data!\n");
            return ERROR;
        }
    }

    fclose(fileHandle);
    frameNumber++;

    return NO_ERROR;

猜你喜欢

转载自blog.csdn.net/u010029439/article/details/81709859