3 # Copyright Calin-Andrei Burloiu, calin.burloiu@gmail.com
5 # Automatically publishes videos in P2P-Tube DB based on the video files and
6 # a videos info file. Parameters: videos_info_file videos_directory category_id
24 default_video_ext = 'ogv'
26 def __init__(self, dbCur, directory, name, title, description, tags, category_id):
28 self.directory = directory
32 self.description = description
33 self.duration, self.formats = self.findVideosMeta()
34 self.formats_json = json.dumps(self.formats, separators=(',', ':'))
35 self.category_id = category_id
37 tagList = tags.split(',')
41 self.tags[tag.strip()] = 0
42 self.tags_json = json.dumps(self.tags, separators=(',', ':'))
44 def getVideoDefinition(self, fileName):
45 pipe = subprocess.Popen('mediainfo --Inform="Video;%Height%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
46 height = pipe.readline().strip()
48 pipe = subprocess.Popen('mediainfo --Inform="Video;%ScanType%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
49 scanType = pipe.readline().strip()
50 if scanType == '' or scanType == 'Progressive':
52 elif scanType == 'Interlaced':
55 return height + scanType
57 def getVideoDuration(self, fileName):
58 pipe = subprocess.Popen('mediainfo --Inform="General;%Duration/String3%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
59 output = pipe.readline().strip()
60 dotPos = output.find('.')
61 if output[0:2] == '00':
62 duration = output[3:dotPos]
64 duration = output[:dotPos]
69 # Returns a pair with duration and formats list.
70 def findVideosMeta(self):
71 files = [f for f in os.listdir(self.directory) if os.path.isfile(os.path.join(self.directory, f))]
72 files = fnmatch.filter(files, self.name + "*")
80 if f.find('.tstream') == -1:
81 # Duration (if not set yet)
83 duration = self.getVideoDuration(f)
85 format_['def'] = f[(f.rfind('_')+1):f.rfind('.')]
86 ext = f[(f.rfind('.')+1):]
87 if ext != self.default_video_ext:
89 if format_['def'] != self.getVideoDefinition(f):
90 raise VideoDefException(f)
91 formats.append(format_)
93 return (duration, formats)
96 if self.duration == None or self.formats_json == None or self.tags_json == None:
98 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) + ")"
99 self.dbCur.execute(query)
102 def getAllNames(dbCur, category_id):
104 query = "SELECT name FROM `" + VideosTable.tableName + "` WHERE category_id = " + str(category_id)
108 row = dbCur.fetchone()
116 class VideoDefException(Exception):
117 def __init__(self, value):
118 self.value = 'Invalid video definition in file name "' + value + '"! '
121 return repr(self.value)
126 if len(sys.argv) < 3:
127 sys.stdout.write('usage: ' + sys.argv[0] + ' videos_info_file videos_dir category_id\n')
130 # Command line arguments
131 fileName = sys.argv[1]
132 directory = sys.argv[2]
133 category_id = int(sys.argv[3])
134 if len(sys.argv) == 4:
135 thumbsDir = sys.argv[3]
140 dbConn = MySQLdb.connect(host = 'koala.cs.pub.ro', user = 'koala_p2pnext',
141 passwd = 'ahmitairoo', db = 'koala_livinglab')
142 dbCur = dbConn.cursor()
144 allNames = VideosTable.getAllNames(dbCur, category_id)
147 file = open(fileName, 'r')
149 # Read videos info file
151 name = file.readline()
154 title = file.readline().strip()
155 description = file.readline().strip()
156 tags = file.readline().strip()
158 if not name in allNames:
159 sys.stdout.write(str(i) + '. ' + name + '\r')
161 video = VideosTable(dbCur, directory, name, title, description, tags, category_id)
165 except VideoDefException as e:
166 sys.stdout.write('\n' + e.value + '\n')
168 name = file.readline()
173 sys.stdout.write('\n')
178 if __name__ == "__main__":