def all_videos(self, serialize=False): try: temp_session = Scope_Session() start = time.time() videos = temp_session.query(Video).filter( Video.status != -1).order_by( Video.upload_time.desc()).offset(40).limit(10) print videos print("查询所有video用时: %.2fs" % (time.time() - start)) new_start = time.time() count = temp_session.query(Video).filter( Video.status != -1).count() print count print("查询总共有多少video用时: %.2fs" % (time.time() - new_start)) new_start = time.time() count = temp_session.query(func.count(Video.status != -1)) print count print("新方式查询总共有多少video用时: %.2fs" % (time.time() - new_start)) clean_videos = [Video.get_new_instance(vi) for vi in videos] Scope_Session.remove() if serialize: return [vi.mini_serialize() for vi in clean_videos] else: return clean_videos except (Exception) as e: print "抓到exception" print "all_videos 操作失败" print e.message return []
def videos_at_page(self, page=1, key="", serialize=False): try: offset = (page - 1) * per_page temp_session = Scope_Session() # count = temp_session.query(func.count(Video.status != -1)) count = temp_session.query(Video).filter( Video.status != -1, Video.name.like('%' + key + '%')).count() page_count = int(math.ceil(count / float(per_page))) print page_count if page_count == 0: page_count = 1 page_indexs = [(i + 1) for i in range(page_count)] current_page = page videos = temp_session.query(Video).filter( Video.status != -1, Video.name.like('%' + key + '%')).order_by( Video.upload_time.desc()).offset(offset).limit(per_page) clean_videos = [Video.get_new_instance(vi) for vi in videos] Scope_Session.remove() if serialize: return [vi.mini_serialize() for vi in clean_videos], page_indexs, current_page else: return clean_videos, page_indexs, current_page except (Exception) as e: print "抓到exception" print "all_videos 操作失败" print e.message return [], [], 1
def get_all_fetching_caption_videos(self, serialize=False): try: temp_session = Scope_Session() videos = temp_session.query(Video).filter(Video.status == 7).all() clean_videos = [Video.get_new_instance(vi) for vi in videos] Scope_Session.remove() if serialize: return [vi.serialize() for vi in clean_videos] else: return clean_videos except (Exception) as e: print "抓到exception" print "get_all_fetching_caption_videos 操作失败" print e.message
def update_video(self, video): ## trick: 先查出来,再更新 try: temp_session = Scope_Session() videos = temp_session.query(Video).filter( Video.id == video.id).all() if len(videos) > 0: vi = videos[0] vi = Video.sync_old_video(old_vi=vi, vi=video) temp_session.add(vi) temp_session.commit() Scope_Session.remove() except (Exception) as e: print "抓到exception" print "update_video 操作失败" print e.message
def get_video_by_name(self, name, serialize=False): try: temp_session = Scope_Session() videos = temp_session.query(Video).filter( Video.name.like('%' + name + '%')).limit(1) clean_videos = [Video.get_new_instance(vi) for vi in videos] Scope_Session.remove() if serialize: return [vi.serialize() for vi in clean_videos] else: return clean_videos except (Exception) as e: print "抓到exception" print e.message print "get_video_by_name 操作失败"
def process_unprocessed(): files = [ f for f in os.listdir(config['UPLOAD_FOLDER']) if os.path.isfile(os.path.join(config['UPLOAD_FOLDER'], f)) and f not in IGNORED_FILES ] sql = "" videos = [] for f in files: if f.rsplit(".", 1)[1].lower() == "mp4": print f fName = f.rsplit(".", 1)[0] print fName hash_name = md5_name(fName) original = config['UPLOAD_FOLDER'] + f video_clip = VideoFileClip(original) duration = sec_2_time(video_clip.duration) duration_seconds = video_clip.duration dimention = "%d*%d" % (video_clip.size[0], video_clip.size[1]) size = round(float(os.path.getsize(original)) / 1024.0 / 1024.0, 2) size_str = "%.2f M" % size info = { 'duration': duration, 'duration_seconds': duration_seconds, 'dimention': dimention, 'size': size_str } info_str = json.dumps(info) hashed = config['UPLOAD_FOLDER'] + hash_name + '.mp4' cmd = 'mv ' + zhuanyi(original) + ' ' + zhuanyi(hashed) print cmd os.system(cmd) video = Video(name=fName, hash_name=hash_name, extension="mp4", status=0, update_time=datetime.now(), upload_time=datetime.now(), video_info=info_str) videos.append(video) # sql += 'insert into video (name, hash_name, extension, status, update_time, upload_time) values ("%s", "%s", "mp4", 0, current_timestamp, current_timestamp);' % (fName, hash_name) # print sql result = DATA_PROVIDER.add_unprocessed_videos( DATA_PROVIDER.main_queue_session, videos) print result
def process_processed(): videos = [] files = [ f for f in os.listdir(config['PROCESSED_FOLDER']) if os.path.isfile(os.path.join(config['PROCESSED_FOLDER'], f)) and f not in IGNORED_FILES ] for f in files: if f.rsplit(".", 1)[1].lower() == "mp4": print f fName = f.rsplit(".", 1)[0] print fName hash_name = md5_name(fName) original = config['PROCESSED_FOLDER'] + f video_clip = VideoFileClip(original) duration = sec_2_time(video_clip.duration) duration_seconds = video_clip.duration dimention = "%d*%d" % (video_clip.size[0], video_clip.size[1]) size = round(float(os.path.getsize(original)) / 1024.0 / 1024.0, 2) size_str = "%.2f M" % size info = { 'duration': duration, 'duration_seconds': duration_seconds, 'dimention': dimention, 'size': size_str } info_str = json.dumps(info) hashed = config['PROCESSED_FOLDER'] + hash_name + '.mp4' cmd = 'mv ' + zhuanyi(original) + ' ' + zhuanyi(hashed) print cmd os.system(cmd) ## 读取caption (不需要修改caption文件名,因为用不到了) caption_path = os.path.join(config['BOTTLENECK'], fName + ".txt") print caption_path caption = "" try: with open(caption_path, 'r') as f: caption = f.read() except: print "%s 读字幕文件失败" % fName return ## 更改zip名 ziped_gif_file_name = fName + ".zip" new_ziped_gif_file_name = hash_name + ".zip" ziped_gif_path = os.path.join(config['ZIPED_GIF_FOLDER'], ziped_gif_file_name) new_ziped_gif_path = os.path.join(config['ZIPED_GIF_FOLDER'], new_ziped_gif_file_name) cmd = 'mv ' + zhuanyi(ziped_gif_path) + ' ' + zhuanyi( new_ziped_gif_path) print cmd os.system(cmd) ## 更改gif名称 gifs_dir = config['GIF_FOLDER'] + fName new_gifs_dir = config['GIF_FOLDER'] + hash_name index = 0 for f in sorted(os.listdir(gifs_dir)): if f.rsplit(".", 1)[1].lower() == "gif": new_name = "%.2d.gif" % index cmd = "mv " + zhuanyi(gifs_dir + '/' + f) + " " + zhuanyi(gifs_dir + '/' + new_name) print cmd os.system(cmd) index += 1 cmd_gif = 'mv ' + zhuanyi(gifs_dir) + " " + zhuanyi(new_gifs_dir) print cmd_gif os.system(cmd_gif) ## 更改original_gif名称 ori_gifs_dir = config['ORIGINAL_GIF_FOLDER'] + fName new_ori_gifs_dir = config['ORIGINAL_GIF_FOLDER'] + hash_name index = 0 for f in sorted(os.listdir(ori_gifs_dir)): if f.rsplit(".", 1)[1].lower() == "mp4": new_name = "%.2d.mp4" % index cmd = "mv " + zhuanyi(ori_gifs_dir + '/' + f) + " " + zhuanyi(ori_gifs_dir + '/' + new_name) print cmd os.system(cmd) index += 1 cmd_gif = 'mv ' + zhuanyi(ori_gifs_dir) + " " + zhuanyi( new_ori_gifs_dir) print cmd_gif os.system(cmd_gif) video = Video(name=fName, hash_name=hash_name, extension="mp4", status=1, update_time=datetime.now(), upload_time=datetime.now(), processed_time=datetime.now(), split_type=0, caption=caption, video_info=info_str) videos.append(video) result = DATA_PROVIDER.add_unprocessed_videos( DATA_PROVIDER.main_queue_session, videos) print result
def upload(): if request.method == 'POST': files = request.files['file'] upload_info_dic = {} if files: filename = files.filename.encode('utf-8') upload_info_dic['name'] = filename mime_type = files.content_type upload_info_dic['type'] = mime_type upload_info_dic['upload_time'] = str(datetime.now()) upload_info_dic['message'] = "" if not allowed_file(filename): upload_info_dic['error'] = "请上传MP4文件" upload_info_dic['size'] = "" else: # save file to disk fName = filename.rsplit('.', 1)[0] extension = filename.rsplit('.', 1)[1] fName = gen_file_name(fName) hash_name = md5_name(fName) uploaded_file_path = os.path.join(app.config['UPLOAD_FOLDER'], hash_name + "." + extension) try: files.save(uploaded_file_path) video_clip = VideoFileClip(uploaded_file_path) duration = sec_2_time(video_clip.duration) dimention = "%d*%d" % (video_clip.size[0], video_clip.size[1]) size = round( float(os.path.getsize(uploaded_file_path)) / 1024.0 / 1024.0, 2) size_str = "%.2f M" % size fps = video_clip.fps info = { 'duration': duration, 'dimention': dimention, 'size': size_str, 'fps': fps } info_str = json.dumps(info) upload_info_dic['size'] = size_str upload_info_dic['message'] = "上传成功" except (Exception) as e: upload_info_dic['size'] = "" upload_info_dic['error'] = "处理失败: %s" % e.message print "%s上传文件完成后,处理失败" % filename print e.message return simplejson.dumps({"files": [upload_info_dic]}) print "saved path:" print uploaded_file_path video = Video(name=fName, hash_name=hash_name, extension="mp4", status=0, update_time=datetime.now(), upload_time=datetime.now(), video_info=info_str) ## 保存成功后, 在数据库中添加记录 DATA_PROVIDER.add_unprocessed_videos([video]) # return json for js call back return simplejson.dumps({"files": [upload_info_dic]}) if request.method == 'GET': return simplejson.dumps({"files": []}) return redirect(url_for('index'))