ffmpeg读取H264,截取BMP

来源:互联网 发布:traceroute windows 编辑:程序博客网 时间:2024/06/14 17:16
void SaveBmp(AVFrame* avPacket, int nWidth, int nHeight, int nBpp){BITMAPFILEHEADER bmpHeader;bmpHeader.bfType = 0x4d42;bmpHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nWidth * nHeight * 3;bmpHeader.bfReserved1 = 0;bmpHeader.bfReserved2 = 0;bmpHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);BITMAPINFOHEADER bmpInfoHeader;bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);bmpInfoHeader.biWidth = nWidth;bmpInfoHeader.biHeight = nHeight;bmpInfoHeader.biPlanes = 1;bmpInfoHeader.biBitCount = nBpp;bmpInfoHeader.biCompression = BI_RGB;bmpInfoHeader.biSizeImage = (nWidth*nBpp + 31) / 32 * 4 * nHeight;bmpInfoHeader.biXPelsPerMeter = 100;bmpInfoHeader.biYPelsPerMeter = 100;bmpInfoHeader.biClrUsed = 0;bmpInfoHeader.biClrImportant = 0;FILE *fp;char *filename = new char[255];//文件存放路径,根据自己的修改sprintf_s(filename, 255, "%s%d.bmp", "D:\\", 0);if ((fp = fopen(filename, "wb+")) == NULL){return;}fwrite(&bmpHeader, sizeof(bmpHeader), 1, fp);fwrite(&bmpInfoHeader, sizeof(bmpInfoHeader), 1, fp);fwrite(avPacket->data[0], nWidth * nHeight * 3, 1, fp);fclose(fp);}void ConvertH264ToBMP(){av_register_all();avformat_network_init();AVFormatContext* pFormatContext = avformat_alloc_context();if (avformat_open_input(&pFormatContext, "D:\\video\\blue_sky.h264", NULL, NULL) != 0){MessageBox(L"avformat_open_input failed", L"提示", MB_OK);return ;}if (avformat_find_stream_info(pFormatContext, NULL) < 0){MessageBox(L"avformat_find_stream_info failed", L"提示", MB_OK);return;}DWORD dwVideoType = -1;for (int i = 0; i < pFormatContext->nb_streams; i++){if (pFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){dwVideoType = i;break;}}AVCodecContext *pCodecContext = pFormatContext->streams[dwVideoType]->codec;AVCodec *pCodec = avcodec_find_decoder(pCodecContext->codec_id);if (avcodec_open2(pCodecContext, pCodec, NULL) != 0){avformat_close_input(&pFormatContext);MessageBox(L"avcodec_open2 failed", L"提示", MB_OK);return;}int nGot = 0;AVFrame *pFrame = av_frame_alloc();AVFrame *pRGB = av_frame_alloc();AVPacket *pPacket = (AVPacket*)av_malloc(sizeof(AVPacket));int nPicSize = avpicture_get_size(AV_PIX_FMT_BGR24, pCodecContext->width, pCodecContext->height);uint8_t *buf = (uint8_t*)av_malloc(nPicSize);if (buf == NULL){printf("av malloc failed!\n");exit(1);}avpicture_fill((AVPicture *)pRGB, buf, AV_PIX_FMT_BGR24, pCodecContext->width, pCodecContext->height);SwsContext* pSws = sws_getContext(pCodecContext->width, pCodecContext->height, pCodecContext->pix_fmt, pCodecContext->width, pCodecContext->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);while (true){if (av_read_frame(pFormatContext, pPacket) >= 0){if (pPacket->stream_index == dwVideoType){if (avcodec_decode_video2(pCodecContext, pFrame, &nGot, pPacket) < 0){break;}if(nGot){int nRet = sws_scale(pSws, pFrame->data, pFrame->linesize, 0, pCodecContext->height, pRGB->data, pRGB->linesize);SaveBmp(pRGB, pCodecContext->width, pCodecContext->height, 24);}}}else{break;}}sws_freeContext(pSws);av_free(pFrame);av_free(pRGB);av_free(buf);avcodec_close(pCodecContext);avformat_close_input(&pFormatContext);}

0 0
原创粉丝点击