]>
diplodocus.org Git - flac-archive/blob - taglib.cc
1 #include <attachedpictureframe.h>
11 static PyObject
*taglib_error
;
14 taglib_apic(const char *fn
, PyObject
*list
)
16 TagLib::MPEG::File
*f
;
17 TagLib::ID3v2::Tag
*t
;
18 PyObject
*tuple
, *description
, *description_utf8
;
19 int i
, type
, width
, height
, depth
, colors
, len
;
20 const char *mime_type
, *description_utf8_s
, *image
;
21 TagLib::ByteVector
*bv
;
22 TagLib::ID3v2::AttachedPictureFrame
*p
;
24 /* GAH! Opening the file is entangled with allocating the object;
25 * if this fails, am i out of memory, or is there a problem with
26 * the file? Hate C++... */
27 f
= new TagLib::MPEG::File (fn
);
29 return PyErr_NoMemory();
32 t
= f
->ID3v2Tag(true);
34 PyErr_SetString(taglib_error
, "taglib failed to return ID3v2");
38 for (i
= 0; i
< PyList_Size(list
); i
++) {
39 tuple
= PyList_GetItem(list
, i
);
40 if (!PyArg_ParseTuple(tuple
, "isUiiiis#",
41 &type
, &mime_type
, &description
, &width
, &height
, &depth
, &colors
, &image
, &len
)) {
45 description_utf8
= PyUnicode_AsEncodedString(description
, "utf8",
47 if (description_utf8
== 0) {
51 description_utf8_s
= PyString_AsString(description_utf8
);
52 if (description_utf8_s
== 0) {
56 bv
= new TagLib::ByteVector (image
, len
);
58 return PyErr_NoMemory();
61 p
= new TagLib::ID3v2::AttachedPictureFrame
;
63 return PyErr_NoMemory();
66 /* Um, look at all these void-returning functions. I'm sure
67 * they can fail, i just have no way to detect it... */
68 p
->setType((TagLib::ID3v2::AttachedPictureFrame::Type
)type
);
69 p
->setMimeType(mime_type
);
70 p
->setDescription(description_utf8_s
);
71 p
->setTextEncoding(TagLib::String::UTF8
);
72 // taglib doesn't support width, height, depth, colors.
75 // another void-returning function that i bet fails
82 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, (char *)fn
);
84 PyErr_SetString(taglib_error
, "taglib failed to save");
94 add_apic_frame_to_mp3(PyObject
*self
, PyObject
*args
)
99 if (!PyArg_ParseTuple(args
, "sO", &fn
, &list
)) {
103 return taglib_apic(fn
, list
);
107 PyMethodDef methods
[] = {
108 {"add_apic_frame_to_mp3", add_apic_frame_to_mp3
, METH_VARARGS
,
116 PyObject
*m
= Py_InitModule("taglib", methods
);
121 taglib_error
= PyErr_NewException("flac_archive.taglib.error", 0, 0);
122 if (taglib_error
== 0) {
125 PyModule_AddObject(m
, "error", taglib_error
);