[Libav-user] Encoding a screenshot into a video using FFMPEG

Steffen Ebersbach ebersbac at hs-mittweida.de
Wed Apr 17 10:48:43 CEST 2013


Hi


this is my code for making a video from a screenshot. I do not use the
hbitmap directly, but rather a gdi element. But i think this is not
necessary i do this or other reasons.  One Idea, because in your last
picture the shell window is shown but the visual studio window is
missing, can be that the copy desktop / window function from windows
can't capy elements from directx and so on , only nativ stuff. This is
because in most remote control softwares, no video is transmitted.
I hope the codes help you.

Steffen

--------------------------------------------------
HWND window;
// get hwnd from your window / desktop / ...

srcDC = GetDC(window);
tarDC = CreateCompatibleDC(srcDC);
m_pal = (HPALETTE) GetCurrentObject(srcDC, OBJ_PAL); //colors

HBITMAP obmp, hbmp = CreateCompatibleBitmap(srcDC, pwidth , pheight);
obmp = (HBITMAP) SelectObject(tarDC, hbmp); //connect hbmp to DC

res = SendMessage(window,WM_PRINT, (WPARAM)tarDC,(LPARAM) PRF_CLIENT  |
~PRF_ERASEBKGND); // Copy BMP
tarbmp = Gdiplus::Bitmap::FromHBITMAP(hbmp,m_pal);

SelectObject(tarDC,obmp);
DeleteObject(hbmp);


//Stream and encoder
av_register_all();
avcodec_init();

AVFormatContext *focontext;
AVStream *videostm;
AVCodec *videocodec;
AVCodecContext *videocontext;
AVFrame *aktframe;
uint8_t *framebuf1;
SwsContext *imgconvctx;

//container
focontext = av_alloc_format_context();

//videostream
videostm = av_new_stream(focontext,0);
// define your parameters here

videocodec = avcodec_find_encoder(CODEC_ID_MPEG2VIDEO);
avcodec_open(videocontext, videocodec);

focontext->video_codec_id = videocontext->codec_id;
av_set_parameters(focontext,0);


//frame
aktframe = avcodec_alloc_frame();
picsize = avpicture_get_size(PIX_FMT_YUV420P, width, height);
framebuf1 = (uint8_t*) av_malloc(picsize);
avpicture_fill( (AVPicture*)aktframe, framebuf1, PIX_FMT_YUV420P, width,
height);

imgconvctx = sws_getContext(width, height ,PIX_FMT_BGR24 ,width,
height,PIX_FMT_YUV420P, SWS_BICUBIC , 0,0,0);

//convert bmp
uint8_t *inbuffer;
Gdiplus::BitmapData inbmpdata;
Gdiplus::Rect cltrct(0,0,width,height);

newbmp->LockBits(&cltrct, Gdiplus::ImageLockModeRead
,PixelFormat24bppRGB , &inbmpdata);

inbuffer = (uint8_t*) inbmpdata.Scan0;
AVFrame *inframe = 0;

inframe = avcodec_alloc_frame();
avpicture_fill( (AVPicture*)inframe, inbuffer, PIX_FMT_BGR24, width,
height);
sws_scale(imgconvctx,inframe->data , inframe->linesize,0, height,
aktframe->data , aktframe->linesize);
int videobuf_size = videocontext->bit_rate *2;

//open output
url_fopen(&focontext->pb,"file.mpg" , URL_WRONLY)
av_write_header(focontext);

//encode and write frame
av_init_packet(&avpkt);
encsize = avcodec_encode_video(videocontext, videobuf ,videobuf_size,
aktframe);
avpkt.stream_index= videostm->index;       
avpkt.data= videobuf;
avpkt.size= encsize;
   
av_write_frame(focontext, &avpkt);

// next frames


More information about the Libav-user mailing list