The simplest FFmpeg-based AVDevice example (screen recording ),

Source: Internet
Author: User

The simplest FFmpeg-based AVDevice example (screen recording ),
FFmpeg has a class library that interacts with multimedia devices: Libavdevice. You can use this library to read data from multimedia devices on your computer or output data to a specified multimedia device.

We plan to write two examples of libavdevice class libraries related to FFmpeg. The previous article documented an example of using the Libavdevice class library based on FFmpeg to read camera data. This article records an example of recording screen of Libavdevice class library based on FFmpeg. In this article, the program records the current desktop content and decodes it to display it. The code for decoding and display is not described in this article. For details, refer to the article:
100-line code implementation of the simplest Video Player (SDL1.x) based on FFMPEG + SDL

Screen capture method

The previous article records how to use libavdevice. In Windows, you can use libavdevice to capture screen data in two ways: gdigrab and dshow. The following sections describe each other.
1. gdigrab
Gdigrab is a FFmpeg device used to capture Windows desktops. It is ideal for screen recording. It supports two methods of capturing through different input urls:
(1) "desktop": captures the entire desktop. Or capture a specific area on the desktop.
(2) "title = {window name}": captures a specific window on the screen (currently, Chinese Windows still have garbled characters ).
Gdigrab also supports some parameters to set the position of the screen capture:
Offset_x: the abscissa of the starting point of the screen capture.
Offset_y: The ordinate of the starting point of the screen capture.
Video_size: the size of the captured screen.
Framerate: Specifies the Frame Rate of the captured screen.
The reference code is as follows:

// Use gdigrab AVDictionary * options = NULL; // Set some options // grabbing frame rate // av_dict_set (& options, "framerate", "5", 0 ); // The distance from the left edge of the screen or desktop // av_dict_set (& options, "offset_x", "20", 0 ); // The distance from the top edge of the screen or desktop // av_dict_set (& options, "offset_y", "40", 0); // Video frame size. the default is to capture the full screen // av_dic T_set (& options, "video_size", "640x480", 0); AVInputFormat * ifmt = av_find_input_format ("gdigrab"); if (avformat_open_input (& pFormatCtx, "desktop ", ifmt, & options )! = 0) {printf ("Couldn't open input stream. (The input stream cannot be opened) \ n"); return-1 ;}

2. dshow
Screen capture software: screen-capture-recorder
Address: http://sourceforge.net/projects/screencapturer/
After installing the downloaded software, you can specify the dshow input device as "screen-capture-recorder. The usage of dshow devices has been described in detail in the previous article. The reference code is as follows:
AVInputFormat * ifmt = av_find_input_format ("dshow"); if (avformat_open_input (& pFormatCtx, "video = screen-capture-recorder", ifmt, NULL )! = 0) {printf ("Couldn't open input stream. (The input stream cannot be opened) \ n"); return-1 ;}

Note: The two screenshot capture methods can also be directly executed using the command line of ffmpeg.exe. For details, refer to the article:

Use FFmpeg to obtain data from the DirectShow device (camera, screen recording)

In Linux, you can use x11grab for screen capture.

Code

Paste the program code below:

