autopublishing script changed in order to complete the new video format (res, dar...
[living-lab-site.git] / scripts / auto-publishing / publish_videos.py
index 2b63c52..e70f256 100755 (executable)
@@ -3,7 +3,7 @@
 # Copyright Calin-Andrei Burloiu, calin.burloiu@gmail.com
 #
 # Automatically publishes videos in P2P-Tube DB based on the video files and
-# a videos info file. Parameters: videos_info_file videos_directory category
+# a videos info file. Parameters: videos_info_file videos_directory category_id
 #
 import sys
 import MySQLdb
@@ -21,9 +21,8 @@ class VideosTable:
     default_thumb = 0
     
     directory = os.curdir
-    default_video_ext = 'ogv'
 
-    def __init__(self, dbCur, directory, name, title, description, tags, category):
+    def __init__(self, dbCur, directory, name, title, description, tags, category_id):
         self.dbCur = dbCur
         self.directory = directory
 
@@ -32,26 +31,29 @@ class VideosTable:
         self.description = description
         self.duration, self.formats = self.findVideosMeta()
         self.formats_json = json.dumps(self.formats, separators=(',', ':'))
-        self.category = category
+        self.category_id = category_id
         
         tagList = tags.split(',')
         self.tags = {}
         for tag in tagList:
-            self.tags[tag.strip()] = 0
+            if tag != '':
+                self.tags[tag.strip()] = 0
         self.tags_json = json.dumps(self.tags, separators=(',', ':'))
         
-    def getVideoDefinition(self, fileName):
+    def getVideoResolution(self, fileName):
+        pipe = subprocess.Popen('mediainfo --Inform="Video;%Width%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
+        width = pipe.readline().strip()
+
         pipe = subprocess.Popen('mediainfo --Inform="Video;%Height%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
         height = pipe.readline().strip()
 
-        pipe = subprocess.Popen('mediainfo --Inform="Video;%ScanType%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
-        scanType = pipe.readline().strip()
-        if scanType == '' or scanType == 'Progressive':
-            scanType = 'p'
-        elif scanType == 'Interlaced':
-            scanType = 'i';
+        return width + 'x' + height
+
+    def getVideoDar(self, fileName):
+        pipe = subprocess.Popen('mediainfo --Inform="Video;%DisplayAspectRatio/String%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
+        dar = pipe.readline().strip()
 
-        return height + scanType
+        return dar
 
     def getVideoDuration(self, fileName):
         pipe = subprocess.Popen('mediainfo --Inform="General;%Duration/String3%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
@@ -68,7 +70,7 @@ class VideosTable:
     # Returns a pair with duration and formats list.
     def findVideosMeta(self):
         files = [f for f in os.listdir(self.directory) if os.path.isfile(os.path.join(self.directory, f))]
-        files = fnmatch.filter(files, self.name + "*")
+        files = fnmatch.filter(files, self.name + "_*")
 
         # Duration not set
         duration = None
@@ -77,30 +79,33 @@ class VideosTable:
         formats = []
         for f in files:
             if f.find('.tstream') == -1:
-                # Duration (if not set yet)
+                # Duration (set once)
                 if duration == None:
                     duration = self.getVideoDuration(f)
+
                 format_ = {}
-                format_['def'] = f[(f.rfind('_')+1):f.rfind('.')]
-                ext = f[(f.rfind('.')+1):]
-                if ext != self.default_video_ext:
-                    format_['ext'] = ext
-                if format_['def'] != self.getVideoDefinition(f):
+                format_['res'] = self.getVideoResolution(f)
+                format_['dar'] = self.getVideoDar(f)
+                format_['ext'] = f[(f.rfind('.')+1):]
+
+                fileDef = f[(f.rfind('_')+1):f.rfind('.')]
+                videoDef = format_['res'].split('x')[1] + 'p'
+                if fileDef != videoDef:
                     raise VideoDefException(f)
+
                 formats.append(format_)
 
         return (duration, formats)
 
     def insert(self):
-        if self.duration == None or self.formats_json == None or self.tags_json == None:
-            print "Bzzzz"
-        query = "INSERT INTO `" + self.tableName + "` (name, title, description, duration, formats, category, user_id, tags, date, thumbs_count, default_thumb) VALUES ('" + self.name + "', '" + self.title + "', '" + self.description + "', '" + self.duration + "', '" + self.formats_json + "', '" + self.category + "', " + str(self.user_id) + ", '" + self.tags_json + "', NOW(), " + str(self.thumbs_count) + ", " + str(self.default_thumb) + ")"
+        #if self.duration == None or self.formats_json == None or self.tags_json == None:
+        query = "INSERT INTO `" + self.tableName + "` (name, title, description, duration, formats, category_id, user_id, tags, date, thumbs_count, default_thumb) VALUES ('" + self.name + "', '" + self.title + "', '" + self.description + "', '" + self.duration + "', '" + self.formats_json + "', " + str(self.category_id) + ", " + str(self.user_id) + ", '" + self.tags_json + "', NOW(), " + str(self.thumbs_count) + ", " + str(self.default_thumb) + ")"
         self.dbCur.execute(query)    
     
     @staticmethod
-    def getAllNames(dbCur, category):
+    def getAllNames(dbCur, category_id):
         allNames = set()
-        query = "SELECT name FROM `" + VideosTable.tableName + "` WHERE category = '" + category + "'"
+        query = "SELECT name FROM `" + VideosTable.tableName + "` WHERE category_id = " + str(category_id)
         dbCur.execute(query)
 
         while(True):
@@ -123,13 +128,13 @@ class VideoDefException(Exception):
 def main():
     # Check arguments.
     if len(sys.argv) < 3:
-        sys.stdout.write('usage: ' + sys.argv[0] + ' videos_info_file videos_dir category\n')
+        sys.stdout.write('usage: ' + sys.argv[0] + ' videos_info_file videos_dir category_id\n')
         exit(1)
 
     # Command line arguments
     fileName = sys.argv[1]
     directory = sys.argv[2]
-    category = sys.argv[3]
+    category_id = int(sys.argv[3])
     if len(sys.argv) == 4:
         thumbsDir = sys.argv[3]
     else:
@@ -140,7 +145,7 @@ def main():
             passwd = 'ahmitairoo', db = 'koala_livinglab')
     dbCur = dbConn.cursor()
 
-    allNames = VideosTable.getAllNames(dbCur, category)
+    allNames = VideosTable.getAllNames(dbCur, category_id)
 
     # Open info file
     file = open(fileName, 'r')
@@ -157,7 +162,7 @@ def main():
         if not name in allNames:
             sys.stdout.write(str(i) + '. ' + name + '\r')
             try:
-                video = VideosTable(dbCur, directory, name, title, description, tags, category)
+                video = VideosTable(dbCur, directory, name, title, description, tags, category_id)
                 video.insert()
                 i = i+1
 
@@ -169,6 +174,7 @@ def main():
     # Clean-up
     dbCur.close()
     dbConn.close()
+    sys.stdout.write('\n')
 
     return 0