// 获取摄像头名字 //Show Dshow Device void show_dshow_device(){ AVFormatContext *pFormatCtx = avformat_alloc_context(); AVDictionary* options = NULL; av_dict_set(&options,"list_devices","true",0); AVInputFormat *iformat = av_find_input_format("dshow"); printf("========Device Info=============\n"); avformat_open_input(&pFormatCtx,"video=dummy",iformat,&options); printf("================================\n"); } //打开摄像头 int openCameraRecordByDShow(char * pFileDevice,AVFormatContext *pFormatCtx ) { AVInputFormat *ifmt=av_find_input_format("dshow"); //Set own video device's name AVDictionary *options=NULL; int h=av_dict_set(&options,"framerate","25",0); //设置帧频率 if(h<0) { printf("set erro dict------1\n"); } h= av_dict_set(&options, "video_size", "352x288", 0);//设置分辨率 if(h<0 printf="" set="" erro="" dict------2="" n="" if="" avformat_open_input="" pformatctx="" pfiledevice="" ifmt="" options="" 0="" printf="" couldn="" t="" open="" input="" stream="" n="" return="" -1="" return="" 0="" h264="" int="" videotranslateyuvtoh264="" avcodeccontext="" pcodecctx="" avframe="" pframe="" avpacket="" pkt="" int="" i="" ret="" got_output="" av_init_packet="" pkt="" pkt-="">data = NULL; // packet data will be allocated by the encoder pkt->size = 0; //Read raw YUV data ret = avcodec_encode_video2(pCodecCtx, pkt, pFrame, &got_output); if (ret < 0) { printf("Error encoding frame\n"); return -1; } if (got_output) { // fwrite(pkt.data, 1, pkt.size, fp_out); return 0; } return 0; } int dealWithCameraData(Video_Recorder_INFO *pVidRecorderInfo) { if (NULL == pVidRecorderInfo) { printf("the parameter is wrong \n"); return -1; } static AVPacket *decodePacket=(AVPacket *)av_malloc(sizeof(AVPacket)); /**/ int ret =0; int got_picture =0; AVFrame *pFrame= avcodec_alloc_frame(); static uint64_t nPts =0; if(av_read_frame(pVidRecorderInfo->pFormatCtx, decodePacket)>=0) { if(decodePacket->stream_index==pVidRecorderInfo->videoindex) { ret = avcodec_decode_video2(pVidRecorderInfo->pDeCodecCtx, pFrame, &got_picture, decodePacket); if(ret < 0) { av_free(decodePacket); av_free(pFrame); printf("Decode Error.\n"); return -1; } if(got_picture) { sws_scale(pVidRecorderInfo->img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0,pVidRecorderInfo->pDeCodecCtx->height, pVidRecorderInfo->pFrameYUV->data,pVidRecorderInfo-> pFrameYUV->linesize); //编码 pVidRecorderInfo->pFrameYUV->format = AV_PIX_FMT_YUV420P; pVidRecorderInfo->pFrameYUV->width = pVidRecorderInfo->pDeCodecCtx->width; pVidRecorderInfo->pFrameYUV->height =pVidRecorderInfo->pDeCodecCtx->height; pVidRecorderInfo->pFrameYUV->pts = nPts; ret =videoTranslateYuvToH264(pVidRecorderInfo->pEnCodecCtx,pVidRecorderInfo->pFrameYUV,&pVidRecorderInfo->enCoderPacket ); if (0 == ret) { // printf("encode sucess ----1 \n"); if (pVidRecorderInfo->enCoderPacket.size >0) { } } av_free_packet(&pVidRecorderInfo->enCoderPacket); nPts=nPts+1; } } } else { printf("read error\n"); } av_free_packet(decodePacket); //av_free(decodePacket); av_free(pFrame); return 0; } </0>