赞
踩
void h264toMp4(const char *fileName, const char *savePath) { int frame_index=0;//统计帧数 int inVStreamIndex=-1,outVStreamIndex=-1;//输入输出视频流在文件中的索引位置 //const char inVFileName[1000]; //strcpy(inVFileName, fileName); //char* str1 = strtok(inVFileName, "/"); //char* fileTmpName; //printf("%s\n", str1); //int k = 0; //char outFile[1000] = { 0 }; //if (savePath != NULL) { // char outTmpFile[1000] = { 0 }; // while (str1 != NULL) // { // str1 = strtok(NULL, "/"); // printf("%s\n", str1); // if (str1 != NULL) { // fileTmpName = str1; // } // } // k = strlen(fileTmpName); // memcpy(outTmpFile, fileTmpName, k - 4); // strcat(outTmpFile, "mp4"); // strcat(outFile, savePath); // strcat(outFile, "/"); // strcat(outFile, outTmpFile); //} //else //{ // k = strlen(fileName); // memcpy(outFile, fileName, k - 4); // strcat(outFile, "mp4"); //} const char *outFileName = savePath; AVFormatContext *inVFmtCtx=NULL,*outFmtCtx=NULL; AVCodecParameters *codecPara=NULL; AVStream *outVStream=NULL; const AVCodec *outCodec=NULL; AVCodecContext *outCodecCtx=NULL; AVCodecParameters *outCodecPara=NULL; AVPacket *pkt=av_packet_alloc(); do{ //======================输入部分============================// //打开输入文件 if(avformat_open_input(&inVFmtCtx, fileName,NULL,NULL)<0){ printf("Cannot open input file.\n"); break; } //查找输入文件中的流 if(avformat_find_stream_info(inVFmtCtx,NULL)<0){ printf("Cannot find stream info in input file.\n"); break; } //查找视频流在文件中的位置 for(size_t i=0;i<inVFmtCtx->nb_streams;i++){ if(inVFmtCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_VIDEO){ inVStreamIndex=(int)i; break; } } codecPara = inVFmtCtx->streams[inVStreamIndex]->codecpar;//输入视频流的编码参数 printf("===============Input information========>\n"); av_dump_format(inVFmtCtx, 0, fileName, 0); printf("===============Input information========<\n"); //=====================输出部分=========================// //打开输出文件并填充格式数据 if(avformat_alloc_output_context2(&outFmtCtx,NULL,NULL,outFileName)<0){ printf("Cannot alloc output file context.\n"); break; } //打开输出文件并填充数据 if(avio_open(&outFmtCtx->pb,outFileName,AVIO_FLAG_READ_WRITE)<0){ printf("output file open failed.\n"); break; } //在输出的mp4文件中创建一条视频流 outVStream = avformat_new_stream(outFmtCtx,NULL); if(!outVStream){ printf("Failed allocating output stream.\n"); break; } outVStream->time_base.den=30; outVStream->time_base.num=1; outVStreamIndex=outVStream->index; //查找编码器 outCodec = avcodec_find_encoder(codecPara->codec_id); if(outCodec==NULL){ printf("Cannot find any encoder.\n"); break; } //从输入的h264编码器数据复制一份到输出文件的编码器中 outCodecCtx=avcodec_alloc_context3(outCodec); outCodecPara = outFmtCtx->streams[outVStream->index]->codecpar; if(avcodec_parameters_copy(outCodecPara,codecPara)<0){ printf("Cannot copy codec para.\n"); break; } if(avcodec_parameters_to_context(outCodecCtx,outCodecPara)<0){ printf("Cannot alloc codec ctx from para.\n"); break; } outCodecCtx->time_base.den = 30; outCodecCtx->time_base.num=1; //打开输出文件需要的编码器 if(avcodec_open2(outCodecCtx,outCodec,NULL)<0){ printf("Cannot open output codec.\n"); break; } printf("============Output Information=============>\n"); av_dump_format(outFmtCtx,0,outFileName,1); printf("============Output Information=============<\n"); //写入文件头 if(avformat_write_header(outFmtCtx,NULL)<0){ printf("Cannot write header to file.\n"); return; } //===============编码部分===============// while(av_read_frame(inVFmtCtx,pkt)>=0){//循环读取每一帧直到读完 if(pkt->stream_index==inVStreamIndex){//确保处理的是视频流 //FIXME:No PTS (Example: Raw H.264) //Simple Write PTS //如果当前处理帧的显示时间戳为0或者没有等等不是正常值 if(pkt->pts==AV_NOPTS_VALUE){ printf("frame_index:%d\n", frame_index); //Write PTS //Duration between 2 frames (us) //Parameters pkt->pts = (double)((timeArr[frame_index] - timeArr[0]) * AV_TIME_BASE); pkt->dts = pkt->pts; if (frame_index != number - 1) { pkt->duration = ((timeArr[frame_index+1] - timeArr[frame_index]) * AV_TIME_BASE); } else { pkt->duration = 400000; } frame_index++; } AVRational time_base; time_base.num = 1; time_base.den = AV_TIME_BASE; //Convert PTS/DTS pkt->pts = av_rescale_q_rnd(pkt->pts, time_base, outVStream->time_base, (enum AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); pkt->dts = av_rescale_q_rnd(pkt->dts, time_base, outVStream->time_base, (enum AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); pkt->duration = av_rescale_q(pkt->duration, time_base, outVStream->time_base); pkt->pos = -1; pkt->stream_index = outVStreamIndex; printf("Write 1 Packet. size:%5d\tpts:%I64d\n", pkt->size, pkt->pts); //Write if (av_interleaved_write_frame(outFmtCtx, pkt) < 0) { printf("Error muxing packet\n"); break; } av_packet_unref(pkt); } } av_write_trailer(outFmtCtx); }while(0); //=================释放所有指针======================= av_packet_free(&pkt); avformat_close_input(&inVFmtCtx); avformat_close_input(&outFmtCtx); avcodec_free_context(&outCodecCtx); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。