# API documentation for libmpg123, libout123, and libsyn123

Note: This API doc is automatically generated from the current development version that you can get via Subversion or as a daily snapshot from http://mpg123.org/snapshot. There may be differences (additions) compared to the latest stable release. See NEWS.libmpg123, NEWS.libout123, NEWS.libsyn123, and the overall NEWS file on libmpg123 versions and important changes between them.
Let me emphasize that the policy for the lib*123 family is to always stay backwards compatible -- only additions are planned (and it's not yet planned to change the plans;-).
extract_frames.c
Go to the documentation of this file.
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 This is example code only sensible to be considered in the public domain.
5 Initially written by Thomas Orgis.
6 */
7
8 #include <mpg123.h>
9
10 /* unistd.h is not available under MSVC,
11 io.h defines the read and write functions */
12 #ifndef _MSC_VER
13 #include <unistd.h>
14 #else
15 #include <io.h>
16 #endif
17
18 #include <stdio.h>
19 #include <string.h>
20
22 int do_work(mpg123_handle *m);
23
25 int main(int argc, char **argv)
26 {
27 int ret = 0;
28 mpg123_handle *m;
29
30 #if MPG123_API_VERSION < 46
31 // Newer versions of the library don't need that anymore, but it is safe
32 // to have the no-op call present for compatibility with old versions.
33 mpg123_init();
34 #endif
35 m = mpg123_new(NULL, &ret);
36
37 if(m == NULL)
38 {
39 fprintf(stderr, "Cannot create handle: %s", mpg123_plain_strerror(ret));
40 }
41 else
42 {
43 fprintf(stderr, "I'll take your dirty MPEG audio from standard input and will write the extracted pure MPEG data to standard output.\n");
44 if(argc > 1 && strcmp(argv[1], "--noinfo") == 0)
45 {
46 fprintf(stderr, "Enabling parsing/consuming of the Info frame so that it will not appear in output.\n");
47 ret = mpg123_param(m, MPG123_REMOVE_FLAGS, MPG123_IGNORE_INFOFRAME, 0.);
48 }
49 else
50 {
51 fprintf(stderr, "If you'd have given --noinfo as argument, I would omit a LAME/Xing info frame.\n");
52 ret = mpg123_param(m, MPG123_ADD_FLAGS, MPG123_IGNORE_INFOFRAME, 0.);
53 }
54 if(ret == 0) ret = do_work(m);
55
56 if(ret != 0) fprintf(stderr, "Some error occured: %s\n", mpg123_strerror(m));
57
58
59 mpg123_delete(m); /* Closes, too. */
60 }
61
62 return ret;
63 }
64
65 int do_work(mpg123_handle *m)
66 {
67 int ret;
68 size_t count = 0;
69 ret = mpg123_open_fd(m, STDIN_FILENO);
70 if(ret != MPG123_OK) return ret;
71
72 ssize_t wret = 0;
73 while( !wret && (ret = mpg123_framebyframe_next(m)) == MPG123_OK || ret == MPG123_NEW_FORMAT )
74 {
75 unsigned long header;
76 unsigned char *bodydata;
77 size_t bodybytes;
78 if(mpg123_framedata(m, &header, &bodydata, &bodybytes) == MPG123_OK)
79 {
80 /* Need to extract the 4 header bytes from the native storage in the correct order. */
81 unsigned char hbuf[4];
82 int i;
83 for(i=0; i<4; ++i) hbuf[i] = (unsigned char) ((header >> ((3-i)*8)) & 0xff);
84
85 /* Now write out both header and data, fire and forget. */
86 wret = write(STDOUT_FILENO, hbuf, 4);
87 if(!wret)
88 wret = write(STDOUT_FILENO, bodydata, bodybytes);
89 fprintf(stderr, "%zu: header 0x%08lx, %zu body bytes\n", ++count, header, bodybytes);
90 }
91 }
92
93 if(ret != MPG123_DONE)
94 fprintf(stderr, "Some error occured (non-fatal?): %s\n", mpg123_strerror(m));
95 if(wret)
96 {
97 fprintf(stderr, "Write error.\n");
98 ret = MPG123_ERR;
99 }
100
101 fprintf(stderr, "Done with %zu MPEG frames.\n", count);
102
103 return 0;
104 }
MPG123_REMOVE_FLAGS
@ MPG123_REMOVE_FLAGS
Definition: mpg123.h:300
mpg123_plain_strerror
const MPG123_EXPORT char * mpg123_plain_strerror(int errcode)
mpg123_framebyframe_next
MPG123_EXPORT int mpg123_framebyframe_next(mpg123_handle *mh)
mpg123_delete
MPG123_EXPORT void mpg123_delete(mpg123_handle *mh)
MPG123_ERR
@ MPG123_ERR
Definition: mpg123.h:532
mpg123_framedata
MPG123_EXPORT int mpg123_framedata(mpg123_handle *mh, unsigned long *header, unsigned char **bodydata, size_t *bodybytes)
MPG123_ADD_FLAGS
@ MPG123_ADD_FLAGS
Definition: mpg123.h:288
MPG123_IGNORE_INFOFRAME
@ MPG123_IGNORE_INFOFRAME
Definition: mpg123.h:331
do_work
int do_work(mpg123_handle *m)
Definition: extract_frames.c:65
mpg123_handle
struct mpg123_handle_struct mpg123_handle
Definition: mpg123.h:222
mpg123_strerror
const MPG123_EXPORT char * mpg123_strerror(mpg123_handle *mh)
mpg123_new
MPG123_EXPORT mpg123_handle * mpg123_new(const char *decoder, int *error)
mpg123.h
main
int main(int argc, char **argv)
Definition: extract_frames.c:25
MPG123_NEW_FORMAT
@ MPG123_NEW_FORMAT
Definition: mpg123.h:530
mpg123_open_fd
MPG123_EXPORT int mpg123_open_fd(mpg123_handle *mh, int fd)
mpg123_init
MPG123_EXPORT int mpg123_init(void)
MPG123_DONE
@ MPG123_DONE
Definition: mpg123.h:529
MPG123_OK
@ MPG123_OK
Definition: mpg123.h:533
mpg123_param
MPG123_EXPORT int mpg123_param(mpg123_handle *mh, enum mpg123_parms type, long value, double fvalue)