[mythtv] [PATCH] Import Recordings

John Hurliman jhurliman at focustheater.com
Sun Aug 3 14:50:42 EDT 2003


Here's a patch to add new .nuv files to the database if there's no 
existing entry for them. This will only work for software encoded files, 
hardware encoded files aren't supported since the seektable is stored in 
the database. New files are added when you go to the Watch Recordings 
screen. If the original filename is preserved an entry is added in 
similar style to manual recording entries, otherwise it attempts to 
parse a useful entry from the filename (eg. The Simpsons - Episode 
107.nuv will be split at the dash). The file is renamed to match it's 
corresponding db entry.

Known issues: This opens up a whole new can of worms. MythTV wasn't 
designed to interact with things outside it's own environment. If you 
drop a bad .nuv file in there, Watch Recordings will get stuck in an 
ugly loop (although it keeps running and you can get out). Who knows 
what the decoder would do if you import a correct .nuv with really nasty 
errors in it. If you are moving things in and out of the recordings 
directory by the filesystem instead of the myth interface you'll end up 
with a lot of entries pointing to non-existing files, as it appears myth 
doesn't delete bad entries from the database. Apply at your own risk.

jph
-------------- next part --------------
Index: playbackbox.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythfrontend/playbackbox.cpp,v
retrieving revision 1.89
diff -u -r1.89 playbackbox.cpp
--- playbackbox.cpp	29 Jul 2003 05:39:24 -0000	1.89
+++ playbackbox.cpp	3 Aug 2003 08:14:36 -0000
@@ -99,6 +99,8 @@
         exit(0);
     }
 
+    ScanRecordings();
+    
     FillList(); 
 
     curTitle = 0;
@@ -999,6 +1001,182 @@
         }
         else
             curShowing = 0;
