File: ffmpeg_screen_grab_ex.cpp

package info (click to toggle)
dlib 20.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 293,056 kB
  • sloc: cpp: 331,568; xml: 27,095; python: 1,631; sh: 290; java: 229; makefile: 179; javascript: 73; perl: 18
file content (56 lines) | stat: -rw-r--r-- 1,327 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

    This is an example that illustrates how to use the ffmpeg wrappers
    for taking screen grabs and plotting to a GUI window.
*/

#include <cstdio>
#include <dlib/media.h>
#include <dlib/gui_widgets.h>

using namespace std;
using namespace dlib;

int main()
try
{
    const auto demuxers              = ffmpeg::list_demuxers();
    const bool screen_grab_available = std::find(begin(demuxers), end(demuxers), "x11grab") != demuxers.end();

    if (screen_grab_available)
    {
        ffmpeg::demuxer::args args;
        args.filepath       = "";
        args.input_format   = "x11grab";

        ffmpeg::demuxer cap(args);
        if (!cap.is_open() || !cap.video_enabled())
        {
            printf("Failed to open demuxer for screen grab\n");
            return EXIT_FAILURE;
        }

        image_window win;

        ffmpeg::frame frame;
        array2d<rgb_pixel> img;

        while (cap.read(frame))
        {
            convert(frame, img);
            win.set_image(img);
        }
    }
    else
    {
        printf("Sorry your installation of ffmpeg doesn't support screen grab\n");
    }

    return EXIT_SUCCESS;
}
catch (const std::exception& e)
{
    printf("%s\n", e.what());
    return EXIT_FAILURE;
}