dlib/examples/ffmpeg_microphone_to_file_e...

91 lines
2.8 KiB
C++
Raw Normal View History

// 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("t", "capture time in seconds", 1);
parser.add_option("i", "input microphone device. E.g. hw:0,0", 1);
[FFmpeg] decoding and demuxing improvements (#2784) * typo * - added compile time information to audio object. Not convinced this is needed actually. I'm perfectly happy just using the ffmpeg::frame object. I'm pretty sure I'm the only user who cares about audio. - created resizing_args and resampling_args * smaller videos for unit tests * shorter videos for unit tests * - decoder and demuxer: you now resize or resample at the time of read. therefore you don't set resizing or resampling parameters in constructor, but you pass them to read() - added templated read() function - simplified load_frame() * inherit from resizing_args and resampling_args * reorganised the tests to segragate decoding, demuxing, encoding and muxing as much as possible * much more basic example * demxing examples split * examples * fixing examples * wip * Fix load_frame() * added frame - specific tests * - makes sense to have a set_params() method rather than constructing a new object and moving. I mean, it works and it absolutely does the right thing, and in fact the same thing as calling set_params() now, but it can look a bit weird. * notes on defaults and good pairings * Update ffmpeg_demuxer.h Watch out for `DLIB_ASSERT` statements. Maybe one of the unit tests should build with asserts enabled. * Update ffmpeg_details.h * Update ffmpeg_muxer.h * WIP * WIP * - simplified details::resizer - added frame::set_params() - added frame::clear() - forward packet directly into correct queue * pick best codec if not specified * added image data * warn when we're choosing an appropriate codec * test load_frame() * - for some reason, you sometimes get warning messages about too many b-frames. Resetting pict_type suppresses this. - you can move freshly decoded frames directly out. * callback passed to push() * I think it's prettier this way * WIP * full callback API for decoder * updated tests * updated example * check the template parameter is callable and has 1 argument first before getting it's first argument * Potential bug fix * - write out the enable_if's explictly. It's fine. I think it's clear what's going on if someone cares - guard push() with a boolean which asserts when recursion is detected * pre-conditions on callbacks: no recursion --------- Co-authored-by: pf <pf@me> Co-authored-by: Your name <you@example.com>
2023-05-16 09:24:47 +08:00
parser.add_option("o", "output audio file. E.g. recording.m4a, recording.wav. Default: recording.m4a", 1);
parser.add_option("codec", "audio codec. E.g. `aac`, `pcm_s16le`. Recommend `pcm_s16le` for WAV files and `aac` for `M4A` files. Default: `aac`", 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[] = {"t", "i", "o", "codec"};
parser.check_one_time_options(one_time_opts);
if (parser.option("h") || parser.option("help"))
{
parser.print_options();
return 0;
}
const seconds time{get_option(parser, "t", 1)};
const std::string device = get_option(parser, "i", "hw:0,0");
const std::string filename = get_option(parser, "o", "recording.m4a");
const std::string codec = get_option(parser, "codec", "aac");
// Open microphone
demuxer cap([&] {
demuxer::args args;
args.filepath = device;
args.input_format = "alsa";
return args;
}());
if (!cap.is_open())
{
cout << "Failed to open device: " << device << '\n';
return EXIT_FAILURE;
}
// Create WAV file
muxer writer([&] {
muxer::args args;
args.filepath = filename;
args.enable_image = false;
args.args_audio.codec_name = codec;
args.args_audio.sample_rate = cap.sample_rate();
args.args_audio.channel_layout = cap.channel_layout();
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
const auto start = high_resolution_clock::now();
frame f;
while (cap.read(f) && (high_resolution_clock::now() - start) < time)
writer.push(std::move(f));
return EXIT_SUCCESS;
}
catch (const std::exception& e)
{
printf("%s\n", e.what());
return EXIT_FAILURE;
}