]> diplodocus.org Git - flac-archive/blob - apic-read.cc
194e3406eb219556d8107e962214dd0646d8b7ec
[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 ssize_t size = data.size();
44 ssize_t status = write(fd, data.data(), size);
45 if (status < 0) {
46 perror("write");
47 return 2;
48 }
49 if (status != size) {
50 fprintf(stderr, "wrote only %d bytes out of %d\n", status, size);
51 return 3;
52 }
53 if (close(fd) < 0) {
54 perror("close");
55 }
56 }
57
58 return 0;
59 }