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