File: ffmpeg_video_decoding2_ex.cpp

package info (click to toggle)
dlib 19.24.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 292,728 kB
  • sloc: cpp: 327,411; xml: 26,686; python: 1,631; sh: 290; java: 229; makefile: 179; javascript: 73; perl: 18
file content (99 lines) | stat: -rw-r--r-- 2,822 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

    This is an example illustrating the use of the ffmpeg wrappers, 
    in this case the decoding API.

    This is a pretty simple example. It loads a raw codec file, parses chunks of 
    data to the decoder, plots images to a GUI window, and counts audio samples.
*/

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

using namespace std;
using namespace dlib;
using namespace dlib::ffmpeg;

int main(const int argc, const char** argv)
try
{
    command_line_parser parser;
    parser.add_option("i",      "input video encoded stream. e.g. dlib/test/ffmpeg_data/MOT20-08-raw.h264", 1);
    parser.add_option("codec",  "codec name. e.g. h264", 1);
    parser.set_group_name("Help Options");
    parser.add_option("h",      "alias of --help");
    parser.add_option("help",   "display this message and exit");

    parser.parse(argc, argv);
    const char* one_time_opts[] = {"i"};
    parser.check_one_time_options(one_time_opts);

    if (parser.option("h") || parser.option("help"))
    {
        parser.print_options();
        return 0;
    }

    const std::string filepath = parser.option("i").argument();
    const std::string codec    = parser.option("codec").argument();

    decoder dec([&] {
        decoder::args args;
        args.codec_name = codec;
        return args;
    }());

    if (!dec.is_open())
    {
        printf("Failed to create decoder.\n");
        return EXIT_FAILURE;
    }

    frame                   f;
    array2d<rgb_pixel>      img;
    int                     samples{0};
    image_window            win;

    // When reading frames, we get exactly what's in the codec by default.
    // To resize, change pixel format, resample or change sample format, 
    // you have to pass extra arguments to wrap() which either resizes or resamples
    // the frame. Since we want rgb_pixel, we need to set the pixel format appropriately.
    const resizing_args args_image {0, 0, pix_traits<rgb_pixel>::fmt};

    const auto callback = [&](frame& f)
    {
        if (f.is_image())
        {
            convert(f, img);
            win.set_image(img);
        }
        else if (f.is_audio())
        {
            samples += f.nsamples();
        }
    };

    ifstream fin{filepath, std::ios::binary};
    std::vector<char> buf(1024);

    while (fin)
    {
        fin.read(buf.data(), buf.size());
        size_t ret = fin.gcount();
        dec.push((const uint8_t*)buf.data(), ret, wrap(callback, args_image));
    }

    dec.flush(wrap(callback, args_image));

    printf("Read %i audio samples\n", samples);

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