/*** The Simplest FFmpeg-based AVDevice example (Screen recording) * Simplest FFmpeg Device (Screen Capture) ** leixiao Lei Xiaohua * leixiaohua1020@126.com * China Communications University/Digital TV Technology * Communication University of China/Digital TV Technology * http://blog.csdn.net/leixiaohua1020 ** this program implements the screen recording function. You can record and play desktop data. It is the simplest example of libavdevice class library based on FFmpeg. In this example, you can learn how to use * libavdevice class library in FFmpeg. * In Windows, you can use two recording methods: * 1. gdigrab: a GDI-based screen recording device under Win32. * When capturing a desktop, enter "desktop" as the URL ". * 2. dshow: Use Directshow. Note that you need to install additional software screen-capture-recorder * in Linux, you can use the x11grab recording screen. ** This software capture screen of computer. it's the simplest example * about usage of FFmpeg's libavdevice Library. * It's suiltable for the beginner of FFmpeg. * This software support 2 methods to capture screen in Microsoft Windows: * 1. gdigrab: Win32 GDI-based screen capture device. * Input URL in avformat_open_input () is "desktop ". * 2. dshow: Use Directshow. need to install screen-capture-r Ecorder. * It use x11grab to capture screen in Linux. */# include "stdafx. h "extern" C "{# include" libavcodec/avcodec. h "# include" libavformat/avformat. h "# include" libswscale/swscale. h "# include" libavdevice/avdevice. h "// SDL # include" sdl/SDL. h "# include" sdl/SDL_thread.h "}; // Output YUV420P # define OUTPUT_YUV420P 0 // '1' Use Dshow // '0' Use GDIgrab # define USE_DSHOW 0int main (int argc, char * argv []) {AVForm AtContext * pFormatCtx; inti, videoindex; AVCodecContext * pCodecCtx; AVCodec * pCodec; av_register_all (); avformat_network_init (); pFormatCtx = avformat_alloc_context (); // Open File // char filepath [] = "src0109480x272_22.h265"; // avformat_open_input (& pFormatCtx, filepath, NULL, NULL) // Register Deviceavdevice_register_all (); // Windows # ifdef _ WIN32 # if USE_DSHOW // Use dshow // Need to Install screen-capture-recorder // scre En-capture-recorder // Website: http://sourceforge.net/projects/screencapturer///AVInputFormat * ifmt = av_find_input_format ("dshow"); if (avformat_open_input (& pFormatCtx, "video = screen-capture-recorder", ifmt, NULL )! = 0) {printf ("Couldn't open input stream. (The input stream cannot be opened) \ n "); return-1 ;}# else // Use gdigrabAVDictionary * options = NULL; // Set some options // grabbing frame rate // av_dict_set (& options, "framerate", "5", 0 ); // The distance from the left edge of the screen or desktop // av_dict_set (& options, "offset_x", "20", 0 ); // The distance from the top edge of the screen or desktop // av_dict_set (& options, "offset_y", "40", 0); // Video fr Ame size. the default is to capture the full screen // av_dict_set (& options, "video_size", "640x480", 0); AVInputFormat * ifmt = av_find_input_format ("gdigrab "); if (avformat_open_input (& pFormatCtx, "desktop", ifmt, & options )! = 0) {printf ("Couldn't open input stream. (The input stream cannot be opened) \ n "); return-1 ;}# endif # else // LinuxAVDictionary * options = NULL; // Set some options // grabbing frame rate // av_dict_set (& options, "framerate", "5", 0 ); // Make the grabbed area follow the mouse // av_dict_set (& options, "follow_mouse", "centered", 0); // Video frame size. the default is to capture the full screen // av_dict_set (& options, "video_size", "640x480", 0); AVInput Format * ifmt = av_find_input_format ("x11grab"); // Grab at position 10, 20if (avformat_open_input (& pFormatCtx, ": 0.0 +", ifmt, & options )! = 0) {printf ("Couldn't open input stream. (The input stream cannot be opened) \ n "); return-1 ;}# endifif (avformat_find_stream_info (pFormatCtx, NULL) <0) {printf (" Couldn't find stream information. (stream information cannot be obtained) \ n "); return-1;} videoindex =-1; for (I = 0; I <pFormatCtx-> nb_streams; I ++) if (pFormatCtx-> streams [I]-> codec-> codec_type = AVMEDIA_TYPE_VIDEO) {videoindex = I; break;} if (videoindex =-1) {printf ("Didn't find a video stream. (Video Stream not found) \ n "); return- 1;} pCodecCtx = pFormatCtx-> streams [videoindex]-> codec; pCodec = avcodec_find_decoder (pCodecCtx-> codec_id); if (pCodec = NULL) {printf ("Codec not found. (decoder not found) \ n "); return-1;} if (avcodec_open2 (pCodecCtx, pCodec, NULL) <0) {printf (" cocould not open codec. (decoder cannot be enabled) \ n "); return-1;} AVFrame * pFrame, * pFrameYUV; pFrame = avcodec_alloc_frame (); pFrameYUV = avcodec_alloc_frame (); uint8_t * out_buffer = (uint8_t *) av_malloc (avpictu Re_get_size (bytes, pCodecCtx-> width, pCodecCtx-> height); avpicture_fill (AVPicture *) pFrameYUV, out_buffer, bytes, pCodecCtx-> width, pCodecCtx-> height ); // SDL encode if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) {printf ("cocould not initialize SDL-% s \ n", SDL_GetError (); return-1 ;} int screen_w = 640, screen_h = 360; const SDL_VideoInfo * vi = SD Rochelle getvideoinfo (); // Half of the Desktop's width and height. screen_w = vi-> current_w/2; screen_h = vi-> current_h/2; SDL_Surface * screen; screen = SDL_SetVideoMode (screen_w, screen_h, 0, 0); if (! Screen) {printf ("SDL: cocould not set video mode-exiting: % s \ n", SDL_GetError (); return-1;} SDL_Overlay * bmp; bmp = SDL_CreateYUVOverlay (pCodecCtx-> width, pCodecCtx-> height, accuracy, screen); SDL_Rect rect; // SDL End destination int ret, got_picture; AVPacket * packet = (AVPacket *) av_malloc (sizeof (AVPacket); // Output Information embedded printf ("File Information (File Information) --------------------- \ n"); av_dump_format (pFormatCtx, 0, NULL, 0 ); printf ("------------------------------------------------- \ n"); # if OUTPUT_YUV420P FILE * fp_yuv = fopen ("output. yuv "," wb + "); # endif struct SwsContext * img_convert_ctx; Convert = sws_getContext (pCodecCtx-> width, pCodecCtx-> height, pCodecCtx-> pix_fmt, pCodecCtx-> width, pCodecCtx-> height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL); // ------------------------------ while (av_read_frame (pFormatCtx, packet)> = 0) {if (packet-> stream_index = videoindex) {ret = avcodec_decode_video2 (pCodecCtx, pFrame, & got_picture, packet); if (ret <0) {printf ("Decode Error. (decoding error) \ n "); return-1;} if (got_picture) {sws_scale (img_convert_ctx, (const uint8_t * const *) pFrame-> data, pFrame-> linesize, 0, pCodecCtx-> height, pFrameYUV-> data, pFrameYUV-> linesize); # if OUTPUT_YUV420Pint y_size = pCodecCtx-> width * pCodecCtx-> height; fwrite (pFrameYUV-> data [0], 1, y_size, fp_yuv); // Y fwrite (pFrameYUV-> data [1], 1, y_size/4, fp_yuv ); // Ufwrite (pFrameYUV-> data [2], 1, y_size/4, fp_yuv); // V # endifSDL_LockYUVOverlay (bmp ); bmp-> pixels [0] = pFrameYUV-> data [0]; bmp-> pixels [2] = pFrameYUV-> data [1]; bmp-> pixels [1] = pFrameYUV-> data [2]; bmp-> pitches [0] = pFrameYUV-> linesize [0]; bmp-> pitches [2] = pFrameYUV-> linesize [1]; bmp-> pitches [1] = pFrameYUV-> linesize [2]; SDL_UnlockYUVOverlay (bmp); rect. x = 0; rect. y = 0; rect. w = screen_w; rect. h = screen_h; SDL_DisplayYUVOverlay (bmp, & rect); // Delay 40msSDL_Delay (40) ;}} av_free_packet (packet);} sws_freeContext (img_convert_ctx ); # if export fclose (fp_yuv); # endif SDL_Quit (); av_free (out_buffer); av_free (pFrameYUV); avcodec_close (pCodecCtx); avformat_close_input (& pFormatCtx); return 0 ;}

The result program runs as follows. The running result is very interesting, and a screen is "nested" in another screen.
You can determine whether to output the decoded YUV420P data into a file through the macro defined in the Code:
#define OUTPUT_YUV420P 0


SourceForge project homepage:
Https://sourceforge.net/projects/simplestffmpegdevice/
CSDN project:
Http://download.csdn.net/detail/leixiaohua1020/7994049

Note:
This project contains two examples of FFmpeg-based libavdevice:
Simplest_ffmpeg_grabdesktop: screen recording.
Simplest_ffmpeg_readcamera: Read the camera.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.