2015년 10월 11일 일요일

mp4 save with ffmpeg

ip cam -> raw data(encoded) -> mp4 save

  AVCodec *ce;
  AVFormatContext *fc;
  AVOutputFormat *of;
  AVCodecContext *pcc;
  AVStream *pst;
  int vi;
  AVPacket pkt;

BOOL CCodec_Encoder_h264::FileInit(char *fname, int nWidth, int nHeight)
{
av_register_all();
avcodec_register_all();

of = av_guess_format(0, fname, 0);
fc = avformat_alloc_context();
fc->oformat = of;
strcpy(fc->filename, fname);

pst = avformat_new_stream(fc, 0);
vi = pst->index;

ce = avcodec_find_decoder(CODEC_ID_H264);

pcc = pst->codec;
avcodec_get_context_defaults3(pcc, ce);
pcc->codec_type = AVMEDIA_TYPE_VIDEO;
pcc->codec_id = CODEC_ID_H264;
pcc->profile= FF_PROFILE_H264_BASELINE;
pcc->bit_rate = 1280 * 960 * 2;
pcc->width = nWidth;
pcc->height = nHeight;
pcc->time_base.num = 1;
pcc->time_base.den = 30;
pcc->qcompress = 0.6;
pcc->qmin = 0;
pcc->qmax = 9;
pcc->pix_fmt = PIX_FMT_YUV420P;
pcc->gop_size = 150;

if ( !( fc->oformat->flags & AVFMT_NOFILE ) )
avio_open( &fc->pb, fc->filename, AVIO_FLAG_WRITE );

avformat_write_header(fc, NULL);

m_nFrmCnt = 0;

waitkey = 1;

return TRUE;
}

BOOL CCodec_Encoder_h264::FileAppend(BYTE *pData, int nSize, , DWORD key)
{
  AVPacket pkt;
  av_init_packet(&pkt);
  //pkt.flags |= (0 >= getVopType(pData, nSize )) ? AV_PKT_FLAG_KEY : 0;  
  pkt.flags |= key;
  //pkt.stream_index = m_nFrmCnt++;
  pkt.data = (uint8_t*)pData;
  pkt.size = nSize;

if ( waitkey )
if ( 0 == ( pkt.flags & AV_PKT_FLAG_KEY ) )
return TRUE;
else
waitkey = 0;

av_write_frame(fc, &pkt);

return TRUE;
}

BOOL CCodec_Encoder_h264::FileClose()
{
av_write_trailer(fc);

if ( fc->oformat && !( fc->oformat->flags & AVFMT_NOFILE ) && fc->pb )
        avio_close( fc->pb );

av_free(fc);

return TRUE;
}

댓글 없음: