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;-).
id3dump.c
Go to the documentation of this file.
1 /*
2  id3dump: Print ID3 tags of files, scanned using libmpg123.
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 #include <string.h>
10 #include "stdio.h"
11 #include "sys/types.h"
12 
14 void safe_print(char* name, char *data, size_t size)
15 {
16  char safe[31];
17  if(size>30) return;
18 
19  memcpy(safe, data, size);
20  safe[size] = 0;
21  printf("%s: %s\n", name, safe);
22 }
23 
26 {
27  safe_print("Title", v1->title, sizeof(v1->title));
28  safe_print("Artist", v1->artist, sizeof(v1->artist));
29  safe_print("Album", v1->album, sizeof(v1->album));
30  safe_print("Year", v1->year, sizeof(v1->year));
31  safe_print("Comment", v1->comment, sizeof(v1->comment));
32  printf("Genre: %i", v1->genre);
33 }
34 
37 void print_lines(const char* prefix, mpg123_string *inlines)
38 {
39  size_t i;
40  int hadcr = 0, hadlf = 0;
41  char *lines = NULL;
42  char *line = NULL;
43  size_t len = 0;
44 
45  if(inlines != NULL && inlines->fill)
46  {
47  lines = inlines->p;
48  len = inlines->fill;
49  }
50  else return;
51 
52  line = lines;
53  for(i=0; i<len; ++i)
54  {
55  if(lines[i] == '\n' || lines[i] == '\r' || lines[i] == 0)
56  {
57  char save = lines[i]; /* saving, changing, restoring a byte in the data */
58  if(save == '\n') ++hadlf;
59  if(save == '\r') ++hadcr;
60  if((hadcr || hadlf) && hadlf % 2 == 0 && hadcr % 2 == 0) line = "";
61 
62  if(line)
63  {
64  lines[i] = 0;
65  printf("%s%s\n", prefix, line);
66  line = NULL;
67  lines[i] = save;
68  }
69  }
70  else
71  {
72  hadlf = hadcr = 0;
73  if(line == NULL) line = lines+i;
74  }
75  }
76 }
77 
80 {
81  print_lines("Title: ", v2->title);
82  print_lines("Artist: ", v2->artist);
83  print_lines("Album: ", v2->album);
84  print_lines("Year: ", v2->year);
85  print_lines("Comment: ", v2->comment);
86  print_lines("Genre: ", v2->genre);
87 }
88 
91 {
92  size_t i;
93  for(i=0; i<v2->texts; ++i)
94  {
95  char id[5];
96  char lang[4];
97  memcpy(id, v2->text[i].id, 4);
98  id[4] = 0;
99  memcpy(lang, v2->text[i].lang, 3);
100  lang[3] = 0;
101  if(v2->text[i].description.fill)
102  printf("%s language(%s) description(%s)\n", id, lang, v2->text[i].description.p);
103  else printf("%s language(%s)\n", id, lang);
104 
105  print_lines(" ", &v2->text[i].text);
106  }
107  for(i=0; i<v2->extras; ++i)
108  {
109  char id[5];
110  memcpy(id, v2->extra[i].id, 4);
111  id[4] = 0;
112  printf( "%s description(%s)\n",
113  id,
114  v2->extra[i].description.fill ? v2->extra[i].description.p : "" );
115  print_lines(" ", &v2->extra[i].text);
116  }
117  for(i=0; i<v2->comments; ++i)
118  {
119  char id[5];
120  char lang[4];
121  memcpy(id, v2->comment_list[i].id, 4);
122  id[4] = 0;
123  memcpy(lang, v2->comment_list[i].lang, 3);
124  lang[3] = 0;
125  printf( "%s description(%s) language(%s): \n",
126  id,
127  v2->comment_list[i].description.fill ? v2->comment_list[i].description.p : "",
128  lang );
129  print_lines(" ", &v2->comment_list[i].text);
130  }
131 }
132 
134 int main(int argc, char **argv)
135 {
136  int i;
137  mpg123_handle* m;
138  if(argc < 2)
139  {
140  fprintf(stderr, "\nI will print some ID3 tag fields of MPEG audio files.\n");
141  fprintf(stderr, "\nUsage: %s <mpeg audio file list>\n\n", argv[0]);
142  return -1;
143  }
144 #if MPG123_API_VERSION < 46
145  // Newer versions of the library don't need that anymore, but it is safe
146  // to have the no-op call present for compatibility with old versions.
147  mpg123_init();
148 #endif
149  m = mpg123_new(NULL, NULL);
150 
151  for(i=1; i < argc; ++i)
152  {
153  mpg123_id3v1 *v1;
154  mpg123_id3v2 *v2;
155  int meta;
156  if(mpg123_open(m, argv[i]) != MPG123_OK)
157  {
158  fprintf(stderr, "Cannot open %s: %s\n", argv[i], mpg123_strerror(m));
159  continue;
160  }
161  mpg123_scan(m);
162  meta = mpg123_meta_check(m);
163  if(meta & MPG123_ID3 && mpg123_id3(m, &v1, &v2) == MPG123_OK)
164  {
165  printf("Tag data on %s:\n", argv[i]);
166  printf("\n==== ID3v1 ====\n");
167  if(v1 != NULL) print_v1(v1);
168 
169  printf("\n==== ID3v2 ====\n");
170  if(v2 != NULL) print_v2(v2);
171 
172  printf("\n==== ID3v2 Raw frames ====\n");
173  if(v2 != NULL) print_raw_v2(v2);
174  }
175  else printf("Nothing found for %s.\n", argv[i]);
176 
177  mpg123_close(m);
178  }
179  mpg123_delete(m);
180  return 0;
181 }
MPG123_EXPORT const char * mpg123_strerror(mpg123_handle *mh)
@ MPG123_OK
Definition: mpg123.h:471
MPG123_EXPORT mpg123_handle * mpg123_new(const char *decoder, int *error)
MPG123_EXPORT void mpg123_delete(mpg123_handle *mh)
struct mpg123_handle_struct mpg123_handle
Definition: mpg123.h:164
MPG123_EXPORT int mpg123_init(void)
MPG123_EXPORT int mpg123_close(mpg123_handle *mh)
MPG123_EXPORT int mpg123_open(mpg123_handle *mh, const char *path)
MPG123_EXPORT int mpg123_meta_check(mpg123_handle *mh)
MPG123_EXPORT int mpg123_id3(mpg123_handle *mh, mpg123_id3v1 **v1, mpg123_id3v2 **v2)
#define MPG123_ID3
Definition: mpg123.h:1942
MPG123_EXPORT int mpg123_scan(mpg123_handle *mh)
int main(int argc, char **argv)
Definition: id3dump.c:134
void safe_print(char *name, char *data, size_t size)
Definition: id3dump.c:14
void print_raw_v2(mpg123_id3v2 *v2)
Definition: id3dump.c:90
void print_v2(mpg123_id3v2 *v2)
Definition: id3dump.c:79
void print_lines(const char *prefix, mpg123_string *inlines)
Definition: id3dump.c:37
void print_v1(mpg123_id3v1 *v1)
Definition: id3dump.c:25
char title[30]
Definition: mpg123.h:1934
char artist[30]
Definition: mpg123.h:1935
char comment[30]
Definition: mpg123.h:1938
char year[4]
Definition: mpg123.h:1937
unsigned char genre
Definition: mpg123.h:1939
char album[30]
Definition: mpg123.h:1936
mpg123_string * genre
Definition: mpg123.h:1912
size_t texts
Definition: mpg123.h:1919
mpg123_string * title
Definition: mpg123.h:1908
mpg123_text * text
Definition: mpg123.h:1918
mpg123_string * comment
Definition: mpg123.h:1913
mpg123_string * artist
Definition: mpg123.h:1909
mpg123_string * year
Definition: mpg123.h:1911
size_t comments
Definition: mpg123.h:1917
mpg123_text * comment_list
Definition: mpg123.h:1916
mpg123_string * album
Definition: mpg123.h:1910
mpg123_text * extra
Definition: mpg123.h:1920
size_t extras
Definition: mpg123.h:1921
char * p
Definition: mpg123.h:1634
size_t fill
Definition: mpg123.h:1636
char lang[3]
Definition: mpg123.h:1855
mpg123_string text
Definition: mpg123.h:1858
char id[4]
Definition: mpg123.h:1856
mpg123_string description
Definition: mpg123.h:1857
Hopefully valid HTML! Valid CSS!