1、打開rtmp讀流地址:
1 const char* filename ="rtmp://live.hkstv.hk.lxdns.com/live/hks 2 int ret; 3 if ((ret = avformat_open_input(&pFormatCtx, filename, NULL, NULL)) < 0) 4 { 5 abort(); 6 } 7 8 videoStreamIndex = 0; 9 10 if ((ret = avformat_find_stream_info(pFormatCtx, 0)) < 0) 11 { 12 abort(); 13 } 14 15 //解碼視頻 16 pCodeCtx = pFormatCtx->streams[videoStreamIndex]->codec; 17 18 //獲取解碼器 19 pCodec = avcodec_find_decoder(pCodeCtx->codec_id); 20 if (pCodec == NULL) 21 { 22 abort(); 23 } 24 if (pCodec->capabilities & CODEC_CAP_TRUNCATED) 25 { 26 pCodeCtx->flags |= CODEC_FLAG_TRUNCATED; 27 } 28 if (avcodec_open2(pCodeCtx, pCodec, NULL) < 0) 29 { 30 abort(); 31 }
2、打開rtmp推流地址
1 const char* out_file = "rtmp://live-push.meidou.d3dstore.com/live/200446?tm=20160907155557&sign=30f1877d6fab32de9a555f1285c7e1ed"; 2 in_w = 1776; 3 in_h = 888; 4 avformat_alloc_output_context2(&ppFormatCtx, NULL, "flv", out_file); 5 ppCodec = avcodec_find_encoder(AV_CODEC_ID_H264); 6 7 if (!ppCodec) 8 { 9 //printf("Can not find encoder! \n"); 10 //return; 11 AfxMessageBox("Can not find encoder!"); 12 return; 13 } 14 pCodecCtx = avcodec_alloc_context3(ppCodec); 15 pCodecCtx->pix_fmt = PIX_FMT_YUV420P; 16 pCodecCtx->width = in_w; 17 pCodecCtx->height = in_h; 18 pCodecCtx->time_base.num = 1; 19 pCodecCtx->time_base.den = 20;// 25;// 25; 20 pCodecCtx->bit_rate = 2000000;// 400000; 21 pCodecCtx->gop_size = 500;// 250; 22 pCodecCtx->qcompress = 0.6; 23 24 pCodecCtx->thread_count = 4;//將幀分塊,由不同的進程分別編碼,0 for auto 25 26 27 28 if (ppFormatCtx->oformat->flags & AVFMT_GLOBALHEADER) 29 pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER; 30 31 //H264 codec param 32 pCodecCtx->me_range = 16; 33 pCodecCtx->max_qdiff = 4; 34 //pCodecCtx->qcompress = 0.6; 35 pCodecCtx->qmin = 10; 36 pCodecCtx->qmax = 23;// 20;// 51;//編碼質量,值越大,質量越差,最差為51 37 //Optional Param 38 pCodecCtx->max_b_frames = 3; 39 // Set H264 preset and tune 40 AVDictionary *param = 0; 41 av_dict_set(¶m, "preset", "fast", 0); 42 av_dict_set(¶m, "tune", "zerolatency", 0); 43 44 if (avcodec_open2(pCodecCtx, ppCodec, ¶m) < 0) 45 { 46 //printf("Failed to open encoder! \n"); 47 AfxMessageBox("Failed to open encoder!"); 48 return; 49 50 }