Support recording to MKV

Implement recording to Matroska files.

The format to use is determined by the option -F/--record-format if set,
or by the file extension (".mp4" or ".mkv").
This commit is contained in:
Romain Vimont
2019-02-09 15:20:07 +01:00
parent 1aaad6ba35
commit 0ed2373952
6 changed files with 100 additions and 11 deletions

View File

@@ -7,11 +7,13 @@
#include "config.h"
#include "log.h"
#include "recorder.h"
struct args {
const char *serial;
const char *crop;
const char *record_filename;
enum recorder_format record_format;
SDL_bool fullscreen;
SDL_bool help;
SDL_bool version;
@@ -42,6 +44,9 @@ static void usage(const char *arg0) {
" -f, --fullscreen\n"
" Start in fullscreen.\n"
"\n"
" -F, --record-format\n"
" Force recording format (either mp4 or mkv).\n"
"\n"
" -h, --help\n"
" Print this help.\n"
"\n"
@@ -57,6 +62,8 @@ static void usage(const char *arg0) {
"\n"
" -r, --record file.mp4\n"
" Record screen to file.\n"
" The format is determined by the -F/--record-format option if\n"
" set, or by the file extension (.mp4 or .mkv).\n"
"\n"
" -s, --serial\n"
" The device serial number. Mandatory only if several devices\n"
@@ -208,6 +215,36 @@ static SDL_bool parse_port(char *optarg, Uint16 *port) {
return SDL_TRUE;
}
static SDL_bool
parse_record_format(const char *optarg, enum recorder_format *format) {
if (!strcmp(optarg, "mp4")) {
*format = RECORDER_FORMAT_MP4;
return SDL_TRUE;
}
if (!strcmp(optarg, "mkv")) {
*format = RECORDER_FORMAT_MKV;
return SDL_TRUE;
}
LOGE("Unsupported format: %s (expected mp4 or mkv)", optarg);
return SDL_FALSE;
}
static enum recorder_format
guess_record_format(const char *filename) {
size_t len = strlen(filename);
if (len < 4) {
return 0;
}
const char *ext = &filename[len - 4];
if (!strcmp(ext, ".mp4")) {
return RECORDER_FORMAT_MP4;
}
if (!strcmp(ext, ".mkv")) {
return RECORDER_FORMAT_MKV;
}
return 0;
}
static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
static const struct option long_options[] = {
{"always-on-top", no_argument, NULL, 'T'},
@@ -218,13 +255,14 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
{"max-size", required_argument, NULL, 'm'},
{"port", required_argument, NULL, 'p'},
{"record", required_argument, NULL, 'r'},
{"record-format", required_argument, NULL, 'f'},
{"serial", required_argument, NULL, 's'},
{"show-touches", no_argument, NULL, 't'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0 },
};
int c;
while ((c = getopt_long(argc, argv, "b:c:fhm:p:r:s:tTv", long_options, NULL)) != -1) {
while ((c = getopt_long(argc, argv, "b:c:fF:hm:p:r:s:tTv", long_options, NULL)) != -1) {
switch (c) {
case 'b':
if (!parse_bit_rate(optarg, &args->bit_rate)) {
@@ -237,6 +275,11 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
case 'f':
args->fullscreen = SDL_TRUE;
break;
case 'F':
if (!parse_record_format(optarg, &args->record_format)) {
return SDL_FALSE;
}
break;
case 'h':
args->help = SDL_TRUE;
break;
@@ -276,6 +319,21 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
LOGE("Unexpected additional argument: %s", argv[index]);
return SDL_FALSE;
}
if (args->record_format && !args->record_filename) {
LOGE("Record format specified without recording");
return SDL_FALSE;
}
if (args->record_filename && !args->record_format) {
args->record_format = guess_record_format(args->record_filename);
if (!args->record_format) {
LOGE("No format specified for \"%s\" (try with -F mkv)",
args->record_filename);
return SDL_FALSE;
}
}
return SDL_TRUE;
}
@@ -290,6 +348,7 @@ int main(int argc, char *argv[]) {
.serial = NULL,
.crop = NULL,
.record_filename = NULL,
.record_format = 0,
.help = SDL_FALSE,
.version = SDL_FALSE,
.show_touches = SDL_FALSE,
@@ -329,6 +388,7 @@ int main(int argc, char *argv[]) {
.crop = args.crop,
.port = args.port,
.record_filename = args.record_filename,
.record_format = args.record_format,
.max_size = args.max_size,
.bit_rate = args.bit_rate,
.show_touches = args.show_touches,