Advertisement
Guest User

ffmpeg avformat example

a guest
Sep 17th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. First, one of the initialization methods.
  2.  
  3. void Exporty::_InitializeVideoCodecContext( AVFormatContext* avFormatContext,
  4. MEDIA_PARSER::MediaInfo* mediaInfo,
  5. AVCodecContext* avCodecContext ) const
  6. {
  7. avcodec_get_context_defaults2(avCodecContext, CODEC_TYPE_VIDEO);
  8.  
  9. avCodecContext->codec_id = (CodecID)avFormatContext->oformat->video_codec;
  10. avCodecContext->codec_type = CODEC_TYPE_VIDEO;
  11.  
  12. avCodecContext->bit_rate = mediaInfo->GetBitRate();
  13. X_LOG_INFO( "Detected bit rate: %u\n", mediaInfo->GetBitRate() );
  14.  
  15. // Don't need to send this, but might at some point...
  16. // avCodecContext->gop_size = _videoGopSize;
  17.  
  18. avCodecContext->width = mediaInfo->GetFrameWidth();
  19. avCodecContext->height = mediaInfo->GetFrameHeight();
  20. X_LOG_INFO( "Resolution: %uX%u", mediaInfo->GetFrameWidth(), mediaInfo->GetFrameHeight() );
  21.  
  22. avCodecContext->time_base.num = 1;
  23. avCodecContext->time_base.den = mediaInfo->GetFrameRate();
  24. X_LOG_INFO( "Time Base - Numerator: 1, Denomination: %d", mediaInfo->GetFrameRate() );
  25.  
  26. avCodecContext->pix_fmt = PIX_FMT_YUV420P;
  27.  
  28. // some formats want stream headers to be seperate
  29. if( !strcmp(avFormatContext->oformat->name, "mp4") ||
  30. !strcmp(avFormatContext->oformat->name, "mov") ||
  31. !strcmp(avFormatContext->oformat->name, "3gp") )
  32. avCodecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
  33. }
  34.  
  35.  
  36. 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...
  37.  
  38. av_register_all();
  39.  
  40. AVFormatContext* formatContext = avformat_alloc_context();
  41. if( !formatContext )
  42. X_STHROW( InternalServerError_500, ("Could not allocate AVFormatContext.") );
  43.  
  44. formatContext->oformat = av_guess_format(NULL, _ConvertMIMETypeToFileName( _type ).c_str(), NULL);
  45.  
  46. if( !formatContext->oformat )
  47. X_STHROW( InternalServerError_500, ("Could not allocate output format context.") );
  48.  
  49. formatContext->oformat->video_codec = CODEC_ID_H264;
  50. // CODEC_ID_MJPEGCODEC_ID_MJPEG
  51. // CODEC_ID_MPEG4
  52.  
  53. AVStream* videoStream = av_new_stream( formatContext, 1 );
  54. if( !videoStream )
  55. X_STHROW( InternalServerError_500, ("Unable to allocate video stream.") );
  56.  
  57. _InitializeVideoCodecContext( formatContext, mediaInfo, videoStream->codec );
  58.  
  59. av_set_parameters( formatContext, NULL );
  60.  
  61. url_open_dyn_buf( &formatContext->pb );
  62.  
  63. av_write_header( formatContext );
  64.  
  65. _WriteConfigBuffer( videoStream, formatContext, sps );
  66. _WriteConfigBuffer( videoStream, formatContext, pps );
  67.  
  68. for( int i = 0; i < numFramesToExport; i++ )
  69. {
  70. lastFrameTS = resultParser->GetFrameTS();
  71.  
  72. AVPacket pkt;
  73. av_init_packet(&pkt);
  74.  
  75. pkt.stream_index = videoStream->index;
  76.  
  77. XIRef<XMemory> frameBuffer = new XMemory;
  78.  
  79. resultParser->GetFrameData( &frameBuffer->Extend( resultParser->GetFrameSize() ),
  80. resultParser->GetFrameSize() );
  81.  
  82. pkt.data = frameBuffer->Map();
  83. pkt.size = frameBuffer->GetDataSize();
  84.  
  85. av_write_frame( formatContext, &pkt );
  86.  
  87. resultParser->Step();
  88. }
  89.  
  90. av_write_trailer( formatContext );
  91.  
  92. uint8_t* fileBytes = NULL;
  93. int fileSize = url_close_dyn_buf( formatContext->pb, &fileBytes );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement