您当前的位置: 首页 > 

鱼儿-1226

暂无认证

  • 0浏览

    0关注

    1100博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

最简单的基于FFMPEG的推流器附件:收流器

鱼儿-1226 发布时间:2020-08-07 10:32:46 ,浏览量:0

[cpp] view plain copy

  1. /** 
  2.  * 最简单的基于FFmpeg的收流器(接收RTMP) 
  3.  * Simplest FFmpeg Receiver (Receive RTMP) 
  4.  *  
  5.  * 雷霄骅 Lei Xiaohua 
  6.  * leixiaohua1020@126.com 
  7.  * 中国传媒大学/数字电视技术 
  8.  * Communication University of China / Digital TV Technology 
  9.  * http://blog.csdn.net/leixiaohua1020 
  10.  *  
  11.  * 本例子将流媒体数据(以RTMP为例)保存成本地文件。 
  12.  * 是使用FFmpeg进行流媒体接收最简单的教程。 
  13.  * 
  14.  * This example saves streaming media data (Use RTMP as example) 
  15.  * as a local file. 
  16.  * It's the simplest FFmpeg stream receiver. 
  17.  *  
  18.  */  
  19.   
  20. #include   
  21.   
  22. #define __STDC_CONSTANT_MACROS  
  23.   
  24. #ifdef _WIN32  
  25. //Windows  
  26. extern "C"  
  27. {  
  28. #include "libavformat/avformat.h"  
  29. #include "libavutil/mathematics.h"  
  30. #include "libavutil/time.h"  
  31. };  
  32. #else  
  33. //Linux...  
  34. #ifdef __cplusplus  
  35. extern "C"  
  36. {  
  37. #endif  
  38. #include   
  39. #include   
  40. #include   
  41. #ifdef __cplusplus  
  42. };  
  43. #endif  
  44. #endif  
  45.   
  46. //'1': Use H.264 Bitstream Filter   
  47. #define USE_H264BSF 0  
  48.   
  49. int main(int argc, char* argv[])  
  50. {  
  51.     AVOutputFormat *ofmt = NULL;  
  52.     //Input AVFormatContext and Output AVFormatContext  
  53.     AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;  
  54.     AVPacket pkt;  
  55.     const char *in_filename, *out_filename;  
  56.     int ret, i;  
  57.     int videoindex=-1;  
  58.     int frame_index=0;  
  59.     in_filename  = "rtmp://live.hkstv.hk.lxdns.com/live/hks";  
  60.     //in_filename  = "rtp://233.233.233.233:6666";  
  61.     //out_filename = "receive.ts";  
  62.     //out_filename = "receive.mkv";  
  63.     out_filename = "receive.flv";  
  64.   
  65.     av_register_all();  
  66.     //Network  
  67.     avformat_network_init();  
  68.     //Input  
  69.     if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) codec->codec_type==AVMEDIA_TYPE_VIDEO){  
  70.             videoindex=i;  
  71.             break;  
  72.         }  
  73.   
  74.     av_dump_format(ifmt_ctx, 0, in_filename, 0);  
  75.   
  76.     //Output  
  77.     avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename); //RTMP  
  78.   
  79.     if (!ofmt_ctx) {  
  80.         printf( "Could not create output context\n");  
  81.         ret = AVERROR_UNKNOWN;  
  82.         goto end;  
  83.     }  
  84.     ofmt = ofmt_ctx->oformat;  
  85.     for (i = 0; i nb_streams; i++) {  
  86.         //Create output AVStream according to input AVStream  
  87.         AVStream *in_stream = ifmt_ctx->streams[i];  
  88.         AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);  
  89.         if (!out_stream) {  
  90.             printf( "Failed allocating output stream\n");  
  91.             ret = AVERROR_UNKNOWN;  
  92.             goto end;  
  93.         }  
  94.         //Copy the settings of AVCodecContext  
  95.         ret = avcodec_copy_context(out_stream->codec, in_stream->codec);  
  96.         if (ret codec->codec_tag = 0;  
  97.         if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)  
  98.             out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;  
  99.     }  
  100.     //Dump Format------------------  
  101.     av_dump_format(ofmt_ctx, 0, out_filename, 1);  
  102.     //Open output URL  
  103.     if (!(ofmt->flags & AVFMT_NOFILE)) {  
  104.         ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);  
  105.         if (ret time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  
  106.         pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  
  107.         pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);  
  108.         pkt.pos = -1;  
  109.         //Print to Screen  
  110.         if(pkt.stream_index==videoindex){  
  111.             printf("Receive %8d video frames from input URL\n",frame_index);  
  112.             frame_index++;  
  113.   
  114. #if USE_H264BSF  
  115.             av_bitstream_filter_filter(h264bsfc, in_stream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);  
  116. #endif  
  117.         }  
  118.         //ret = av_write_frame(ofmt_ctx, &pkt);  
  119.         ret = av_interleaved_write_frame(ofmt_ctx, &pkt);  
  120.   
  121.         if (ret flags & AVFMT_NOFILE))  
  122.         avio_close(ofmt_ctx->pb);  
  123.     avformat_free_context(ofmt_ctx);  
  124.     if (ret 
关注
打赏
1604459285
查看更多评论
立即登录/注册

微信扫码登录

0.0584s