]> diplodocus.org Git - flac-archive/blob - apic-read.cc
fd83ac8a1e3ffb87fb91b95a9658ce20b891226d
[flac-archive] / apic-read.cc
1 #include <sys/stat.h>
2 #include <sys/types.h>
3
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 #include <attachedpictureframe.h>
10 #include <id3v2tag.h>
11 #include <mpegfile.h>
12
13 // some demo apic reading code; would be nice to save all APIC frames.
14
15 int
16 main(int argc, char **argv)
17 {
18 const char *mp3_path = argv[1];
19 TagLib::MPEG::File f(mp3_path);
20 TagLib::ID3v2::Tag *id3v2tag = f.ID3v2Tag();
21 if (!id3v2tag) {
22 return 0;
23 }
24 TagLib::ID3v2::FrameList::ConstIterator it;
25 for (it = id3v2tag->frameList().begin();
26 it != id3v2tag->frameList().end();
27 it++) {
28 TagLib::ByteVector frameid = (*it)->frameID();
29 uint frameid_size = frameid.size();
30 char *frameid_s = new char[frameid_size + 1];
31 memcpy(frameid_s, frameid.data(), frameid_size);
32 frameid_s[frameid_size] = '\0';
33 printf("%s: %s\n", frameid_s, (*it)->toString().toCString());
34 if (frameid != "APIC") {
35 continue;
36 }
37 TagLib::ID3v2::AttachedPictureFrame *frame =
38 (TagLib::ID3v2::AttachedPictureFrame *)(*it);
39 TagLib::ByteVector data = frame->picture();
40 int fd = open(argv[2], O_CREAT | O_WRONLY);
41 ssize_t size = data.size();
42 ssize_t status = write(fd, data.data(), size);
43 if (status < 0) {
44 perror("write");
45 return 2;
46 }
47 if (status != size) {
48 fprintf(stderr, "wrote only %d bytes out of %d\n", status, size);
49 return 3;
50 }
51 if (close(fd) < 0) {
52 perror("close");
53 }
54 }
55
56 return 0;
57 }