]> diplodocus.org Git - flac-archive/blob - apic-read.cc
324450e411147437d656fac1a28c2534d570307e
[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 using TagLib::ID3v2::Tag;
16 using TagLib::ID3v2::FrameList;
17 using TagLib::ID3v2::AttachedPictureFrame;
18
19 int
20 main(int argc, char **argv)
21 {
22 const char *mp3_path = argv[1];
23 TagLib::MPEG::File f(mp3_path);
24 Tag *id3v2tag = f.ID3v2Tag();
25 if (!id3v2tag) {
26 return 0;
27 }
28 const FrameList &frames = id3v2tag->frameList();
29 FrameList::ConstIterator it;
30 for (it = frames.begin(); it != frames.end(); it++) {
31 TagLib::ByteVector frameid = (*it)->frameID();
32 uint frameid_size = frameid.size();
33 char *frameid_s = new char[frameid_size + 1];
34 memcpy(frameid_s, frameid.data(), frameid_size);
35 frameid_s[frameid_size] = '\0';
36 printf("%s: %s\n", frameid_s, (*it)->toString().toCString());
37 if (frameid != "APIC") {
38 continue;
39 }
40 AttachedPictureFrame *frame = (AttachedPictureFrame *)(*it);
41 TagLib::ByteVector data = frame->picture();
42 int fd = open(argv[2], O_CREAT | O_WRONLY);
43 if (fd < 0) {
44 perror("open");
45 return 2;
46 }
47 ssize_t size = data.size();
48 ssize_t status = write(fd, data.data(), size);
49 if (status < 0) {
50 perror("write");
51 return 3;
52 }
53 if (status != size) {
54 fprintf(stderr, "wrote only %d bytes out of %d\n", status, size);
55 return 4;
56 }
57 if (close(fd) < 0) {
58 perror("close");
59 return 5;
60 }
61 }
62
63 return 0;
64 }