+    }
+}
+
+void PlaybackBox::ScanRecordings()
+{
+    QSqlQuery query;
+    QString recordingDir = gContext->GetSetting("RecordFilePrefix");
+    QDir dir = QDir(recordingDir);
+    QString hostname;
+        
+    query.exec("SELECT starttime,hostname FROM recorded;");
+
+    QStringList files = dir.entryList("*.nuv");
+    int querySize = query.size();
+    int fileCount = files.count();
+        
+    if (querySize > -1 && querySize < fileCount)
+    {
+        QString starttime;
+        QStringList::iterator it;
+        
+        if (querySize > 0)
+        {
+            while (query.next())
+            {
+                // Check for a matching file
+                for (it = files.begin(); it != files.end(); ++it)
+                {
+                    // hostname grabbed now for convenience
+                    hostname = query.value(1).toString();
+                    starttime = (*it).section("_", 1, 1);
+
+                    if (query.value(0).toString() == starttime)
+                    {
+                        files.remove(it);
+                        --it;
+                    }
+                }
+            }
+        }
+
+        if (files.count() > 0)
+        {
+            QDateTime dt = QDateTime::currentDateTime();
+            QString currentStamp = dt.toString("yyyyMMddhhmm00");
+            QString chanid;
+            QString defaultChanid;
+            QString startHour;
+            QString starttime;
+            QString endHour;
+            QString endtime;
+            QString title;
+            QString subtitle;
+            QString description;
+            QString insertQuery;
+            QString oldFile;
+            QString newFile;
+            QString strippedName;
+            QFile file;
+            QDir nuvFile;
+
+            // Grab a default chanid just in case
+            query.exec("SELECT chanid FROM channel;");
+            if (query.size() > 0)
+            {
+                query.next();
+                defaultChanid = query.value(0).toString();
+            }
+            else
+            {
+                cerr << "playbackbox.o: No channels defined in database" << endl;
+                return;
+            }
+
+            for (it = files.begin(); it != files.end(); ++it)
+            {
+                strippedName = (*it).left((*it).length() - 4).stripWhiteSpace();
+
+                // Scrape any useful information out of the filename
+                if ((*it).contains("_") == 2)
+                {
+                    // Parse the chanid and check if it exists
+                    chanid = strippedName.section("_", 0, 0);
+                    query.exec(QString("SELECT name FROM channel WHERE chanid=\"%1\"").arg(chanid));
+                    if (query.size() < 1)
+                        chanid = defaultChanid;
+                    
+                    // Grab the timestamps if they're valid
+                    dt = QDateTime::fromString(strippedName.section("_", 1, 1), Qt::ISODate);
+                    if (!dt.isNull())
+                    {
+                        starttime = dt.toString("yyyyMMddhhmm00");
+                        startHour = dt.toString("hh.mm");
+                    }
+                    else
+                    {
+                        starttime = currentStamp;
+                        dt = QDateTime::currentDateTime();
+                        startHour = dt.toString("hh.mm");
+                    }
+                        
+                    dt = QDateTime::fromString(strippedName.section("_", 2, 2), Qt::ISODate);
+                    if (!dt.isNull())
+                    {
+                        endtime = dt.toString("yyyyMMddhhmm00");
+                        endHour = dt.toString("hh.mm");
+                    }
+                    else
+                    {
+                        endtime = currentStamp;
+                        dt = QDateTime::currentDateTime();
+                        endHour = dt.toString("hh.mm");
+                    }
+
+                    title = QString("%1 %2-%3").arg(dt.toString("yyyyMMdd")).arg(startHour).arg(endHour);
+                    // Subtitle could be expanded to match manual recording style
+                    subtitle = "Imported recording";
+                }
+                else
+                {
+                    chanid = defaultChanid;
+
+                    starttime = currentStamp;
+                    // If we really had cpu cycles to spare we could open the file and
+                    // extract the length and convert to starttime + length
+                    endtime = currentStamp;
+                    
+                    // Non-standard filename, try to parse it
+                    if (strippedName.contains("-"))
+                    {
+                        title = strippedName.section("-", 0, 0);
+                        subtitle = strippedName.section("-", 1, 1);
+                        subtitle.stripWhiteSpace();
+                    }
+                    else
+                    {
+                        title = strippedName;
+                        subtitle = strippedName;
+                    }
+                }
+
+                description = QString("Imported recording %1").arg(strippedName);
+
+                if (recordingDir.right(1) != "/")
+                    recordingDir += "/";
+                
+                oldFile = QString("%1%2").arg(recordingDir).arg(*it);
+                newFile = QString("%1_%2_%3.nuv").arg(chanid).arg(starttime).arg(endtime);
+                newFile = QString("%1%2").arg(recordingDir).arg(newFile);
+                
+                // Rename to fit mythtv naming scheme
+                if (!nuvFile.rename(oldFile, newFile))
+                {
+                    cerr << "Could not rename " << oldFile << " to " << newFile << endl;
+                    break;
+                }
+                
+                // Add an entry to the database
+                insertQuery = QString("INSERT INTO recorded (chanid,starttime,endtime,title,"
+                                      "subtitle,description,category,hostname,bookmark,editing,"
+                                      "cutlist,autoexpire) VALUES(%1,\"%2\",\"%3\",\"%4\","
+                                      "\"%5\",\"%6\",\"%7\",\"%8\",NULL,0,NULL,1);")
+                                      .arg(chanid)
+                                      .arg(starttime)
+                                      .arg(endtime)
+                                      .arg(title)
+                                      .arg(subtitle)
+                                      .arg(description)
+                                      .arg("Imported")
+                                      .arg(hostname);
+
+                query.exec(insertQuery);
+                if (!query.isActive())
+                    MythContext::DBError("Import new recording", insertQuery);
+            }
+        }
     }
 }
 
Index: playbackbox.h
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythfrontend/playbackbox.h,v
retrieving revision 1.30
diff -u -r1.30 playbackbox.h
--- playbackbox.h	29 Jul 2003 05:39:24 -0000	1.30
+++ playbackbox.h	3 Aug 2003 08:14:36 -0000
@@ -3,6 +3,7 @@
 
 #include <qdatetime.h>
 #include <qdom.h>
+#include <qdir.h>
 #include "mythwidgets.h"
 #include "mythdialogs.h"
 #include "uitypes.h"
@@ -61,6 +62,7 @@
     void keyPressEvent(QKeyEvent *e);
 
   private:
+    void ScanRecordings(void);
     void FillList(void);
     void UpdateProgressBar(void);
 


More information about the mythtv-dev mailing list