[cpp] view plain copy
- /**
- * 最简单的基于FFmpeg的收流器(接收RTMP)
- * Simplest FFmpeg Receiver (Receive RTMP)
- *
- * 雷霄骅 Lei Xiaohua
- * leixiaohua1020@126.com
- * 中国传媒大学/数字电视技术
- * Communication University of China / Digital TV Technology
- * http://blog.csdn.net/leixiaohua1020
- *
- * 本例子将流媒体数据(以RTMP为例)保存成本地文件。
- * 是使用FFmpeg进行流媒体接收最简单的教程。
- *
- * This example saves streaming media data (Use RTMP as example)
- * as a local file.
- * It's the simplest FFmpeg stream receiver.
- *
- */
- #include
- #define __STDC_CONSTANT_MACROS
- #ifdef _WIN32
- //Windows
- extern "C"
- {
- #include "libavformat/avformat.h"
- #include "libavutil/mathematics.h"
- #include "libavutil/time.h"
- };
- #else
- //Linux...
- #ifdef __cplusplus
- extern "C"
- {
- #endif
- #include
- #include
- #include
- #ifdef __cplusplus
- };
- #endif
- #endif
- //'1': Use H.264 Bitstream Filter
- #define USE_H264BSF 0
- int main(int argc, char* argv[])
- {
- AVOutputFormat *ofmt = NULL;
- //Input AVFormatContext and Output AVFormatContext
- AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
- AVPacket pkt;
- const char *in_filename, *out_filename;
- int ret, i;
- int videoindex=-1;
- int frame_index=0;
- in_filename = "rtmp://live.hkstv.hk.lxdns.com/live/hks";
- //in_filename = "rtp://233.233.233.233:6666";
- //out_filename = "receive.ts";
- //out_filename = "receive.mkv";
- out_filename = "receive.flv";
- av_register_all();
- //Network
- avformat_network_init();
- //Input
- if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) codec->codec_type==AVMEDIA_TYPE_VIDEO){
- videoindex=i;
- break;
- }
- av_dump_format(ifmt_ctx, 0, in_filename, 0);
- //Output
- avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename); //RTMP
- if (!ofmt_ctx) {
- printf( "Could not create output context\n");
- ret = AVERROR_UNKNOWN;
- goto end;
- }
- ofmt = ofmt_ctx->oformat;
- for (i = 0; i nb_streams; i++) {
- //Create output AVStream according to input AVStream
- AVStream *in_stream = ifmt_ctx->streams[i];
- AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
- if (!out_stream) {
- printf( "Failed allocating output stream\n");
- ret = AVERROR_UNKNOWN;
- goto end;
- }
- //Copy the settings of AVCodecContext
- ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
- if (ret codec->codec_tag = 0;
- if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
- out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
- }
- //Dump Format------------------
- av_dump_format(ofmt_ctx, 0, out_filename, 1);
- //Open output URL
- if (!(ofmt->flags & AVFMT_NOFILE)) {
- ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
- if (ret time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
- 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));
- pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
- pkt.pos = -1;
- //Print to Screen
- if(pkt.stream_index==videoindex){
- printf("Receive %8d video frames from input URL\n",frame_index);
- frame_index++;
- #if USE_H264BSF
- av_bitstream_filter_filter(h264bsfc, in_stream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
- #endif
- }
- //ret = av_write_frame(ofmt_ctx, &pkt);
- ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
- if (ret flags & AVFMT_NOFILE))
- avio_close(ofmt_ctx->pb);
- avformat_free_context(ofmt_ctx);
- if (ret
关注打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?