国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS ffmpeg獲取視頻關鍵幀

2019-11-09 16:41:01
字體:
來源:轉載
供稿:網友
av_register_all();      if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) {          PRintf("error!/n"); }     if(av_find_stream_info(pFormatCtx)<0)   {        printf("error!/n");   }   videoStream=-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("error!/n");// Didn't find a video stream  	}        // 得到視頻流編碼上下文的指針       pCodecCtx=pFormatCtx->streams[videoStream]->codec;  }用兩種方式,一是利用ffmpeg提供的可執(zhí)行文件進行提取,另外就是用ffmpeg的sdk,進行開發(fā)。我下面說一下如何使用ffmpeg sdk進行提取(假設把提取的關鍵幀保存成bmp,源文件名是sample.mpg):首先獲取文件中的視頻流: 然后選擇解碼器進行解碼:
AVCodec *pCodec;   
//  尋找視頻流的解碼器   pCodec=avcodec_find_decoder(pCodecCtx->codec_id);   if(pCodec==NULL)  {     printf("error!/n");// 找不到解碼器   }
// 打開解碼器   if(avcodec_open(pCodecCtx, pCodec)<0)   {    printf("error!/n"); // 打不開解碼器  }現(xiàn)在開始,進入解碼和提取關鍵幀的過程:
pFrame=avcodec_alloc_frame();   pFrameRGB = avcodec_alloc_frame();   numBytes=avpicture_get_size(PIX_FMT_BGR24, pCodecCtx->width,pCodecCtx->height);   buffer=new uint8_t[numBytes];   avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx->height);   pSWSCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, 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)           {               if(pFrame->key_frame==1) // 這就是關鍵幀               {                   sws_scale(pSWSCtx, pFrame->data, pFrame->linesize,0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);                   // 保存到磁盤                   char pic[200];                   sprintf(pic,"pic%d.bmp",i);                   i++;                    av_create_bmp(pic,pFrameRGB->data[0],pCodecCtx->width,pCodecCtx->height,24);               }           }       }       av_free_packet(&packet);   } 最后,釋放資源和句柄
// 釋放 RGB 圖象   av_free(pFrameRGB);  
// 釋放YUV 幀   av_free(pFrame);   
sws_freeContext(pSWSCtx);   
// 關閉解碼器(codec)   avcodec_close(pCodecCtx);  
// 關閉視頻文件   av_close_input_file(pFormatCtx);  ffmpeg相關知識

轉自:http://fengqing888.blog.163.com/blog/static/330114162011111632548120/


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宣城市| 桑植县| 湟中县| 昂仁县| 准格尔旗| 石门县| 全南县| 刚察县| 平顶山市| 长垣县| 南充市| 鄱阳县| 泉州市| 陵水| 舞钢市| 天峨县| 贡觉县| 黔西县| 陈巴尔虎旗| 武穴市| 黄龙县| 通渭县| 皮山县| 棋牌| 乐业县| 三穗县| 九龙县| 兴化市| 上饶县| 仙居县| 杭锦后旗| 高尔夫| 夏河县| 观塘区| 河西区| 星子县| 正镶白旗| 宣汉县| 霍邱县| 额济纳旗| 临西县|