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
25 def __init__(self, dbCur, directory, name, title, description, tags, category_id):
27 self.directory = directory
31 self.description = description
32 self.duration, self.formats = self.findVideosMeta()
33 self.formats_json = json.dumps(self.formats, separators=(',', ':'))
34 self.category_id = category_id
36 tagList = tags.split(',')
40 self.tags[tag.strip()] = 0
41 self.tags_json = json.dumps(self.tags, separators=(',', ':'))
43 def getVideoResolution(self, fileName):
44 pipe = subprocess.Popen('mediainfo --Inform="Video;%Width%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
45 width = pipe.readline().strip()
47 pipe = subprocess.Popen('mediainfo --Inform="Video;%Height%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
48 height = pipe.readline().strip()
50 return width + 'x' + height
52 def getVideoDar(self, fileName):
53 pipe = subprocess.Popen('mediainfo --Inform="Video;%DisplayAspectRatio/String%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
54 dar = pipe.readline().strip()
58 def getVideoDuration(self, fileName):
59 pipe = subprocess.Popen('mediainfo --Inform="General;%Duration/String3%" ' + os.path.join(self.directory, fileName), shell=True, stdout=subprocess.PIPE).stdout
60 output = pipe.readline().strip()
61 dotPos = output.find('.')
62 if output[0:2] == '00':
63 duration = output[3:dotPos]
65 duration = output[:dotPos]
70 # Returns a pair with duration and formats list.
71 def findVideosMeta(self):
72 files = [f for f in os.listdir(self.directory) if os.path.isfile(os.path.join(self.directory, f))]
73 files = fnmatch.filter(files, self.name + "_*")
81 if f.find('.tstream') == -1:
84 duration = self.getVideoDuration(f)
87 format_['res'] = self.getVideoResolution(f)
88 format_['dar'] = self.getVideoDar(f)
89 format_['ext'] = f[(f.rfind('.')+1):]
91 fileDef = f[(f.rfind('_')+1):f.rfind('.')]
92 videoDef = format_['res'].split('x')[1] + 'p'
93 if fileDef != videoDef:
94 raise VideoDefException(f)
96 formats.append(format_)
98 return (duration, formats)
101 #if self.duration == None or self.formats_json == None or self.tags_json == None:
102 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) + ")"
103 self.dbCur.execute(query)
106 def getAllNames(dbCur, category_id):
108 query = "SELECT name FROM `" + VideosTable.tableName + "` WHERE category_id = " + str(category_id)
112 row = dbCur.fetchone()
120 class VideoDefException(Exception):
121 def __init__(self, value):
122 self.value = 'Invalid video definition in file name "' + value + '"! '
125 return repr(self.value)
130 if len(sys.argv) < 3:
131 sys.stdout.write('usage: ' + sys.argv[0] + ' videos_info_file videos_dir category_id\n')
134 # Command line arguments
135 fileName = sys.argv[1]
136 directory = sys.argv[2]
137 category_id = int(sys.argv[3])
138 if len(sys.argv) == 4:
139 thumbsDir = sys.argv[3]
144 dbConn = MySQLdb.connect(host = 'koala.cs.pub.ro', user = 'koala_p2pnext',
145 passwd = 'ahmitairoo', db = 'koala_livinglab')
146 dbCur = dbConn.cursor()
148 allNames = VideosTable.getAllNames(dbCur, category_id)
151 file = open(fileName, 'r')
153 # Read videos info file
155 name = file.readline()
158 title = file.readline().strip()
159 description = file.readline().strip()
160 tags = file.readline().strip()
162 if not name in allNames:
163 sys.stdout.write(str(i) + '. ' + name + '\r')
165 video = VideosTable(dbCur, directory, name, title, description, tags, category_id)
169 except VideoDefException as e:
170 sys.stdout.write('\n' + e.value + '\n')
172 name = file.readline()
177 sys.stdout.write('\n')
182 if __name__ == "__main__":