[mythtv] [PATCH] MythMusic - rescan metadata

BP lists at qucae.com
Mon May 31 09:46:29 EDT 2004


This is essentially the patch submitted by BHills in February ( 
http://www.gossamer-threads.com/lists/mythtv/dev/18882?search_string=mythmusic%20id3;#18882 
) which didn't seem to get commited.  I've made a couple small changes, 
removed a couple parts and applied it to current CVS.  I find it 
extremely useful since I had a few thousand files with inconsistent ID3 
tags.  Just thought I'd send it to the list in case someone else needs 
this functionality.

This patch adds an option through the Settings->MythMusic->General 
dialog (replaces "Ignore ID3 Tag" checkbox with a drop list) to rescan 
all files and rewrite the metadata to the existing database entries. 
This should preserve existing play lists and rankings.  The current 
settings for ignoring ID3 Tags or not should be honored by this patch.

Known problems:

1)  The Updating data progress bar starts at about 98% and quickly hits 100%
2)  The check for title.isEmpty() in Vorbisdecoder.cpp always seemed to 
indicate Empty, so this patch has it removed

I have tested this against both Ogg and MP3 files.  I have not tested 
with FLAC.


-------------- next part --------------
Index: flacdecoder.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythmusic/mythmusic/flacdecoder.cpp,v
retrieving revision 1.16
diff -u -d -r1.16 flacdecoder.cpp
--- flacdecoder.cpp	25 May 2004 04:22:56 -0000	1.16
+++ flacdecoder.cpp	31 May 2004 13:10:26 -0000
@@ -411,12 +411,15 @@
 
 Metadata *FlacDecoder::getMetadata(QSqlDatabase *db)
 {
-    Metadata *testdb = new Metadata(filename);
-    if (testdb->isInDatabase(db, musiclocation))
-        return testdb;
-
-    delete testdb;
+    if (ignore_id3 != 2)   // Skip check if we want to refresh data on existing files
+    {
+        Metadata *testdb = new Metadata(filename);
+        if (testdb->isInDatabase(db, musiclocation))
+            return testdb;
 
+        delete testdb;
+    }
+    
     QString artist = "", album = "", title = "", genre = "";
     int year = 0, tracknum = 0, length = 0;
 
@@ -465,7 +468,7 @@
     FLAC__ASSERT(0 != block);
     FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
 
-    if (ignore_id3 || title.isEmpty())
+    if ((ignore_id3 == 1) || title.isEmpty())
     {
         getMetadataFromFilename(filename, QString(".flac$"), artist, album, 
                                 title, genre, tracknum);
Index: globalsettings.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythmusic/mythmusic/globalsettings.cpp,v
retrieving revision 1.19
diff -u -d -r1.19 globalsettings.cpp
--- globalsettings.cpp	22 May 2004 05:45:16 -0000	1.19
+++ globalsettings.cpp	31 May 2004 13:10:26 -0000
@@ -121,17 +121,19 @@
 };
 
 
-class IgnoreID3Tags: public CheckBoxSetting, public GlobalSetting {
+class IgnoreID3Tags: public ComboBoxSetting, public GlobalSetting {
 public:
     IgnoreID3Tags():
         GlobalSetting("Ignore_ID3") {
-        setLabel(QObject::tr("Ignore ID3 Tags"));
-        setValue(false);
-        setHelpText(QObject::tr("If set, MythMusic will skip checking ID3 tags "
-                    "in files and just try to determine Genre, Artist, "
-                    "Album, and Track number and title from the "
-                    "filename."));
-    };
+         setLabel(QObject::tr("Scan for Music Options"));
+         addSelection(QObject::tr("Add new files:  Use ID3 Tag"), "0");
+         addSelection(QObject::tr("Add new files:  Ignore ID3 Tag"), "1");
+         addSelection(QObject::tr("Refresh data for all files:  Use ID3 Tags"),"2");
+         setHelpText(QObject::tr("When scanning for music:  1)  Add new files with ID3 data. "
+                     "2)  Do not check ID3 tags.  Determine Artist, title, etc from filename. "
+                     "3)  Rescan all files, refreshing ID3 data in database"));
+
+   };
 };
 
 class AutoLookupCD: public CheckBoxSetting, public GlobalSetting {
Index: maddecoder.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythmusic/mythmusic/maddecoder.cpp,v
retrieving revision 1.21
diff -u -d -r1.21 maddecoder.cpp
--- maddecoder.cpp	29 May 2004 05:44:39 -0000	1.21
+++ maddecoder.cpp	31 May 2004 13:10:26 -0000
@@ -537,25 +537,26 @@
 
 Metadata *MadDecoder::getMetadata(QSqlDatabase *db)
 {
-    Metadata *testdb = new Metadata(filename);
-    if (testdb->isInDatabase(db, musiclocation))
+    if (ignore_id3 != 2)   // Skip check if we want to refresh data on existing files
     {
-        return testdb;
-    }
-
-    delete testdb;
+        Metadata *testdb = new Metadata(filename);
+        if (testdb->isInDatabase(db, musiclocation))
+            return testdb;
 
+        delete testdb;
+    }
+    
     QString artist = "", album = "", title = "", genre = "";
     int year = 0, tracknum = 0, length = 0;
     id3_file *id3file = NULL;
 
-    if (!ignore_id3)
+    if (! (ignore_id3 == 1))
     {
         id3file = id3_file_open(filename.local8Bit(), ID3_FILE_MODE_READONLY);
         if (!id3file)
             id3file = id3_file_open(filename.ascii(), ID3_FILE_MODE_READONLY);
     }
-
+    
     if (id3file)
     {
         id3_tag *tag = id3_file_tag(id3file);
Index: main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythmusic/mythmusic/main.cpp,v
retrieving revision 1.49
diff -u -d -r1.49 main.cpp
--- main.cpp	21 Mar 2004 06:22:42 -0000	1.49
+++ main.cpp	31 May 2004 13:10:26 -0000
@@ -176,6 +176,7 @@
 {
     MusicLoadedMap music_files;
     MusicLoadedMap::Iterator iter;
+    int ignore_id3 = gContext->GetNumSetting("Ignore_ID3",0);
 
     BuildFileList(directory, music_files);
 
@@ -197,9 +198,30 @@
             if (name != QString::null)
             {
                 if ((iter = music_files.find(name)) != music_files.end())
-                    music_files.remove(iter);
+                {
+                    switch(ignore_id3)
+                    {
+                        case 0:  // Add new files using ID3 Tags
+                        {
+                            music_files.remove(iter);
+                            break;
+                        }
+                        case 2:  // Udate Metadata from all files
+                        {
+                            break;
+                        }
+                        case 1:  //  Old "Ignore ID3 Tag" setting
+                        {
+                            music_files.remove(iter);
+                            break;
+                        }
+
+                    }
+                }
                 else
+                {
                     music_files[name] = kDatabase;
+                }
             }
             file_checking->setProgress(++counter);
         }
Index: metadata.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythmusic/mythmusic/metadata.cpp,v
retrieving revision 1.25
diff -u -d -r1.25 metadata.cpp
--- metadata.cpp	10 Apr 2004 18:29:35 -0000	1.25
+++ metadata.cpp	31 May 2004 13:10:27 -0000
@@ -149,6 +149,44 @@
     sqlfilename.replace(QRegExp("\\\\"), QString("\\\\"));
     sqlfilename.replace(QRegExp("\""), QString("\\\""));
 
+    QFileInfo *fi= new QFileInfo(startdir + filename);
+    if(!fi)
+        return;
+    QString checkforupdate = QString("SELECT intid FROM musicmetadata WHERE "
+                                 "( filename = \"%1\" );")
+                                .arg(sqlfilename);
+    QSqlQuery updquery = db->exec(checkforupdate);
+    if (updquery.isActive() && updquery.numRowsAffected() > 0)
+    {
+        if(updquery.next())
+        {
+            QString updatemetadata = QString("UPDATE musicmetadata set artist=\"%1\","
+                                             " album=\"%2\","
+                                             " title=\"%3\","
+                                             " genre=\"%4\","
+                                             " year=%5,"
+                                             " tracknum=%6,"
+                                             " length=%7,"
+                                             " filename=\"%8\" "
+                                             )
+                                             .arg(artist.latin1())
+                                             .arg(album.latin1())
+                                             .arg(title.latin1())
+                                             .arg(genre)
+                                             .arg(year)
+                                             .arg(tracknum)
+                                             .arg(length)
+                                             .arg(sqlfilename);
+            updatemetadata = updatemetadata.append( QString ("WHERE intid=%1;")
+                                           .arg(updquery.value(0).toUInt()));
+            
+            db->exec(updatemetadata);
+        }
+        delete fi;
+        return;
+    }
+    
+    
     // Don't update the database if a song with the exact same
     // metadata is already there
     QString checkquery = QString("SELECT filename FROM musicmetadata WHERE "
@@ -156,7 +194,8 @@
                                  "( album = \"%2\" ) AND ( title = \"%3\" ) "
                                  "AND ( genre = \"%4\" ) AND "
                                  "( year = \"%5\" ) AND ( tracknum = \"%6\" ) "
-                                 "AND ( length = \"%7\" ) );")
+                                 "AND ( length = \"%7\" )"
+                                 "AND ( filename = \"%8\");")
                                  .arg(artist.latin1())
                                  .arg(album.latin1())
                                  .arg(title.latin1())
@@ -183,6 +222,7 @@
 
     // easiest way to ensure we've got 'id' filled.
     fillData(db);
+    delete fi;
 }
 
 void Metadata::setField(const QString &field, const QString &data)
Index: vorbisdecoder.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythmusic/mythmusic/vorbisdecoder.cpp,v
retrieving revision 1.11
diff -u -d -r1.11 vorbisdecoder.cpp
--- vorbisdecoder.cpp	25 May 2004 04:22:56 -0000	1.11
+++ vorbisdecoder.cpp	31 May 2004 13:10:28 -0000
@@ -319,12 +319,15 @@
 
 Metadata *VorbisDecoder::getMetadata(QSqlDatabase *db)
 {
-    Metadata *testdb = new Metadata(filename);
-    if (testdb->isInDatabase(db, musiclocation))
-        return testdb;
-
-    delete testdb;
+    if (ignore_id3 != 2)   // Skip check if we want to refresh data on existing files
+    {
+        Metadata *testdb = new Metadata(filename);
+        if (testdb->isInDatabase(db, musiclocation))
+            return testdb;
 
+        delete testdb;
+    }
+ 
     QString artist = "", album = "", title = "", genre = "";
     int year = 0, tracknum = 0, length = 0;
 
@@ -345,7 +348,7 @@
     comment = ov_comment(&vf, -1);
     length = (int)ov_time_total(&vf, -1) * 1000;
 
-    if (ignore_id3 || title.isEmpty())
+    if (ignore_id3 ==1)
     {
         getMetadataFromFilename(filename, QString(".ogg$"), artist, album, 
                                 title, genre, tracknum);


More information about the mythtv-dev mailing list