Package: id3v2
Version: 0.1.12-2
Severity: wishlist
Tags: patch

Small patch enabling album cover tagging, adding a -p/--picture switch.
Tested on 0.1.12-2 with debian patches applied.
Mimetype is determined via libmagic (in my case version 5.11-2+deb7u8).
Written to be compiled with 'make WITH_MAGIC=true', since I didn't knew 
if another dependency is in maintainers mind. Also I can't say much 
about portability, I only know there is a windows version of libmagic 
included with GNU File for Windows:
http://gnuwin32.sourceforge.net/packages/file.htm

Thanks
diff --git a/Makefile b/Makefile
index 545ec02..b7923ff 100644
--- a/Makefile
+++ b/Makefile
@@ -6,6 +6,12 @@ PREFIX=	/opt/local
 CXXFLAGS+=	-Wall -I${PREFIX}/include/ -DVERSION="\"${VERSION}\"" #-DSORT_RUNTIME
 LDFLAGS+=	-L${PREFIX}/lib/ 
 
+WITH_MAGIC?=false
+ifeq (${WITH_MAGIC},true)
+	LDFLAGS+= -lmagic
+	CXXFLAGS+=	-DWITH_MAGIC
+endif
+
 id3v2:	convert.o list.o id3v2.o genre.o charset.o
 	${CXX} ${LDFLAGS} -pedantic -Wall -g -o $@ $^ -lz -lid3
 
diff --git a/id3v2.cpp b/id3v2.cpp
index 2908337..7c529fb 100644
--- a/id3v2.cpp
+++ b/id3v2.cpp
@@ -16,6 +16,10 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#ifdef WITH_MAGIC
+#include <magic.h>
+#endif
+
 #include "genre.h"
 
 #define MAXNOFRAMES 1000
@@ -46,6 +50,9 @@ void PrintUsage(char *sName)
   std::cout << "  -a,  --artist       \"ARTIST\"    Set the artist information" << std::endl;
   std::cout << "  -A,  --album        \"ALBUM\"     Set the album title information" << std::endl;
   std::cout << "  -t,  --song         \"SONG\"      Set the song title information" << std::endl;
+#ifdef WITH_MAGIC
+  std::cout << "  -p,  --picture   \"IMAGE FILE\"    Set the front cover image" << std::endl;
+#endif
   std::cout << "  -c,  --comment      \"DESCRIPTION\":\"COMMENT\":\"LANGUAGE\"  "<< std::endl
        << "                            Set the comment information (both" << std::endl
        << "                            description and language optional)" << std::endl;    
@@ -209,7 +216,7 @@ int main( int argc, char *argv[])
       { "WXXX",    required_argument, &optFrameID, ID3FID_WWWUSER },
       { 0, 0, 0, 0 }
     };
-    iOpt = getopt_long (argc, argv, "12hfLvlRdsDCr:a:A:t:c:g:y:T:",
+    iOpt = getopt_long (argc, argv, "12hfLvlRdsDCr:a:A:t:p:c:g:y:T:",
                         long_options, &option_index);
 
     if (iOpt == -1  && argCounter == 0)
@@ -275,6 +282,13 @@ int main( int argc, char *argv[])
                 frameList[frameCounter].data = optarg;
                 frameCounter++;
                 break;
+#ifdef WITH_MAGIC
+      case 'p':
+                frameList[frameCounter].id   = ID3FID_PICTURE;
+                frameList[frameCounter].data = optarg;
+                frameCounter++;
+                break;
+#endif
       case 'c': 
                 frameList[frameCounter].id   = ID3FID_COMMENT;
                 frameList[frameCounter].data = optarg;
@@ -635,6 +649,44 @@ int main( int argc, char *argv[])
         }
         case ID3FID_PICTURE:
         {
+#ifdef WITH_MAGIC
+          if (pFrame != NULL)
+          {
+            ID3_Frame * todel = myTag.RemoveFrame(pFrame);
+            delete todel;
+          }
+          if (strlen(frameList[ii].data) > 0)
+          {
+            magic_t m = magic_open(MAGIC_SYMLINK | MAGIC_MIME_TYPE);
+            if (m == NULL)
+            {
+              std::cerr << "Couldn't create magic cookie." << std::endl;
+              break;
+            }
+            int res = magic_load(m, NULL);
+            if (res != 0)
+            {
+              std::cerr << "magic_load: Couldn't load magic database." << std::endl;
+              break;
+            }
+            const char *mime = magic_file(m, frameList[ii].data);
+            if (mime == NULL)
+            {
+              std::cerr << "Couldn't determine mime type or file not found." << std::endl;
+              break;
+            }
+			if (strstr(mime, "image/") == NULL) {
+              std::cerr << "File " << frameList[ii].data << " is no image." << std::endl;
+              break;
+			}
+            myFrame->Field(ID3FN_TEXTENC) = ID3TE_ASCII;
+            myFrame->Field(ID3FN_MIMETYPE) = mime;
+            myFrame->Field(ID3FN_PICTURETYPE) = ID3PT_COVERFRONT;
+            myFrame->Field(ID3FN_DATA).FromFile(frameList[ii].data);
+            myTag.AttachFrame(myFrame);
+            magic_close(m);
+          }
+#else
           char
             *sMimeType = ID3_GetString(myFrame, ID3FN_MIMETYPE),
             *sDesc     = ID3_GetString(myFrame, ID3FN_DESCRIPTION),
@@ -648,6 +700,7 @@ int main( int argc, char *argv[])
           delete [] sMimeType;
           delete [] sDesc;
           delete [] sFormat;
+#endif
           break;
         }
         case ID3FID_GENERALOBJECT:

Reply via email to