| 1 |
/* |
| 2 |
extract_frams: utlize the framebyframe API and mpg123_framedata to extract the MPEG frames out of a stream (strip off anything else). |
| 3 |
|
| 4 |
copyright 2011-2023 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 5 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 6 |
initially written by Thomas Orgis |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "mpg123config.h" |
| 10 |
#include "version.h" |
| 11 |
#include "compat/compat.h" |
| 12 |
|
| 13 |
#include <mpg123.h> |
| 14 |
|
| 15 |
#include "getlopt.h" |
| 16 |
|
| 17 |
|
| 18 |
static struct |
| 19 |
{ |
| 20 |
int info; |
| 21 |
long icy_interval; |
| 22 |
int verbose; |
| 23 |
} param = |
| 24 |
{ |
| 25 |
TRUE |
| 26 |
,0 |
| 27 |
,0 |
| 28 |
}; |
| 29 |
|
| 30 |
static const char* progname; |
| 31 |
|
| 32 |
static void usage(int err) |
| 33 |
{ |
| 34 |
FILE* o = stdout; |
| 35 |
if(err) |
| 36 |
{ |
| 37 |
o = stderr; |
| 38 |
fprintf(o, "You made some mistake in program usage... let me briefly remind you:\n\n"); |
| 39 |
} |
| 40 |
fprintf(o, "Extract only MPEG frames from a stream using libmpg123 (stdin to stdout)\n"); |
| 41 |
fprintf(o, "\tversion %s; written and copyright by Thomas Orgis and the mpg123 project\n", MPG123_VERSION); |
| 42 |
fprintf(o,"\nusage: %s [option(s)] < input > output\n", progname); |
| 43 |
fprintf(o,"\noptions:\n"); |
| 44 |
fprintf(o," -h --help give usage help\n"); |
| 45 |
fprintf(o," -i <n> --icy-interval <n> stream has ICY metadata present with this interval\n"); |
| 46 |
fprintf(o," -n --no-info also strip info frame at beginning\n"); |
| 47 |
fprintf(o," -v[*] --verbose increase verbosity level\n"); |
| 48 |
exit(err); |
| 49 |
} |
| 50 |
|
| 51 |
static void want_usage(char* bla, topt *opts) |
| 52 |
{ |
| 53 |
usage(0); |
| 54 |
} |
| 55 |
|
| 56 |
static void set_verbose (char *arg, topt *opts) |
| 57 |
{ |
| 58 |
param.verbose++; |
| 59 |
} |
| 60 |
|
| 61 |
static topt opts[] = |
| 62 |
{ |
| 63 |
{'h', "help", 0, want_usage, 0, 0} |
| 64 |
,{'i', "icy-interval", GLO_ARG|GLO_LONG, 0, ¶m.icy_interval, 0} |
| 65 |
,{'n', "no-info", GLO_INT, 0, ¶m.info, FALSE} |
| 66 |
,{'v', "verbose", 0, set_verbose, 0, 0} |
| 67 |
,{0, 0, 0, 0, 0, 0} |
| 68 |
}; |
| 69 |
|
| 70 |
int do_work(mpg123_handle *m); |
| 71 |
|
| 72 |
int main(int argc, char **argv) |
| 73 |
{ |
| 74 |
int ret = 0; |
| 75 |
mpg123_handle *m; |
| 76 |
|
| 77 |
progname = argv[0]; |
| 78 |
|
| 79 |
while ((ret = getlopt(argc, argv, opts))) |
| 80 |
switch (ret) { |
| 81 |
case GLO_UNKNOWN: |
| 82 |
fprintf (stderr, "%s: Unknown option \"%s\".\n", |
| 83 |
progname, loptarg); |
| 84 |
usage(1); |
| 85 |
case GLO_NOARG: |
| 86 |
fprintf (stderr, "%s: Missing argument for option \"%s\".\n", |
| 87 |
progname, loptarg); |
| 88 |
usage(1); |
| 89 |
} |
| 90 |
|
| 91 |
mpg123_init(); |
| 92 |
m = mpg123_new(NULL, &ret); |
| 93 |
|
| 94 |
if(m == NULL) |
| 95 |
{ |
| 96 |
fprintf(stderr, "Cannot create handle: %s", mpg123_plain_strerror(ret)); |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
ret = mpg123_param(m, MPG123_VERBOSE, param.verbose, 0.); |
| 101 |
if(ret == MPG123_OK) |
| 102 |
mpg123_param(m, MPG123_RESYNC_LIMIT, -1, 0.0); |
| 103 |
if(ret == MPG123_OK) |
| 104 |
{ |
| 105 |
if(param.verbose) |
| 106 |
fprintf(stderr, "Info frame handling: %s\n", |
| 107 |
param.info ? "pass-through" : "remove"); |
| 108 |
ret = param.info /* If info frame is ignored, it is treated as MPEG data. */ |
| 109 |
? mpg123_param(m, MPG123_ADD_FLAGS, MPG123_IGNORE_INFOFRAME, 0.) |
| 110 |
: mpg123_param(m, MPG123_REMOVE_FLAGS, MPG123_IGNORE_INFOFRAME, 0.); |
| 111 |
} |
| 112 |
if(ret == MPG123_OK && param.icy_interval > 0) |
| 113 |
{ |
| 114 |
if(param.verbose) fprintf(stderr, "ICY interval: %li\n", param.icy_interval); |
| 115 |
ret = mpg123_param(m, MPG123_ICY_INTERVAL, param.icy_interval, 0); |
| 116 |
} |
| 117 |
|
| 118 |
if(ret == MPG123_OK) ret = do_work(m); |
| 119 |
|
| 120 |
if(ret != MPG123_OK && mpg123_errcode(m)) |
| 121 |
fprintf(stderr, "Some error occured: %s\n", mpg123_strerror(m)); |
| 122 |
|
| 123 |
mpg123_delete(m); /* Closes, too. */ |
| 124 |
} |
| 125 |
mpg123_exit(); |
| 126 |
|
| 127 |
return ret; |
| 128 |
} |
| 129 |
|
| 130 |
int do_work(mpg123_handle *m) |
| 131 |
{ |
| 132 |
int ret; |
| 133 |
size_t count = 0; |
| 134 |
INT123_compat_binmode(STDIN_FILENO, TRUE); |
| 135 |
INT123_compat_binmode(STDOUT_FILENO, TRUE); |
| 136 |
ret = mpg123_open_fd(m, STDIN_FILENO); |
| 137 |
if(ret != MPG123_OK) return ret; |
| 138 |
|
| 139 |
while( (ret = mpg123_framebyframe_next(m)) == MPG123_OK || ret == MPG123_NEW_FORMAT ) |
| 140 |
{ |
| 141 |
unsigned long header; |
| 142 |
unsigned char *bodydata; |
| 143 |
size_t bodybytes; |
| 144 |
if(mpg123_framedata(m, &header, &bodydata, &bodybytes) == MPG123_OK) |
| 145 |
{ |
| 146 |
/* Need to extract the 4 header bytes from the native storage in the correct order. */ |
| 147 |
unsigned char hbuf[4]; |
| 148 |
int i; |
| 149 |
for(i=0; i<4; ++i) hbuf[i] = (unsigned char) ((header >> ((3-i)*8)) & 0xff); |
| 150 |
|
| 151 |
/* Now write out both header and data, fire and forget. */ |
| 152 |
if( 4 != INT123_unintr_write(STDOUT_FILENO, hbuf, 4) || |
| 153 |
bodybytes != INT123_unintr_write(STDOUT_FILENO, bodydata, bodybytes) ) |
| 154 |
{ |
| 155 |
fprintf(stderr, "Failed to write data: %s\n", INT123_strerror(errno)); |
| 156 |
return MPG123_ERR; |
| 157 |
} |
| 158 |
if(param.verbose) |
| 159 |
fprintf(stderr, "%zu: header 0x%08lx, %zu body bytes\n" |
| 160 |
, ++count, header, bodybytes); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
if(ret != MPG123_DONE) |
| 165 |
fprintf(stderr, "Some error occured (non-fatal?): %s\n", mpg123_strerror(m)); |
| 166 |
|
| 167 |
if(param.verbose) |
| 168 |
fprintf(stderr, "Done with %zu MPEG frames.\n", count); |
| 169 |
|
| 170 |
return MPG123_OK; |
| 171 |
} |