首页 > 编程知识 正文

ffmpeg转码推流,ffmpeg推流rtsp代码

时间:2023-05-03 17:54:41 阅读:33871 作者:3555

另一方面,推送流是对输入输出文件没有要求的1,输入文件是网络流URL,能够实现数据流。

2、将输入的文件转换为回调函数(内存读取)的形式,可以推送内存中的视频数据。

3、在将输入文件改变为系统设备(通过libavdevice )的同时加入编码功能,可以实现实时流媒体(实时直播)。

二、代码实现# include stdio.h # define _ stdc _ constant _ macros # ifdef _ win32//windows extern ' c ' { # include ' librobe } # else//Linux . # ifdef _ cplusplus extern ' c ' { # endif # includelibavformat/av format.h # includelibavutil/matation ' avformat.lib ' ) pragmacomment ) lib,' avcodec.lib ' ) pragmacomment ) lib //输入为zxdby,输出为zxd by//(inputavformationd by ) const char wmdlqin_filename,wmdlqout_filename; int ret,I; int视频索引=-1; int frame_index=0; int64_t start_time=0; in _ filename=' UDP ://238.1.1.1133601234 '; //本地文件或其他格式的实时流、设备等out _ filename=' UDP ://238.1.1.1033606016 '; av_register_all (; //Networkavformat_network_init (; AVDictionary wmdlqinputdic=NULL; //如果不设置,输入源为实时流时,变为花屏。 单位bytes av _ dict _ set (输入DIC,' buffer_size ',' 10485760 ',0 ); av _ dict _ set (输入DIC,' reuse ','1',0 ); //输入(Input ) if ) (ret=avformat_open_input ) ifmt_CTX,in_filename,0,inputdic ) (0) (printf ) cout } if ((ret=av format _ find _ stream _ info ) ifmt_CTX,0 ) printf ) failedtoretrieveinputstreaminformation gotton (for ) I=0; iifmt_ctx-nb_streams; I ) if (含ifmt _ CTX-streams [ I ]-codec-codec _ type==av media _ type的棉糖) {videoindex=i; 布雷克; }av_dump_format(ifmt_CTX,0,in_filename,0 ); //输出(Output ) av format _ alloc _ output _ context2(ofmt _ CTX,NULL,' mpegts ',out_filename ); //UDPif (! ofmt _ CTX (打印(couldnotcreateoutputcontextn ) ); ret=AVERROR_UNKNOWN; goto end; }ofmt=ofmt_ctx-oformat; for(I=0; i ifmt_ctx-nb_streams; 从I//输入流创建输出流(createoutputavstreamaccordingtoinputavstream ) avstreamwmdlqin _ stream=ifmt _ CTX-streams avstreamwmdlqout _ stream=av format _ new _ stream (ofmt _ CTX,in_stream-codec-codec ); if (! out_stream(printf )故障诊断程序

utput streamn");ret = AVERROR_UNKNOWN;goto end;}//复制AVCodecContext的设置(Copy the settings of AVCodecContext)//tanzhenwenret = avcodec_parameters_copy(out_stream->codecpar, ifmt_ctx->streams[i]->codecpar);if (ret < 0) {printf("Failed to copy parameters from input to output stream codec contextn");goto end;}/wmdlqret = avcodec_copy_context(out_stream->codec, in_stream->codec);if (ret < 0) {printf("Failed to copy context from input to output stream codec contextn");goto end;}wmdlq/out_stream->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);//打开输出URL(Open output URL)if (!(ofmt->flags & AVFMT_NOFILE)) {AVDictionarywmdlqdic = NULL;av_dict_set(&dic, "pkt_size", "1316", 0);//Maximum UDP packet size//av_dict_set(&dic, "fifo_size", "18800", 0);//av_dict_set(&dic, "buffer_size", "1000000", 0);//av_dict_set(&dic, "bitrate", "11000000", 0);//av_dict_set(&dic, "buffer_size", "1000000", 0);//1316av_dict_set(&dic, "reuse", "1", 0);ret = avio_open2(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE, NULL, &dic);//ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);if (ret < 0) {printf("Could not open output URL '%s'", out_filename);goto end;}}//av_opt_set(ofmt_ctx->priv_data, "muxrate", "11000000", 0);av_opt_set(ofmt_ctx->priv_data, "MpegTSWrite", "1", 0);av_opt_set(ofmt_ctx->priv_data, "pes_payload_size", "300", 0);//写文件头(Write file header)ret = avformat_write_header(ofmt_ctx, NULL);if (ret < 0) {printf("Error occurred when opening output axdcn");goto end;}start_time = av_gettime();int64_t deltpts = 0;while (1) {AVStream wmdlqin_stream, wmdlqout_stream;//获取一sydzfj(Get an AVPacket)ret = av_read_frame(ifmt_ctx, &pkt);if (ret < 0)break;//FIX:No PTS (Example: Raw H.264)//Simple Write PTSif (pkt.pts == AV_NOPTS_VALUE) {//Write PTSAVRational time_base1 = ifmt_ctx->streams[videoindex]->time_base;//Duration between 2 frames (us)int64_t calc_duration = (double)AV_TIME_BASE / av_q2d(ifmt_ctx->streams[videoindex]->r_frame_rate);//Parameterspkt.pts = (double)(frame_indexwmdlqcalc_duration) / (double)(av_q2d(time_base1)wmdlqAV_TIME_BASE);pkt.dts = pkt.pts;pkt.duration = (double)calc_duration / (double)(av_q2d(time_base1)wmdlqAV_TIME_BASE);}//Important:Delayif (pkt.stream_index == videoindex) {AVRational time_base = ifmt_ctx->streams[videoindex]->time_base;AVRational time_base_q = { 1,AV_TIME_BASE };int64_t pts_time = av_rescale_q(pkt.dts, time_base, time_base_q);int64_t now_time = av_gettime() - start_time;static bool first = true;if (first){deltpts = pts_time;first = false;}if (pts_time - deltpts > now_time)av_usleep(pts_time - deltpts - now_time);}in_stream = ifmt_ctx->streams[pkt.stream_index];out_stream = ofmt_ctx->streams[pkt.stream_index];tanzhenwen//if (pkt.stream_index == videoindex) {//out_stream->time_base = AVRational{ 1, 25 };//}/wmdlq copy packet wmdlq///转换PTS/DTS(Convert PTS/DTS)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));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 Screenif (pkt.stream_index == videoindex) {//printf("Send %8d video frames to output axdcn", frame_index);frame_index++;}//ret = av_write_frame(ofmt_ctx, &pkt);ret = av_interleaved_write_frame(ofmt_ctx, &pkt);if (ret < 0) {printf("Error muxing packetn");break;}av_free_packet(&pkt);}//写文件尾(Write file trailer)av_write_trailer(ofmt_ctx);end:avformat_close_input(&ifmt_ctx);/wmdlq close output wmdlq/if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))avio_close(ofmt_ctx->pb);avformat_free_context(ofmt_ctx);if (ret < 0 && ret != AVERROR_EOF) {printf("Error occurred.n");return -1;}return 0;} 参考

雷神博客
https://blog.csdn.net/leixiaohua1020/article/details/39803457

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。