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'))