当前位置:   article > 正文

H264转MP4

h264转mp4

函数体

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);

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号