1 #include <FLAC/metadata.h>
5 static PyObject
*flac_error
;
8 flac_pictures(char *fn
)
10 FLAC__Metadata_Chain
*chain
;
11 FLAC__Metadata_Iterator
*iterator
;
12 FLAC__StreamMetadata
*block
;
13 PyObject
*description
, *tuple
, *list
;
15 chain
= FLAC__metadata_chain_new();
17 return PyErr_NoMemory();
20 if(!FLAC__metadata_chain_read(chain
, fn
)) {
21 PyObject
*s
= PyString_FromFormat("metadata_chain_read (%d)",
22 FLAC__metadata_chain_status(chain
));
24 PyErr_SetObject(flac_error
, s
);
29 iterator
= FLAC__metadata_iterator_new();
30 if (iterator
== NULL
) {
31 return PyErr_NoMemory();
34 FLAC__metadata_iterator_init(iterator
, chain
);
42 /* This can fail, but the documentation doesn't say how, why,
43 * or how to react. metaflac/operations.c just prints a
44 * useless error message and keeps going. */
45 block
= FLAC__metadata_iterator_get_block(iterator
);
47 PyErr_SetString(flac_error
,
48 "metadata_iterator_get_block returned NULL");
52 if (block
->type
!= FLAC__METADATA_TYPE_PICTURE
) {
56 description
= PyUnicode_DecodeUTF8(block
->data
.picture
.description
,
57 strlen(block
->data
.picture
.description
),
59 if (description
== NULL
) {
63 tuple
= Py_BuildValue("(isOiiiis#)",
64 block
->data
.picture
.type
,
65 block
->data
.picture
.mime_type
,
67 block
->data
.picture
.width
,
68 block
->data
.picture
.height
,
69 block
->data
.picture
.depth
,
70 block
->data
.picture
.colors
,
71 block
->data
.picture
.data
,
72 block
->data
.picture
.data_length
);
77 if (PyList_Append(list
, tuple
) == -1) {
80 } while(FLAC__metadata_iterator_next(iterator
));
82 FLAC__metadata_iterator_delete(iterator
);
83 FLAC__metadata_chain_delete(chain
);
89 get_pictures(PyObject
*self
, PyObject
*args
)
93 if (!PyArg_ParseTuple(args
, "s", &fn
)) {
97 return flac_pictures(fn
);
101 PyMethodDef methods
[] = {
102 {"get_pictures", get_pictures
, METH_VARARGS
,
105 {NULL
, NULL
, 0, NULL
}
109 export_constants(PyObject
*m
)
114 n = PyInt_FromLong(FLAC__STREAM_METADATA_ ## x); \
115 if (n == NULL || PyModule_AddObject(m, #x, n) == -1) return;
117 foo(PICTURE_TYPE_OTHER
);
118 foo(PICTURE_TYPE_FILE_ICON_STANDARD
);
119 foo(PICTURE_TYPE_FILE_ICON
);
120 foo(PICTURE_TYPE_FRONT_COVER
);
121 foo(PICTURE_TYPE_BACK_COVER
);
122 foo(PICTURE_TYPE_LEAFLET_PAGE
);
123 foo(PICTURE_TYPE_MEDIA
);
124 foo(PICTURE_TYPE_LEAD_ARTIST
);
125 foo(PICTURE_TYPE_ARTIST
);
126 foo(PICTURE_TYPE_CONDUCTOR
);
127 foo(PICTURE_TYPE_BAND
);
128 foo(PICTURE_TYPE_COMPOSER
);
129 foo(PICTURE_TYPE_LYRICIST
);
130 foo(PICTURE_TYPE_RECORDING_LOCATION
);
131 foo(PICTURE_TYPE_DURING_RECORDING
);
132 foo(PICTURE_TYPE_DURING_PERFORMANCE
);
133 foo(PICTURE_TYPE_VIDEO_SCREEN_CAPTURE
);
134 foo(PICTURE_TYPE_FISH
);
135 foo(PICTURE_TYPE_ILLUSTRATION
);
136 foo(PICTURE_TYPE_BAND_LOGOTYPE
);
137 foo(PICTURE_TYPE_PUBLISHER_LOGOTYPE
);
138 foo(PICTURE_TYPE_UNDEFINED
);
146 PyObject
*m
= Py_InitModule("flac", methods
);
151 flac_error
= PyErr_NewException("flac_archive.flac.error", NULL
, NULL
);
152 if (flac_error
== NULL
) {
155 PyModule_AddObject(m
, "error", flac_error
);