音視頻流::ffplay::tutorial01

來源:互聯網
上載者:User

原創:

http://blog.chinaunix.net/uid-24203478-id-3035890.html

 

所用的版本為ffmpeg 0.6.3

使用編譯命令:
gcc -o tutorial01 tutorial01.c -lavutil -lavformat -lavcodec -lswscale

來源程式如下:

[cpp]
view plaincopy
  1. #include <libavcodec/avcodec.h>    
  2. #include <libavformat/avformat.h>    
  3. #include <libswscale/swscale.h>    
  4.     
  5. #include <stdio.h>    
  6.     
  7. #define debug() fprintf(stderr,"%s#%i\n",__func__,__LINE__)    
  8.     
  9. static struct SwsContext *img_convert_ctx;    
  10.     
  11. void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame)    
  12. {    
  13.     FILE  *pFile;    
  14.     char   szFilename[32];    
  15.     int    y;    
  16.      
  17.     sprintf(szFilename, "frame%d.ppm", iFrame);    
  18.     pFile = fopen(szFilename, "wb");    
  19.     if (pFile == NULL) {    
  20.         return;    
  21.     }    
  22.     
  23.     fprintf(pFile, "P6\n%d %d\n255\n", width, height);    
  24.     
  25.     for (y = 0; y < height; y++) {    
  26.         fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);    
  27.     }    
  28.     
  29.     fclose(pFile);    
  30. }    
  31.     
  32. int main(int argc, char *argv[])    
  33. {    
  34.     AVFormatContext  *pFormatCtx;    
  35.     int       i, videoStream;    
  36.     AVCodecContext   *pCodecCtx;    
  37.     AVCodec      *pCodec;    
  38.     AVFrame      *pFrame;    
  39.     AVFrame      *pFrameRGB;    
  40.     AVPacket      packet;    
  41.     int       frameFinished;    
  42.     int               numBytes;    
  43.     uint8_t       *buffer;    
  44.     
  45.     if (argc < 2) {    
  46.         printf("Please provide a movie file\n");    
  47.         return -1;    
  48.     }    
  49.     fprintf(stderr,"ARG OK.\n");    
  50.     debug();    
  51.     av_register_all();    
  52.     fprintf(stderr,"av_register_all() OK.\n");    
  53.     debug();    
  54.         
  55.     pFormatCtx = avformat_alloc_context(); // 分配空間    
  56.     fprintf(stderr,"avformat_alloc_context() OK.\n");    
  57.     debug();    
  58.     if (av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL) != 0) {    
  59.     //if (avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0) { // 開啟輸入檔案    
  60.         return -1;    
  61.     }    
  62.     fprintf(stderr,"avformat_open_input() OK.\n");    
  63.     debug();    
  64.     
  65.     if (av_find_stream_info(pFormatCtx) < 0) {    
  66.         return -1;    
  67.     }    
  68.     fprintf(stderr,"av_find_stream_info() OK.\n");    
  69.     debug();    
  70.     
  71.     dump_format(pFormatCtx, 0, argv[1], 0);    
  72.     fprintf(stderr,"dump_format() OK.\n");    
  73.     
  74.     debug();    
  75.     videoStream = -1;    
  76.     for(i=0; i<pFormatCtx->nb_streams; i++) {    
  77.         if (pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {    
  78.         videoStream = i;    
  79.         break;    
  80.         }    
  81.     }    
  82.     fprintf(stderr,"find CODEC_TYPE_VIDEO OK.\n");    
  83.     debug();    
  84.     if(videoStream == -1) {    
  85.         return -1;    
  86.     }    
  87.     debug();    
  88.     pCodecCtx=pFormatCtx->streams[videoStream]->codec;    
  89.     img_convert_ctx = sws_getContext(    
  90.             pCodecCtx->width,     
  91.             pCodecCtx->height,    
  92.             pCodecCtx->pix_fmt,     
  93.             pCodecCtx->width,     
  94.             pCodecCtx->height,     
  95.             PIX_FMT_RGB24,     
  96.             SWS_BICUBIC,     
  97.             NULL, NULL, NULL);    
  98.     fprintf(stderr,"sws_getContext() OK.\n");    
  99.     
  100.     debug();    
  101.     pCodec = avcodec_find_decoder(pCodecCtx->codec_id);    
  102.     if (pCodec == NULL) {    
  103.         fprintf(stderr, "Unsupported codec!\n");    
  104.         return -1;    
  105.     }    
  106.     fprintf(stderr,"avcodec_find_decoder() OK.\n");    
  107.     
  108.     debug();    
  109.     if (avcodec_open(pCodecCtx, pCodec) < 0) {    
  110.         return -1;    
  111.     }    
  112.     fprintf(stderr,"avcodec_open() OK.\n");    
  113.     debug();    
  114.     
  115.     pFrame = avcodec_alloc_frame();    
  116.     fprintf(stderr,"avcodec_alloc_frame() OK.\n");    
  117.     debug();    
  118.     
  119.     pFrameRGB = avcodec_alloc_frame();    
  120.     if (pFrameRGB == NULL) {    
  121.         return -1;    
  122.     }    
  123.     fprintf(stderr,"avcodec_alloc_frame() OK.\n");    
  124.     debug();    
  125.     
  126.     numBytes = avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,    
  127.                           pCodecCtx->height);    
  128.     fprintf(stderr,"avpicture_get_size() OK.\n");    
  129.     debug();    
  130.     buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));    
  131.     
  132.     debug();    
  133.     avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,    
  134.                pCodecCtx->width, pCodecCtx->height);    
  135.     fprintf(stderr,"avpicture_fill() OK.\n");    
  136.     debug();    i = 0;    
  137.     while(av_read_frame(pFormatCtx, &packet) >= 0) {    
  138.         if (packet.stream_index == videoStream) {    
  139.             avcodec_decode_video(pCodecCtx, pFrame,     
  140.                          &frameFinished,    
  141.                          packet.data,     
  142.                          packet.size);    
  143.             if(frameFinished) {    
  144.                 sws_scale(img_convert_ctx,     
  145.                       (const uint8_t **)pFrame->data,     
  146.                       pFrame->linesize, 0,     
  147.                       pCodecCtx->height,     
  148.                       pFrameRGB->data,     
  149.                       pFrameRGB->linesize);    
  150.                 if (++i <= 5) {    
  151.                     SaveFrame(pFrameRGB, pCodecCtx->width,pCodecCtx->height,i);    
  152.                 }    
  153.             }    
  154.         }    
  155.         av_free_packet(&packet);    
  156.     }    
  157.     debug();    
  158.     
  159.     av_free(buffer);    
  160.     av_free(pFrameRGB);    
  161.     
  162.     av_free(pFrame);    
  163.     
  164.     avcodec_close(pCodecCtx);    
  165.     
  166.     av_close_input_file(pFormatCtx);    
  167.     
  168.     return 0;    
  169. }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.