FFmpeg拉取rtmp流转udp输出播放

     用ffmpeg可以拉取rtmp流,比如湖南卫视的:rtmp://58.200.131.2:1935/livetv/hunantv

     在vlc播放器可以测试看看,网速不好的,会有延时, 媒体----打开网络串流,填入湖南卫视的rtmp地址。

        播放如下

        太卡了,没专业播放器爽。

       下面开始本篇的主题,拉取rtmp流,用udp转发出去,至于怎么处理就看软件功能需求了。ffmpeg环境自己配吧,也不是很难,api 不会的可以看英文注释,代码如下:

 
  1. #include "stdafx.h"

  2. #include "pch.h"

  3. #include <string>

  4. #include <memory>

  5. #include <thread>

  6. #include <iostream>

  7. using namespace std;

  8.  
  9. AVFormatContext *inputContext = nullptr;

  10. AVFormatContext * outputContext;

  11. int64_t lastReadPacktTime ;

  12.  
  13. static int interrupt_cb(void *ctx)

  14. {

  15. int timeout = 10; //比如拉取湖南卫视的rtmp流,可能延时比较长

  16. if(av_gettime() - lastReadPacktTime > timeout *1000 *1000)

  17. {

  18. return -1;

  19. }

  20. return 0;

  21. }

  22.  
  23. int OpenInput(string inputUrl)

  24. {

  25. inputContext = avformat_alloc_context();

  26. lastReadPacktTime = av_gettime();

  27. inputContext->interrupt_callback.callback = interrupt_cb;

  28. int ret = avformat_open_input(&inputContext, inputUrl.c_str(), nullptr,nullptr);

  29. if(ret < 0)

  30. {

  31. av_log(NULL, AV_LOG_ERROR, "Input file open input failed\n");

  32. return ret;

  33. }

  34. ret = avformat_find_stream_info(inputContext,nullptr);

  35. if(ret < 0)

  36. {

  37. av_log(NULL, AV_LOG_ERROR, "Find input file stream inform failed\n");

  38. }

  39. else

  40. {

  41. av_log(NULL, AV_LOG_FATAL, "Open input file %s success\n",inputUrl.c_str());

  42. }

  43. return ret;

  44. }

  45.  
  46. shared_ptr<AVPacket> ReadPacketFromSource()

  47. {

  48. shared_ptr<AVPacket> packet(static_cast<AVPacket*>(av_malloc(sizeof(AVPacket))), [&](AVPacket *p) { av_packet_free(&p); av_freep(&p);});

  49. av_init_packet(packet.get());

  50. lastReadPacktTime = av_gettime();

  51. int ret = av_read_frame(inputContext, packet.get());

  52. if(ret >= 0)

  53. {

  54. return packet;

  55. }

  56. else

  57. {

  58. return nullptr;

  59. }

  60. }

  61.  
  62. void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)

  63. {

  64. if (pkt->pts != AV_NOPTS_VALUE)

  65. pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);

  66. if (pkt->dts != AV_NOPTS_VALUE)

  67. pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);

  68. if (pkt->duration > 0)

  69. pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);

  70. }

  71.  
  72. int WritePacket(shared_ptr<AVPacket> packet)

  73. {

  74. auto inputStream = inputContext->streams[packet->stream_index];

  75. auto outputStream = outputContext->streams[packet->stream_index];

  76. av_packet_rescale_ts(packet.get(),inputStream->time_base,outputStream->time_base);

  77. return av_interleaved_write_frame(outputContext, packet.get());

  78. }

  79.  
  80. int OpenOutput(string outUrl)

  81. {

  82. int ret = avformat_alloc_output_context2(&outputContext, nullptr, "mpegts", outUrl.c_str());

  83. if(ret < 0)

  84. {

  85. av_log(NULL, AV_LOG_ERROR, "open output context failed\n");

  86. goto Error;

  87. }

  88.  
  89. ret = avio_open2(&outputContext->pb, outUrl.c_str(), AVIO_FLAG_WRITE,nullptr, nullptr);

  90. if(ret < 0)

  91. {

  92. av_log(NULL, AV_LOG_ERROR, "open avio failed");

  93. goto Error;

  94. }

  95.  
  96. for(int i = 0; i < inputContext->nb_streams; i++)

  97. {

  98. AVStream * stream = avformat_new_stream(outputContext, inputContext->streams[i]->codec->codec);

  99. ret = avcodec_copy_context(stream->codec, inputContext->streams[i]->codec);

  100. if(ret < 0)

  101. {

  102. av_log(NULL, AV_LOG_ERROR, "copy coddec context failed");

  103. goto Error;

  104. }

  105. }

  106.  
  107. ret = avformat_write_header(outputContext, nullptr);

  108. if(ret < 0)

  109. {

  110. av_log(NULL, AV_LOG_ERROR, "format write header failed");

  111. goto Error;

  112. }

  113.  
  114. av_log(NULL, AV_LOG_FATAL, " Open output file success %s\n",outUrl.c_str());

  115. return ret ;

  116. Error:

  117. if(outputContext)

  118. {

  119. for(int i = 0; i < outputContext->nb_streams; i++)

  120. {

  121. avcodec_close(outputContext->streams[i]->codec);

  122. }

  123. avformat_close_input(&outputContext);

  124. }

  125. return ret ;

  126. }

  127.  
  128. void CloseInput()

  129. {

  130. if(inputContext != nullptr)

  131. {

  132. avformat_close_input(&inputContext);

  133. }

  134. }

  135.  
  136. void CloseOutput()

  137. {

  138. if(outputContext != nullptr)

  139. {

  140. for(int i = 0 ; i < outputContext->nb_streams; i++)

  141. {

  142. AVCodecContext *codecContext = outputContext->streams[i]->codec;

  143. avcodec_close(codecContext);

  144. }

  145. avformat_close_input(&outputContext);

  146. }

  147. }

  148.  
  149. void Init()

  150. {

  151. av_register_all();

  152. avfilter_register_all();

  153. avformat_network_init();

  154. av_log_set_level(AV_LOG_ERROR);

  155. }

  156.  
  157. int _tmain(int argc, _TCHAR* argv[])

  158. {

  159. Init();

  160. int ret = OpenInput("rtmp://58.200.131.2:1935/livetv/hunantv");

  161. if(ret >= 0)

  162. {

  163. ret = OpenOutput("udp://127.0.0.1:6789");

  164. }

  165.  
  166. if(ret <0)

  167. goto Error;

  168.  
  169. while(true)

  170. {

  171. auto packet = ReadPacketFromSource();

  172. if(packet)

  173. {

  174. ret = WritePacket(packet);

  175. if(ret >= 0)

  176. {

  177. cout<<"WritePacket Success!"<<endl;

  178. }

  179. else

  180. {

  181. cout<<"WritePacket failed!"<<endl;

  182. }

  183. }

  184. else

  185. {

  186. break;

  187. }

  188. }

  189.  
  190. Error:

  191. CloseInput();

  192. CloseOutput();

  193. while(true)

  194. {

  195. this_thread::sleep_for(chrono::seconds(100));

  196. }

  197. return 0;

  198. }

      需要注意的是,由于拉取时网络情况会有影响,那么延时设置需要长点,我设为了10秒,如下所示

 
  1. static int interrupt_cb(void *ctx)

  2. {

  3. int timeout = 10; //比如拉取湖南卫视的rtmp流,可能延时比较长

  4. if(av_gettime() - lastReadPacktTime > timeout *1000 *1000)

  5. {

  6. return -1;

  7. }

  8. return 0;

  9. }

        编译测试,用ffplay可以播放,由于电脑,网速等原因,可能会有一定的卡顿,比如先出声音,画面延迟一会儿才出现。如下:

        以上有不当之处请指出,或在评论区留言。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值