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
24 default_video_ext = 'ogv'
26 def __init__(self, dbCur, directory, name, title, description, tags, category):
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 = category
37 tagList = tags.split(',')
40 self.tags[tag.strip()] = 0
41 self.tags_json = json.dumps(self.tags, separators=(',', ':'))
43 def getVideoDefinition(self, fileName):
44 pipe = subprocess.Popen('mediainfo --Inform="Video;%Height%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
45 height = pipe.readline().strip()
47 pipe = subprocess.Popen('mediainfo --Inform="Video;%ScanType%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
48 scanType = pipe.readline().strip()
49 if scanType == '' or scanType == 'Progressive':
51 elif scanType == 'Interlaced':
54 return height + scanType
56 def getVideoDuration(self, fileName):
57 pipe = subprocess.Popen('mediainfo --Inform="General;%Duration/String3%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
58 output = pipe.readline().strip()
59 dotPos = output.find('.')
60 if output[0:2] == '00':
61 duration = output[3:dotPos]
63 duration = output[:dotPos]
68 # Returns a pair with duration and formats list.
69 def findVideosMeta(self):
70 files = [f for f in os.listdir(self.directory) if os.path.isfile(os.path.join(self.directory, f))]
71 files = fnmatch.filter(files, self.name + "*")
79 if f.find('.tstream') == -1:
80 # Duration (if not set yet)
82 duration = self.getVideoDuration(f)
84 format_['def'] = f[(f.rfind('_')+1):f.rfind('.')]
85 ext = f[(f.rfind('.')+1):]
86 if ext != self.default_video_ext:
88 if format_['def'] != self.getVideoDefinition(f):
89 raise VideoDefException(f)
90 formats.append(format_)
92 return (duration, formats)
95 if self.duration == None or self.formats_json == None or self.tags_json == None:
97 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) + ")"
98 self.dbCur.execute(query)
101 def getAllNames(dbCur, category):
103 query = "SELECT name FROM `" + VideosTable.tableName + "` WHERE category = '" + category + "'"
107 row = dbCur.fetchone()
115 class VideoDefException(Exception):
116 def __init__(self, value):
117 self.value = 'Invalid video definition in file name "' + value + '"! '
120 return repr(self.value)
125 if len(sys.argv) < 3:
126 sys.stdout.write('usage: ' + sys.argv[0] + ' videos_info_file videos_dir category\n')
129 # Command line arguments
130 fileName = sys.argv[1]
131 directory = sys.argv[2]
132 category = sys.argv[3]
133 if len(sys.argv) == 4:
134 thumbsDir = sys.argv[3]
139 dbConn = MySQLdb.connect(host = 'koala.cs.pub.ro', user = 'koala_p2pnext',
140 passwd = 'ahmitairoo', db = 'koala_livinglab')
141 dbCur = dbConn.cursor()
143 allNames = VideosTable.getAllNames(dbCur, category)
146 file = open(fileName, 'r')
148 # Read videos info file
150 name = file.readline()
153 title = file.readline().strip()
154 description = file.readline().strip()
155 tags = file.readline().strip()
157 if not name in allNames:
158 sys.stdout.write(str(i) + '. ' + name + '\r')
160 video = VideosTable(dbCur, directory, name, title, description, tags, category)
164 except VideoDefException as e:
165 sys.stdout.write('\n' + e.value + '\n')
167 name = file.readline()
176 if __name__ == "__main__":