Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- First, one of the initialization methods.
- void Exporty::_InitializeVideoCodecContext( AVFormatContext* avFormatContext,
- MEDIA_PARSER::MediaInfo* mediaInfo,
- AVCodecContext* avCodecContext ) const
- {
- avcodec_get_context_defaults2(avCodecContext, CODEC_TYPE_VIDEO);
- avCodecContext->codec_id = (CodecID)avFormatContext->oformat->video_codec;
- avCodecContext->codec_type = CODEC_TYPE_VIDEO;
- avCodecContext->bit_rate = mediaInfo->GetBitRate();
- X_LOG_INFO( "Detected bit rate: %u\n", mediaInfo->GetBitRate() );
- // Don't need to send this, but might at some point...
- // avCodecContext->gop_size = _videoGopSize;
- avCodecContext->width = mediaInfo->GetFrameWidth();
- avCodecContext->height = mediaInfo->GetFrameHeight();
- X_LOG_INFO( "Resolution: %uX%u", mediaInfo->GetFrameWidth(), mediaInfo->GetFrameHeight() );
- avCodecContext->time_base.num = 1;
- avCodecContext->time_base.den = mediaInfo->GetFrameRate();
- X_LOG_INFO( "Time Base - Numerator: 1, Denomination: %d", mediaInfo->GetFrameRate() );
- avCodecContext->pix_fmt = PIX_FMT_YUV420P;
- // some formats want stream headers to be seperate
- if( !strcmp(avFormatContext->oformat->name, "mp4") ||
- !strcmp(avFormatContext->oformat->name, "mov") ||
- !strcmp(avFormatContext->oformat->name, "3gp") )
- avCodecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
- }
- Here is the main part where everything begins... Obviously this is taken out of context... but this is where the ffmpeg calls begin... The above function is called below...
- av_register_all();
- AVFormatContext* formatContext = avformat_alloc_context();
- if( !formatContext )
- X_STHROW( InternalServerError_500, ("Could not allocate AVFormatContext.") );
- formatContext->oformat = av_guess_format(NULL, _ConvertMIMETypeToFileName( _type ).c_str(), NULL);
- if( !formatContext->oformat )
- X_STHROW( InternalServerError_500, ("Could not allocate output format context.") );
- formatContext->oformat->video_codec = CODEC_ID_H264;
- // CODEC_ID_MJPEGCODEC_ID_MJPEG
- // CODEC_ID_MPEG4
- AVStream* videoStream = av_new_stream( formatContext, 1 );
- if( !videoStream )
- X_STHROW( InternalServerError_500, ("Unable to allocate video stream.") );
- _InitializeVideoCodecContext( formatContext, mediaInfo, videoStream->codec );
- av_set_parameters( formatContext, NULL );
- url_open_dyn_buf( &formatContext->pb );
- av_write_header( formatContext );
- _WriteConfigBuffer( videoStream, formatContext, sps );
- _WriteConfigBuffer( videoStream, formatContext, pps );
- for( int i = 0; i < numFramesToExport; i++ )
- {
- lastFrameTS = resultParser->GetFrameTS();
- AVPacket pkt;
- av_init_packet(&pkt);
- pkt.stream_index = videoStream->index;
- XIRef<XMemory> frameBuffer = new XMemory;
- resultParser->GetFrameData( &frameBuffer->Extend( resultParser->GetFrameSize() ),
- resultParser->GetFrameSize() );
- pkt.data = frameBuffer->Map();
- pkt.size = frameBuffer->GetDataSize();
- av_write_frame( formatContext, &pkt );
- resultParser->Step();
- }
- av_write_trailer( formatContext );
- uint8_t* fileBytes = NULL;
- int fileSize = url_close_dyn_buf( formatContext->pb, &fileBytes );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement