File: ffmpeg_file_to_speaker_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 (90 lines) | stat: -rw-r--r-- 2,546 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
// 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.
    It attempts to read audio from a microphone if available, and saves the audio to wav.
*/

#include <iostream>
#include <chrono>
#include <dlib/media.h>
#include <dlib/cmd_line_parser.h>

using namespace std;
using namespace std::chrono;
using namespace std::chrono_literals;
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 audio file", 1);
    parser.add_option("o",      "output audio device (speaker). E.g. hw:0,0", 1);
    parser.add_option("codec",  "audio codec. E.g. pcm_s16le", 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", "o", "codec"};
    parser.check_one_time_options(one_time_opts);

    if (parser.option("h") || parser.option("help"))
    {
        parser.print_options();
        return 0;
    }
    
    const std::string filename  = get_option(parser, "i",       "");
    const std::string device    = get_option(parser, "o",       "hw:0,0");
    const std::string codec     = get_option(parser, "codec",   "");

    if (device.empty())
    {
        cout << "Didn't find a speaker. Exiting.\n";
        return EXIT_FAILURE;
    }

    // Open file
    demuxer cap({filename, video_disabled, audio_enabled});

    if (!cap.is_open())
    {
        cout << "Failed to open " << device << endl;
        return EXIT_FAILURE;
    }

    // Create writer to speaker
    muxer writer([&] {
        muxer::args args;
        args.filepath                   = device;
        args.output_format              = "alsa";
        args.enable_image               = false;
        args.args_audio.codec_name      = codec;
        args.args_audio.sample_rate     = 44100;
        args.args_audio.channel_layout  = AV_CH_LAYOUT_STEREO;
        args.args_audio.fmt             = cap.sample_fmt();
        return args;
    }());

    if (!writer.is_open())
    {
        cout << "Failed to open wav file" << endl;
        return EXIT_FAILURE;
    }

    // Pull and push
    frame f;
    while (cap.read(f))
        writer.push(std::move(f));

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