ffmpeg解码视频存为BMP文件
生活随笔
收集整理的這篇文章主要介紹了
ffmpeg解码视频存为BMP文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ffmpeg解碼視頻存為BMP文件
分類:?ffmpeg2011-07-28 12:13?8人閱讀?評論(0)?收藏?舉報 view plain- #include?<windows.h>??
- ?#include?<stdio.h>??
- #include?<stdlib.h>??
- #include?<string.h>??
- #pragma?once????
- ??
- ?#ifdef?__cplusplus??
- extern?"C"?{??
- #endif??
- #include?<libavcodec/avcodec.h>??
- #include?<libavformat/avformat.h>??
- #include?<libswscale/swscale.h>??
- ??
- ??
- #ifdef?__cplusplus??
- }??
- #endif??
- ??
- //定義BMP文件頭??
- ?#ifndef?_WINGDI_???
- #define?_WINGDI_??
- typedef?struct?tagBITMAPFILEHEADER?{???
- ????????WORD????bfType;???
- ????????DWORD???bfSize;???
- ????????WORD????bfReserved1;???
- ????????WORD????bfReserved2;???
- ????????DWORD???bfOffBits;???
- }?BITMAPFILEHEADER,?FAR?*LPBITMAPFILEHEADER,?*PBITMAPFILEHEADER;???
- ???
- typedef?struct?tagBITMAPINFOHEADER{???
- ????????DWORD??????biSize;???
- ????????LONG???????biWidth;???
- ????????LONG???????biHeight;???
- ????????WORD???????biPlanes;???
- ????????WORD???????biBitCount;???
- ????????DWORD??????biCompression;???
- ????????DWORD??????biSizeImage;???
- ????????LONG???????biXPelsPerMeter;???
- ????????LONG???????biYPelsPerMeter;???
- ????????DWORD??????biClrUsed;???
- ????????DWORD??????biClrImportant;???
- }?BITMAPINFOHEADER,?FAR?*LPBITMAPINFOHEADER,?*PBITMAPINFOHEADER;???
- ???
- #endif???
- ???
- //保存BMP文件的函數??
- void?SaveAsBMP?(AVFrame?*pFrameRGB,?int?width,?int?height,?int?index,?int?bpp)???
- {???
- ????char?buf[5]?=?{0};???
- ????BITMAPFILEHEADER?bmpheader;???
- ????BITMAPINFOHEADER?bmpinfo;???
- ????FILE?*fp;???
- ???????
- ????char?*filename?=?new?char[255];??
- ???????//文件存放路徑,根據自己的修改??
- ????sprintf_s(filename,255,"%s%d.bmp","D:/My?Documents/Visual?Studio?2008/Projects/WriteVideo/Debug/test",index);??
- ????if?(?(fp=fopen(filename,"wb+"))?==?NULL?)???
- ????{???
- ???????printf?("open?file?failed!\n");???
- ???????return;???
- ????}???
- ???
- ????bmpheader.bfType?=?0x4d42;???
- ????bmpheader.bfReserved1?=?0;???
- ????bmpheader.bfReserved2?=?0;???
- ????bmpheader.bfOffBits?=?sizeof(BITMAPFILEHEADER)?+?sizeof(BITMAPINFOHEADER);???
- ????bmpheader.bfSize?=?bmpheader.bfOffBits?+?width*height*bpp/8;???
- ???????
- ????bmpinfo.biSize?=?sizeof(BITMAPINFOHEADER);???
- ????bmpinfo.biWidth?=?width;???
- ????bmpinfo.biHeight?=?height;???
- ????bmpinfo.biPlanes?=?1;???
- ????bmpinfo.biBitCount?=?bpp;???
- ????bmpinfo.biCompression?=?BI_RGB;???
- ????bmpinfo.biSizeImage?=?(width*bpp+31)/32*4*height;???
- ????bmpinfo.biXPelsPerMeter?=?100;???
- ????bmpinfo.biYPelsPerMeter?=?100;???
- ????bmpinfo.biClrUsed?=?0;???
- ????bmpinfo.biClrImportant?=?0;???
- ???????
- ????fwrite?(&bmpheader,?sizeof(bmpheader),?1,?fp);???
- ????fwrite?(&bmpinfo,?sizeof(bmpinfo),?1,?fp);???
- ????fwrite?(pFrameRGB->data[0],?width*height*bpp/8,?1,?fp);???
- ???????
- ????fclose(fp);???
- }???
- ???
- //主函數??
- int?main?(void)???
- {???
- ????unsigned?int?i?=?0,?videoStream?=?-1;???
- ????AVCodecContext?*pCodecCtx;???
- ????AVFormatContext?*pFormatCtx;???
- ????AVCodec?*pCodec;???
- ????AVFrame?*pFrame,?*pFrameRGB;???
- ????struct?SwsContext?*pSwsCtx;???
- ????const?char?*filename?=?"D:/My?Documents/Visual?Studio?2008/Projects/WriteVideo/Debug/DELTA.MPG";???
- ????AVPacket?packet;???
- ????int?frameFinished;???
- ????int?PictureSize;???
- ????uint8_t?*buf;???
- ?????//注冊編解碼器??
- ????av_register_all();???
- ?????//打開視頻文件??
- ????if?(?av_open_input_file(&pFormatCtx,?filename,?NULL,?0,?NULL)?!=?0?)???
- ????{???
- ????????printf?("av?open?input?file?failed!\n");???
- ????????exit?(1);???
- ????}???
- ?????//獲取流信息??
- ????if?(?av_find_stream_info(pFormatCtx)?<?0?)???
- ????{???
- ????????printf?("av?find?stream?info?failed!\n");???
- ????????exit?(1);???
- ????}???
- ?????//獲取視頻流??
- ????for?(?i=0;?i<pFormatCtx->nb_streams;?i++?)???
- ????if?(?pFormatCtx->streams[i]->codec->codec_type?==?CODEC_TYPE_VIDEO?)???
- ????{???
- ???????videoStream?=?i;???
- ???????break;???
- ????}???
- ???????
- ????if?(videoStream?==?-1)???
- ????{???
- ????????printf?("find?video?stream?failed!\n");???
- ????????exit?(1);???
- ????}???
- ???????
- ????pCodecCtx?=?pFormatCtx->streams[videoStream]->codec;???
- ???????
- ????pCodec?=?avcodec_find_decoder?(pCodecCtx->codec_id);???
- ???????
- ????if?(pCodec?==?NULL)???
- ????{???
- ????????printf?("avcode?find?decoder?failed!\n");???
- ????????exit?(1);???
- ????}???
- ?????//打開解碼器??
- ????if?(?avcodec_open(pCodecCtx,?pCodec)<0?)???
- ????{???
- ????????printf?("avcode?open?failed!\n");???
- ????????exit?(1);???
- ????}???
- ???????
- ???//為每幀圖像分配內存??
- ????pFrame?=?avcodec_alloc_frame();???
- ????pFrameRGB?=?avcodec_alloc_frame();???
- ???????
- ????if?(?(pFrame==NULL)||(pFrameRGB==NULL)?)???
- ????{???
- ????????printf("avcodec?alloc?frame?failed!\n");???
- ????????exit?(1);???
- ????}???
- ???????
- ????PictureSize?=?avpicture_get_size?(PIX_FMT_BGR24,?pCodecCtx->width,?pCodecCtx->height);???
- ????buf?=?(uint8_t*)av_malloc(PictureSize);???
- ???????
- ????if?(?buf?==?NULL?)???
- ????{???
- ????????printf(?"av?malloc?failed!\n");???
- ????????exit(1);???
- ????}???
- ????avpicture_fill?(?(AVPicture?*)pFrameRGB,?buf,?PIX_FMT_BGR24,?pCodecCtx->width,?pCodecCtx->height);???
- ???????
- //設置圖像轉換上下文??
- ????pSwsCtx?=?sws_getContext?(pCodecCtx->width,???
- ?????????????pCodecCtx->height,???
- ?????????????pCodecCtx->pix_fmt,???
- ?????????????pCodecCtx->width,???
- ?????????????pCodecCtx->height,???
- ?????????????PIX_FMT_BGR24,???
- ?????????????SWS_BICUBIC,???
- ?????????????NULL,?NULL,?NULL);???
- ????i?=?0;???
- ????while(av_read_frame(pFormatCtx,?&packet)?>=?0)???
- ????{???
- ????if(packet.stream_index==videoStream)???
- ????{???
- ???????avcodec_decode_video(pCodecCtx,?pFrame,?&frameFinished,??
- ?????packet.data,?packet.size);???
- ?????????
- ???????if(frameFinished)???
- ???????{??????
- ????????????//反轉圖像?,否則生成的圖像是上下調到的??
- ????????????pFrame->data[0]?+=?pFrame->linesize[0]?*?(pCodecCtx->height?-?1);???
- ????????????pFrame->linesize[0]?*=?-1;???
- ????????????pFrame->data[1]?+=?pFrame->linesize[1]?*?(pCodecCtx->height?/?2?-?1);???
- ????????????pFrame->linesize[1]?*=?-1;???
- ????????????pFrame->data[2]?+=?pFrame->linesize[2]?*?(pCodecCtx->height?/?2?-?1);???
- ????????????pFrame->linesize[2]?*=?-1;???
- ?????//轉換圖像格式,將解壓出來的YUV420P的圖像轉換為BRG24的圖像??
- ????????????sws_scale?(pSwsCtx,?pFrame->data,?pFrame->linesize,?0,?pCodecCtx->height,?pFrameRGB->data,?pFrameRGB->linesize);???
- ?????SaveAsBMP?(pFrameRGB,?pCodecCtx->width,?pCodecCtx->height,?i++,?24);???
- ???????}???????
- ????}???
- ????av_free_packet(&packet);???
- ????}???
- ???????
- ????sws_freeContext?(pSwsCtx);???
- ????av_free?(pFrame);???
- ????av_free?(pFrameRGB);???
- ????avcodec_close?(pCodecCtx);???
- ????av_close_input_file?(pFormatCtx);???
- ???????
- ????return?0;???
- } ??
轉載于:https://www.cnblogs.com/moonvan/archive/2011/09/11/2173467.html
總結
以上是生活随笔為你收集整理的ffmpeg解码视频存为BMP文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 营转非车辆报废 能报废多少钱啊?
- 下一篇: XFile 关键帧动画的解析遇到的问题