rendered paste bodyAVFormatContext* openMFile(const char* pFileName, AVStream** ppVideoStream, AVStream** ppAudioStream
, int nWidth, int nHeight, int nFps
, int nEncType, int nBitrate, int nSampleRate, int nSampleType, int nChannels)
{
if(!pFileName) return NULL;
AVOutputFormat* pAVFmt;
AVFormatContext* pAVContext;
AVStream* pAudioStream;
AVStream* pVideoStream;
pAVFmt = av_guess_format(NULL, pFileName, NULL);
if (!pAVFmt) {
printf("Could not deduce output format from file extension: using MPEG.\n");
pAVFmt = av_guess_format("mpeg", NULL, NULL);
}
if (!pAVFmt) {
fprintf(stderr, "Could not find suitable output format\n");
return NULL;
}
pAVFmt->video_codec = CODEC_ID_MJPEG;
if(nEncType == 0)
pAVFmt->audio_codec = CODEC_ID_PCM_MULAW;
else
pAVFmt->audio_codec = CODEC_ID_AAC;
pAVContext = avformat_alloc_context();
if (!pAVContext) {
fprintf(stderr, "Memory error\n");
return NULL;
}
pAVContext->oformat = pAVFmt;
snprintf(pAVContext->filename, sizeof(pAVContext->filename), "%s", pFileName);
pAudioStream = NULL;
pVideoStream = NULL;
if (pAVFmt->video_codec != CODEC_ID_NONE) {
pVideoStream = addVideoStream(pAVContext, nWidth, nHeight, nFps);
}
if (pAVFmt->audio_codec != CODEC_ID_NONE) {
pAudioStream = addAudioStream(pAVContext, nBitrate, nSampleRate, nSampleType, nChannels);
}
// av_dump_format(pAVContext, 0, pFileName, 1);
if (!(pAVFmt->flags & AVFMT_NOFILE)) {
if (avio_open(&pAVContext->pb, pFileName, AVIO_FLAG_WRITE) < 0) {
fprintf(stderr, "Could not open '%s'\n", pFileName);
syslog(LOG_NOTICE, "Failed to open %s\n", pFileName);
return NULL;
}
}
avformat_write_header(pAVContext, NULL);
*ppVideoStream = pVideoStream;
*ppAudioStream = pAudioStream;
return pAVContext;
}
AVStream* addVideoStream(AVFormatContext* pAVContext, int nWidth, int nHeight, int nFps)
{
if(!pAVContext) return -1;
AVCodecContext* pCodecContext = NULL;
AVStream* pAVStream = NULL;
AVCodec* codec;
// printf("addVideoStream:%d!!!\n", nFps);
pAVStream = av_new_stream(pAVContext, 0);
if (!pAVStream) {
fprintf(stderr, "Could not alloc stream\n");
NULL;
}
pCodecContext = pAVStream->codec;
pCodecContext->codec_id = pAVContext->oformat->video_codec;
pCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecContext->bit_rate = 400000;
pCodecContext->width = nWidth;
pCodecContext->height = nHeight;
pCodecContext->time_base.den = nFps;
pCodecContext->time_base.num = 1;
pCodecContext->gop_size = 12; /* emit one intra frame every twelve frames at most */
pCodecContext->pix_fmt = PIX_FMT_YUVJ422P;
if (pCodecContext->codec_id == CODEC_ID_MPEG2VIDEO) {
pCodecContext->max_b_frames = 2;
}
if (pCodecContext->codec_id == CODEC_ID_MPEG1VIDEO) {
pCodecContext->mb_decision=2;
}
if(pAVContext->oformat->flags & AVFMT_GLOBALHEADER)
pCodecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
pCodecContext = pAVStream->codec;
// codec = avcodec_find_encoder(pCodecContext->codec_id);
// if (!codec) {
// // fprintf(stderr, "codec not found\n");
// // return NULL;
// }
// if (avcodec_open(pCodecContext, codec) < 0) {
// // fprintf(stderr, "could not open codec\n");
// // return NULL;
// }
return pAVStream;
}