Advertisement
Guest User

Untitled

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