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

=====================================================

最简单的基于FFmpeg的推流器系列文章列表:

《最简单的基于FFmpeg的推流器(以推送RTMP为例)》

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

=====================================================


出于对《最简单的基于FFmpeg的推流器》的补充,本文记录一个最简单的基于FFmpeg的收流器。收流器和推流器的作用正好相反:推流器用于将本地文件以流媒体的形式发送出去,而收流器用于将流媒体内容保存为本地文件。

本文记录的推流器可以将RTMP流媒体保存成为一个本地的FLV文件。由于FFmpeg本身支持很多的流媒体协议和封装格式,所以也支持其它的封装格式和流媒体协议。

源代码

  1. /**
  2. * 最简单的基于FFmpeg的收流器(接收RTMP)
  3. * Simplest FFmpeg Receiver (Receive RTMP)
  4. *
  5. * 雷霄骅 Lei Xiaohua
  6. * 中国传媒大学/数字电视技术
  7. * Communication University of China / Digital TV Technology
  8. * http://blog.csdn.net/leixiaohua1020
  9. *
  10. * 本例子将流媒体数据(以RTMP为例)保存成本地文件。
  11. * 是使用FFmpeg进行流媒体接收最简单的教程。
  12. *
  13. * This example saves streaming media data (Use RTMP as example)
  14. * as a local file.
  15. * It's the simplest FFmpeg stream receiver.
  16. *
  17. */
  18. #include <stdio.h>
  19. #define __STDC_CONSTANT_MACROS
  20. #ifdef _WIN32
  21. //Windows
  22. extern "C"
  23. {
  24. #include "libavformat/avformat.h"
  25. #include "libavutil/mathematics.h"
  26. #include "libavutil/time.h"
  27. };
  28. #else
  29. //Linux...
  30. #ifdef __cplusplus
  31. extern "C"
  32. {
  33. #endif
  34. #include <libavformat/avformat.h>
  35. #include <libavutil/mathematics.h>
  36. #include <libavutil/time.h>
  37. #ifdef __cplusplus
  38. };
  39. #endif
  40. #endif
  41. //'1': Use H.264 Bitstream Filter
  42. #define USE_H264BSF 0
  43. int main(int argc, char* argv[])
  44. {
  45. AVOutputFormat *ofmt = NULL;
  46. //Input AVFormatContext and Output AVFormatContext
  47. AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
  48. AVPacket pkt;
  49. const char *in_filename, *out_filename;
  50. int ret, i;
  51. int videoindex= -1;
  52. int frame_index= 0;
  53. in_filename = "rtmp://live.hkstv.hk.lxdns.com/live/hks";
  54. //in_filename = "rtp://233.233.233.233:6666";
  55. //out_filename = "receive.ts";
  56. //out_filename = "receive.mkv";
  57. out_filename = "receive.flv";
  58. av_register_all();
  59. //Network
  60. avformat_network_init();
  61. //Input
  62. if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
  63. printf( "Could not open input file.");
  64. goto end;
  65. }
  66. if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
  67. printf( "Failed to retrieve input stream information");
  68. goto end;
  69. }
  70. for(i= 0; i<ifmt_ctx->nb_streams; i++)
  71. if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
  72. videoindex=i;
  73. break;
  74. }
  75. av_dump_format(ifmt_ctx, 0, in_filename, 0);
  76. //Output
  77. avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename); //RTMP
  78. if (!ofmt_ctx) {
  79. printf( "Could not create output context\n");
  80. ret = AVERROR_UNKNOWN;
  81. goto end;
  82. }
  83. ofmt = ofmt_ctx->oformat;
  84. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  85. //Create output AVStream according to input AVStream
  86. AVStream *in_stream = ifmt_ctx->streams[i];
  87. AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
  88. if (!out_stream) {
  89. printf( "Failed allocating output stream\n");
  90. ret = AVERROR_UNKNOWN;
  91. goto end;
  92. }
  93. //Copy the settings of AVCodecContext
  94. ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
  95. if (ret < 0) {
  96. printf( "Failed to copy context from input to output stream codec context\n");
  97. goto end;
  98. }
  99. out_stream->codec->codec_tag = 0;
  100. if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
  101. out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
  102. }
  103. //Dump Format------------------
  104. av_dump_format(ofmt_ctx, 0, out_filename, 1);
  105. //Open output URL
  106. if (!(ofmt->flags & AVFMT_NOFILE)) {
  107. ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
  108. if (ret < 0) {
  109. printf( "Could not open output URL '%s'", out_filename);
  110. goto end;
  111. }
  112. }
  113. //Write file header
  114. ret = avformat_write_header(ofmt_ctx, NULL);
  115. if (ret < 0) {
  116. printf( "Error occurred when opening output URL\n");
  117. goto end;
  118. }
  119. #if USE_H264BSF
  120. AVBitStreamFilterContext* h264bsfc = av_bitstream_filter_init( "h264_mp4toannexb");
  121. #endif
  122. while ( 1) {
  123. AVStream *in_stream, *out_stream;
  124. //Get an AVPacket
  125. ret = av_read_frame(ifmt_ctx, &pkt);
  126. if (ret < 0)
  127. break;
  128. in_stream = ifmt_ctx->streams[pkt.stream_index];
  129. out_stream = ofmt_ctx->streams[pkt.stream_index];
  130. /* copy packet */
  131. //Convert PTS/DTS
  132. pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
  133. 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));
  134. pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
  135. pkt.pos = -1;
  136. //Print to Screen
  137. if(pkt.stream_index==videoindex){
  138. printf( "Receive %8d video frames from input URL\n",frame_index);
  139. frame_index++;
  140. #if USE_H264BSF
  141. av_bitstream_filter_filter(h264bsfc, in_stream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
  142. #endif
  143. }
  144. //ret = av_write_frame(ofmt_ctx, &pkt);
  145. ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
  146. if (ret < 0) {
  147. printf( "Error muxing packet\n");
  148. break;
  149. }
  150. av_free_packet(&pkt);
  151. }
  152. #if USE_H264BSF
  153. av_bitstream_filter_close(h264bsfc);
  154. #endif
  155. //Write file trailer
  156. av_write_trailer(ofmt_ctx);
  157. end:
  158. avformat_close_input(&ifmt_ctx);
  159. /* close output */
  160. if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
  161. avio_close(ofmt_ctx->pb);
  162. avformat_free_context(ofmt_ctx);
  163. if (ret < 0 && ret != AVERROR_EOF) {
  164. printf( "Error occurred.\n");
  165. return -1;
  166. }
  167. return 0;
  168. }

运行结果

程序运行之后,即可获取流媒体数据并且在本地保存成一个视频文件。

下载


simplest ffmpeg streamer


项目主页

SourceForge:https://sourceforge.net/projects/simplestffmpegstreamer/

Github:https://github.com/leixiaohua1020/simplest_ffmpeg_streamer

开源中国:http://git.oschina.net/leixiaohua1020/simplest_ffmpeg_streamer


CSDN下载地址:http://download.csdn.net/detail/leixiaohua1020/8924345


解决方案包含2个项目:

simplest_ffmpeg_streamer: 将本地视频文件推送至流媒体服务器。

simplest_ffmpeg_receiver: 将流媒体数据保存成本地文件。


猜你喜欢

转载自blog.csdn.net/liujiayu2/article/details/80858612
今日推荐