利用ffmpeg将RTSP传输的h264原始码流保存到文件中

来源:互联网 发布:天龙八部染发好看数据 编辑:程序博客网 时间:2024/05/22 02:04

    利用ffmpeg将RTSP传输的h264原始码流保存到文件中,没有做任何处理,直接将h264码流保存到文件中。

    其中测试的RTSP地址是网上公开的一个 rtsp流媒体测试地址。

下面是程序:

[cpp] view plain copy
 print?
  1. /** 
  2. *作者:HJL 
  3. *最后更新:2015.7.18 
  4. *利用ffmpeg将RTSP传输的h264原始码流保存到文件中 
  5. *未加任何效果,不显示 
  6. **/  
  7.   
  8.   
  9. #include <stdio.h>  
  10.   
  11. #define __STDC_CONSTANT_MACROS  
  12.   
  13. #ifdef _WIN32  
  14. //Windows  
  15. extern "C"  
  16. {  
  17. #include "libavcodec/avcodec.h"  
  18. #include "libavformat/avformat.h"  
  19. #include "libswscale/swscale.h"  
  20. #include "SDL2/SDL.h"  
  21. };  
  22. #else  
  23. //Linux...  
  24. #ifdef __cplusplus  
  25. extern "C"  
  26. {  
  27. #endif  
  28. #include <libavcodec/avcodec.h>  
  29. #include <libavformat/avformat.h>  
  30. #include <libswscale/swscale.h>  
  31. #include <SDL2/SDL.h>  
  32. #ifdef __cplusplus  
  33. };  
  34. #endif  
  35. #endif  
  36.   
  37. int main(int argc, char* argv[])  
  38. {  
  39.   
  40.     AVFormatContext *pFormatCtx;  
  41.     int             i, videoindex;  
  42.     AVCodecContext  *pCodecCtx;  
  43.     AVCodec         *pCodec;  
  44.     AVFrame *pFrame,*pFrameYUV;  
  45.     uint8_t *out_buffer;  
  46.     AVPacket *packet;  
  47.     int ret, got_picture;  
  48.   
  49.   
  50.     struct SwsContext *img_convert_ctx;  
  51.     //下面是公共的RTSP测试地址  
  52.     char filepath[]="rtsp://218.204.223.237:554/live/1/0547424F573B085C/gsfp90ef4k0a6iap.sdp";  
  53.   
  54.     av_register_all();  
  55.     avformat_network_init();  
  56.     pFormatCtx = avformat_alloc_context();  
  57.   
  58.     if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0)////打开网络流或文件流  
  59.     {  
  60.         printf("Couldn't open input stream.\n");  
  61.         return -1;  
  62.     }  
  63.     if(avformat_find_stream_info(pFormatCtx,NULL)<0)  
  64.     {  
  65.         printf("Couldn't find stream information.\n");  
  66.         return -1;  
  67.     }  
  68.     videoindex=-1;  
  69.     for(i=0; i<pFormatCtx->nb_streams; i++)   
  70.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)  
  71.         {  
  72.             videoindex=i;  
  73.             break;  
  74.         }  
  75.         if(videoindex==-1)  
  76.         {  
  77.             printf("Didn't find a video stream.\n");  
  78.             return -1;  
  79.         }  
  80.         pCodecCtx=pFormatCtx->streams[videoindex]->codec;  
  81.         pCodec=avcodec_find_decoder(pCodecCtx->codec_id);  
  82.         if(pCodec==NULL)  
  83.         {  
  84.             printf("Codec not found.\n");  
  85.             return -1;  
  86.         }  
  87.         if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)  
  88.         {  
  89.             printf("Could not open codec.\n");  
  90.             return -1;  
  91.         }  
  92.         pFrame=av_frame_alloc();  
  93.         pFrameYUV=av_frame_alloc();  
  94.         out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));  
  95.         avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);  
  96.   
  97.         //Output Info---输出一些文件(RTSP)信息  
  98.         printf("---------------- File Information ---------------\n");  
  99.         av_dump_format(pFormatCtx,0,filepath,0);  
  100.         printf("-------------------------------------------------\n");  
  101.   
  102.         img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,   
  103.             pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);   
  104.   
  105.   
  106.         packet=(AVPacket *)av_malloc(sizeof(AVPacket));  
  107.   
  108.         FILE *fpSave;  
  109.         if((fpSave = fopen("geth264.h264""ab")) == NULL) //h264保存的文件名  
  110.             return 0;   
  111.         for (;;)   
  112.         {  
  113.             //------------------------------  
  114.             if(av_read_frame(pFormatCtx, packet)>=0)  
  115.             {  
  116.                 if(packet->stream_index==videoindex)  
  117.                 {  
  118.                     fwrite(packet->data,1,packet->size,fpSave);//写数据到文件中  
  119.                 }  
  120.                 av_free_packet(packet);  
  121.             }  
  122.         }  
  123.   
  124.   
  125.         //--------------  
  126.         av_frame_free(&pFrameYUV);  
  127.         av_frame_free(&pFrame);  
  128.         avcodec_close(pCodecCtx);  
  129.         avformat_close_input(&pFormatCtx);  
  130.   
  131.         return 0;  
  132. }  

控制台打印的信息如下:



最后保存的h264码流的文件用h264播放器打开后如下:




整个VS2012工程文件可以在这里下载。

阅读全文
0 0
原创粉丝点击