网络视频流 -- ffmpeg 推流
生活随笔
收集整理的這篇文章主要介紹了
网络视频流 -- ffmpeg 推流
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ? ? ? 一般流媒體服務(wù)器自動拉流功能要求較多,需要自己實現(xiàn)rtsp協(xié)議中較多部分,而ffmpeg本身rtsp拉流配置支持不好,本著站在巨人肩膀上的原則,直接采用ffmpeg推流,將數(shù)據(jù)推送到視頻服務(wù)器,再由視頻服務(wù)器轉(zhuǎn)發(fā),供更多設(shè)備拉取數(shù)據(jù)。
? ? ? ? ffmpeg推流與文件保存流程相同,只是始終寫而不結(jié)束,拉流配置代碼如下:
AVCodecContext* pCodecCtx = 0;AVCodec* pCodec;int in_w, in_h, got_picture, ret;AVPacket pkt;uint8_t* picture_buf;AVFrame* pFrame = 0;int picture_size;struct SwsContext* img_convert_ctx;AVDictionary* param = 0;//傳輸AVFormatContext* ofmt_ctx = NULL;AVStream* video_st;int framecnt = 0;AVRational time_base_q = { 1, AV_TIME_BASE };int vid_next_pts = 0;if (avformat_alloc_output_context2(&ofmt_ctx, NULL, "rtsp", pInf->RTSP_addr) < 0){printf("Fail: avformat_alloc_output_context2\n");goto o_Terminate_Coder_Thread;}//檢查所有流是否都有數(shù)據(jù),如果沒有數(shù)據(jù)會等待max_interleave_delta微秒ofmt_ctx->max_interleave_delta = 1000000;//使用tcp協(xié)議傳輸av_opt_set(ofmt_ctx->priv_data, "rtsp_transport", "tcp", 0);/* Some formats want stream headers to be separate. *///Add a new stream to output, should be called by the user before avformat_write_header() for muxingvideo_st = avformat_new_stream(ofmt_ctx, pCodec);if (video_st == NULL){goto o_Terminate_Coder_Thread;}video_st->time_base.num = 1;//video_st->time_base.den = 75;video_st->time_base.den = 10;avcodec_parameters_from_context(video_st->codecpar, pCodecCtx);printf("======= av_dump_format output =======\n");av_dump_format(ofmt_ctx, 0, pInf->RTSP_addr, 1);printf("============== dump end ========= \n");//Open output URLif (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)){ret = avio_open(&ofmt_ctx->pb, pInf->RTSP_addr, AVIO_FLAG_READ_WRITE);if (ret < 0){printf("Could not open output URL '%s'", pInf->RTSP_addr);goto o_Terminate_Coder_Thread;}}av_dict_free(¶m);av_dict_set(¶m, "stimeout", "5000000", 0);//很關(guān)鍵,當(dāng)推送目標(biāo)沒有響應(yīng)時,5秒后會自動退出,要不就死這了ret = avformat_write_header(ofmt_ctx, ¶m);if (ret < 0)printf("Error occurred when opening output URL\n");elseprintf("avformat_write_header return: %d\n", ret);while (1){pkt.data = NULL;pkt.size = 0;av_init_packet(&pkt);//注意,在這里獲得編碼數(shù)據(jù)//pkt = 。。。///framecnt++;pkt.stream_index = video_st->index;AVRational time_base = ofmt_ctx->streams[0]->time_base;//{ 1, 1000 };AVRational r_framerate1 = { 1, 25 };//Duration between 2 frames (us)int64_t calc_duration = (double)(AV_TIME_BASE) * (1 / av_q2d(r_framerate1)); //內(nèi)部時間戳//Parameters//enc_pkt.pts = (double)(framecnt*calc_duration)*(double)(av_q2d(time_base_q)) / (double)(av_q2d(time_base));pkt.pts = av_rescale_q(framecnt * calc_duration, time_base_q, time_base);pkt.dts = pkt.pts;pkt.duration = av_rescale_q(calc_duration, time_base_q, time_base); //(double)(calc_duration)*(double)(av_q2d(time_base_q)) / (double)(av_q2d(time_base));pkt.pos = -1;vid_next_pts = framecnt * calc_duration; //general timebaseif(pInf->close_coder_Thread == 0)SendToRTSP(pInf, pkt.data + 4, pkt.size - 4);//av_write_frame(ofmt_ctx, &pkt);ret = av_interleaved_write_frame(ofmt_ctx, &pkt);av_packet_unref(&pkt);delete[] p;pInf->VideoBuffer_Count--;}av_write_trailer(ofmt_ctx);? ? ? ? 推送視頻服務(wù)器為EasyDarwin,開源可免費(fèi)下載,后臺自動運(yùn)行,經(jīng)過測試,可支持多路視頻同時推送和更多用戶觀看。推送地址IP需要與服務(wù)器相同,后面的字符串隨意。
? ? ? ? 該視頻服務(wù)器部署后能被??档囊曨l服務(wù)器拉取視頻流,并正常保存和預(yù)覽。
總結(jié)
以上是生活随笔為你收集整理的网络视频流 -- ffmpeg 推流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 漫画分销系统服务器配置,漫画分销平台哪个
- 下一篇: Raspberry Pi车牌识别系统