Advertisement
Guest User

Broken

a guest
Nov 22nd, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. int VideoTest::convert_first_frame_to_png(const char* inputVideoFileName, const char* outputPngName)
  2. {
  3.     av_register_all();
  4.     avcodec_register_all();
  5.     avformat_network_init();
  6.     av_log_set_level(AV_LOG_DEBUG); // debugging info
  7.  
  8.     ::AVFormatContext * ctx = NULL;
  9.     int err = avformat_open_input(&ctx, inputVideoFileName, NULL, NULL);
  10.     CHECK_ERR(err);
  11.     err = av_find_stream_info(ctx);
  12.     CHECK_ERR(err);
  13.  
  14.     AVCodec * codec = NULL;
  15.     int strm = av_find_best_stream(ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &codec, 0);
  16.  
  17.     AVCodecContext * codecCtx = ctx->streams[strm]->codec;
  18.     err = avcodec_open2(codecCtx, codec, NULL);
  19.     CHECK_ERR(err);
  20.  
  21.     SwsContext * swCtx = sws_getContext(
  22.         codecCtx->width,
  23.         codecCtx->height,
  24.         codecCtx->pix_fmt,
  25.         640,
  26.         480,
  27.         PIX_OUT_FORMAT,
  28.         SWS_FAST_BILINEAR, 0, 0, 0);
  29.  
  30.     for (;;)
  31.     {
  32.         AVPacket pkt;
  33.         err = av_read_frame(ctx, &pkt);
  34.         CHECK_ERR(err);
  35.  
  36.         if (pkt.stream_index == strm)
  37.         {
  38.             int got = 0;
  39.             AVFrame * frame = avcodec_alloc_frame();
  40.             err = avcodec_decode_video2(codecCtx, frame, &got, &pkt);
  41.             CHECK_ERR(err);
  42.  
  43.             if (got)
  44.             {
  45.                 AVFrame * rgbFrame = avcodec_alloc_frame();
  46.                 avpicture_alloc((AVPicture *)rgbFrame, PIX_OUT_FORMAT, 640, 480);
  47.                 sws_scale(swCtx, frame->data, frame->linesize, 0, frame->height, rgbFrame->data, rgbFrame->linesize);
  48.  
  49.                 AVCodec *outCodec = avcodec_find_encoder(AV_CODEC_ID_PNG);
  50.                 AVCodecContext *outCodecCtx = avcodec_alloc_context3(outCodec);
  51.                 outCodecCtx->width = 640;
  52.                 outCodecCtx->height = 480;
  53.                 outCodecCtx->pix_fmt = PIX_OUT_FORMAT;
  54.                 outCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
  55.                 outCodecCtx->time_base.num = codecCtx->time_base.num;
  56.                 outCodecCtx->time_base.den = codecCtx->time_base.den;
  57.                 CheckResult("Open PNG codec", avcodec_open2(outCodecCtx, outCodec, NULL));
  58.  
  59.                 if (!outCodec) return -1;
  60.                 int res = avcodec_open2(outCodecCtx, outCodec, NULL);
  61.                 if (res < 0) return -1;
  62.  
  63.                 AVPacket outPacket;
  64.                 av_init_packet(&outPacket);
  65.                 outPacket.size = 0;
  66.                 outPacket.data = NULL;
  67.                 int gotFrame = 0;
  68.                 int ret = avcodec_encode_video2(outCodecCtx, &outPacket, rgbFrame, &gotFrame);
  69.                 if (ret >= 0 && gotFrame)
  70.                 {
  71.                     FILE * outPng = fopen(outputPngName, "wb");
  72.                     fwrite(outPacket.data, outPacket.size, 1, outPng);
  73.                     fclose(outPng);
  74.                 }
  75.  
  76.                 avcodec_close(outCodecCtx);
  77.                 av_free(outCodecCtx);
  78.  
  79.                 break;
  80.             }
  81.             avcodec_free_frame(&frame);
  82.         }
  83.     }
  84.     avformat_network_deinit();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement