def save_inference_video(sourceFile, targetFile, sess, image_shape, logits, keep_prob, input_image): # Run NN on test images and save them to HD clip = VideoFileClip(sourceFile, audio=False) output_clip = clip.fx(gen_movie_output, sess, logits, keep_prob, input_image, image_shape) output_clip.write_videofile(targetFile, audio=False)
def overwrite_audio(video_path, audio_path, silence): """ :param video_path: 영상에 쓰일 하이라이트 영상 경로 :param audio_path: 음계가 조정된 오디오 파일 경로 :param silence: 오디오 파일 제거 :return: 조정된 하이라이트 영상 경로 """ # Access video file and audio file video_clip = VideoFileClip(video_path) audio_clip = AudioFileClip(audio_path) # Create random file name video_path = get_random_name('mp4') # Adjust video speed to sync with audio file adjusted_video = video_clip.fx(vfx.speedx, final_duration=audio_clip.duration) # Overwrite video audio if silence: adjusted_video.audio = None adjusted_video.write_videofile(video_path, codec='libx264', audio_codec='aac') else: adjusted_video.audio = audio_clip adjusted_video.write_videofile(video_path, codec='libx264', audio_codec='aac') return video_path
def save_inference_video_samples(sess, videos, model_checkpoint, video_fps, video_output_folder, image_shape): from moviepy.editor import VideoFileClip, vfx graph = tf.get_default_graph() saver = tf.train.Saver() try: saver.restore(sess, model_checkpoint) except: print("Couldn't load model last checkpoint ({}).".format( model_checkpoint)) print( "You need to either provide the required checkpoint files or train the network from scratch!" ) return input_image_op = graph.get_tensor_by_name("image_input:0") logits_op = graph.get_tensor_by_name("decoder_logits:0") keep_prob = graph.get_tensor_by_name("keep_prob:0") for video in videos: if not os.path.exists(video_output_folder): os.makedirs(video_output_folder) result_path = video_output_folder + os.path.basename(video) if not os.path.isfile(video): print("Video {} doesn't exist!".format(video)) else: clip1 = VideoFileClip(video) #.subclip(*clip_part) video_slowdown_factor = video_fps / clip1.fps clip1 = clip1.fx(vfx.speedx, video_slowdown_factor) white_clip = clip1.fl_image(lambda img: process_video_image( sess, logits_op, keep_prob, input_image_op, img, image_shape)) white_clip.write_videofile(result_path, audio=False, fps=video_fps)
def run(): num_classes = 2 image_shape = (160, 576) data_dir = './data' runs_dir = './runs' graph = load_graph(GRAPH_FILE) with tf.Session(graph=graph) as sess: input_tensor_name = 'image_input:0' output_tensor_name = "output_layer/BiasAdd:0" keep_prob_tensor_name = 'keep_prob:0' input_tensor = graph.get_tensor_by_name(input_tensor_name) output_tensor = graph.get_tensor_by_name(output_tensor_name) keepprob_tensor = graph.get_tensor_by_name(keep_prob_tensor_name) image = scipy.misc.imresize(scipy.misc.imread("test.png"), image_shape) outimg = infer(sess, input_tensor, output_tensor, keepprob_tensor, image_shape, image ) scipy.misc.imsave("output.png", outimg) clip1 = VideoFileClip('./project_video.mp4').subclip(1, 1.5) projectClip = clip1.fx(frame_infer, sess, input_tensor, output_tensor, keepprob_tensor, (288,512)) projectClip.write_videofile('./challenge_results.mp4', audio=False)
def processVideo(video_path, output_path, *process): clip = VideoFileClip(video_path) clip = clip.fx(*process) clip.write_videofile(output_path, temp_audiofile='temp-audio.m4a', remove_temp=True, codec="libx264", audio_codec="aac")
def movie_upside(vedio_path): """ 倒序播放 :return: """ clip1 = VideoFileClip(vedio_path, audio=False).fx(vfx.crop).subclip(0, 4) clip_upside = clip1.fx(vfx.time_mirror) #倒序播放 return clip_upside
def mirror(): print("Hello from Python M") data = json.loads(sys.argv[2]) video_name = data['name'] xy = data['xy'] try: video = VideoFileClip("./videos/" + video_name) if xy == "X": print("hello from X") newclip = (video.fx(vfx.mirror_x, apply_to='mask')) newclip.write_videofile("./videos/m_" + video_name) elif xy == "Y": print("hello from Y") newclip = (video.fx(vfx.mirror_y, apply_to='mask')) newclip.write_videofile("./videos/m_" + video_name) print("Mirror_OK") except Exception: print("Mirror_NotOK")
def supersample(d, nframes): count = 950 for i in glob.glob(r"F:/UCF/help/remaining/*.mp4"): vi = VideoFileClip(i) my_clip = vi.fx(vfx.supersample, d, nframes) my_clip.write_videofile(r"F:/UCF/help/supersample/vid%d.mp4" % count) my_clip.close() #cv2.imwrite("vid%d.mp4" % count, image) count = count + 1
def center_crop(x_center, y_center, width, height): count = 960 for i in glob.glob(r"F:/UCF/help/videos/*.mp4"): vi = VideoFileClip(i) my_clip = vi.fx(vfx.crop, x_center, y_center, width, height) my_clip.write_videofile(r"F:/UCF/help/center_crop/vid%d.mp4" % count) my_clip.close() #cv2.imwrite("vid%d.mp4" % count, image) count = count + 1
def upsampling(amt): count = 400 for i in glob.glob(r"F:/UCF/help/videos/*.mp4"): vi = VideoFileClip(i) my_clip = vi.fx(vfx.speedx, amt) my_clip.write_videofile(r"F:/UCF/help/upsample_2.5/vid%d.mp4" % count) my_clip.close() #cv2.imwrite("vid%d.mp4" % count, image) count = count + 1
def addedVideos(): clip1 = VideoFileClip("text.mp4").margin(15) clip2 = clip1.fx(vfx.mirror_x) # X 轴镜像 clip3 = clip1.fx(vfx.mirror_y) # Y 轴镜像 clip4 = clip1.resize(0.6) # 尺寸等比例缩放0。6 final_clip = clips_array([ [clip1], [clip3] ]) final_clip.write_videofile("my_stack.mp4")
def supersample(d, nframes): count = 1608 for i in glob.glob(r"F:/UCF/non-help/videos_non_help/*.mp4"): vi = VideoFileClip(i) my_clip = vi.fx(vfx.supersample, d, nframes) my_clip.write_videofile( r"F:/UCF/non-help/supersample_non-help/vid%d.mp4" % count) my_clip.close() #cv2.imwrite("vid%d.mp4" % count, image) count = count + 1
def horizontal_flipping(): count = 800 for i in glob.glob(r"F:/UCF/help/videos/*.mp4"): vi = VideoFileClip(i) my_clip = vi.fx(vfx.mirror_x) my_clip.write_videofile(r"F:/UCF/help/horizontal_flip/vid%d.mp4" % count) my_clip.close() #cv2.imwrite("vid%d.mp4" % count, image) count = count + 1
def change_videos(effect, video_directory_str): video_directory = os.fsencode(video_directory_str) for file in os.listdir(video_directory): filename = os.fsdecode(file) # os.rename(video_directory_str + "/" + filename, video_directory_str + "/" + filename.replace(" ", "")) if filename.endswith(".mp4"): video = VideoFileClip(video_directory_str + "/" + filename) if effect == "mirror_x": out = video.fx(vfx.mirror_x) new_filename = filename.split(".")[0] + '_mirror_x.mp4' else: out = video.fx(vfx.time_mirror) new_filename = filename.split(".")[0] + '_time_mirror.mp4' out.write_videofile("../videos_output/" + new_filename) print("Video save done", new_filename) print() video.close()
def process_package(source: str): log.info(''.join(["SOURCE: ", source])) main_clip = VideoFileClip(source).margin(10) clip2 = main_clip.fx(vfx.mirror_x) clip3 = main_clip.fx(vfx.mirror_y) clip4 = main_clip.resize(0.60) # downsize 60% final_clip = clips_array([[main_clip, clip2], [clip3, clip4]]) final_clip.resize(width=480) return final_clip
def downsampling(amt): count = 1182 for i in glob.glob("F:/UCF/non-help/videos_non_help/*.mp4"): vi = VideoFileClip(i) my_clip = vi.fx(vfx.speedx, amt) my_clip.write_videofile( "F:/UCF/non-help/downsampling_1.5_non-help/vid%d.mp4" % count) my_clip.close() #cv2.imwrite("vid%d.mp4" % count, image) count = count + 1
def process_video(sess, image_shape, logits, keep_prob, input_image, input_video): in_vid = 'data/videos/' + input_video out_vid = 'runs/videos/out_' + input_video clip = VideoFileClip(in_vid) #clip = clip.subclip(15,20) video_clip = clip.fx(video_pipeline, sess, image_shape, logits, keep_prob, input_image) video_clip.write_videofile(out_vid, audio=False) pass
def extractVideoFrames(): ''' Uses moveipy and the pipeline developed above to render each frame of the input video as a given type. :return: output processed video ''' videoType = 'final' project_output = 'video/{0}.mp4'.format(videoType) input_clip = VideoFileClip("project_video.mp4") project_clip = input_clip.fx(processVideo, videoType) project_clip.write_videofile(project_output, audio=False)
def fade(): print("Hello from Python F") data = json.loads(sys.argv[2]) video_name = data['name'] inOut = data['inOut'] dur = float(data['fd']) print(video_name) print(inOut) print(dur) try: video = VideoFileClip("./videos/" + video_name) if inOut == "fadeIn": print("hello from fadeIn") newclip = (video.fx(vfx.fadein, dur, initial_color=None)) newclip.write_videofile("./videos/f_" + video_name) elif inOut == "fadeOut": print("hello from fadeout") newclip = (video.fx(vfx.fadeout, dur, final_color=None)) newclip.write_videofile("./videos/f_" + video_name) print("Fade_OK") except Exception: print("Fade_NotOK")
def blackwhite(): print("Hello from Python BW") data = json.loads(sys.argv[2]) video_name = data['name'] print(video_name) try: video = VideoFileClip("./videos/" + video_name) newclip = (video.fx(vfx.blackwhite, RGB=None, preserve_luminosity=True)) newclip.write_videofile("./videos/bw_" + video_name) print("BlackWhite_OK") except Exception: print("BlackWhite_NotOK")
def gamma(): print("Hello from Python G") data = json.loads(sys.argv[2]) video_name = data['name'] gv = float(data['gv']) print(video_name) try: video = VideoFileClip("./videos/" + video_name) newclip = (video.fx(vfx.gamma_corr, gv)) newclip.write_videofile("./videos/g_" + video_name) print("gamma_OK") except Exception: print("gamma_NotOK")
def processVideo(videoPath, outputDir): camera_mtx, camera_dist, M, Minv = getCameraPerspectiveMatrix() left_lane = Line() right_lane = Line() #print('M matrix: \n',M) videoFileName = videoPath.split('/')[-1] print('video file name:', videoFileName) output = outputDir + '/out' + videoFileName print('out_video:', output) clip = VideoFileClip(videoPath) #.subclip(35,45) processed_clip = clip.fx(transformVideo, camera_mtx, camera_dist, M, Minv, left_lane, right_lane) processed_clip.write_videofile(output, audio=False)
def brightness(): print("Hello from Python BR") data = json.loads(sys.argv[2]) video_name = data['name'] bv = float(data['bv']) print(video_name) print(bv) try: video = VideoFileClip("./videos/" + video_name) newclip = (video.fx(vfx.colorx, bv)) newclip.write_videofile("./videos/br_" + video_name) print("Brightness_OK") except Exception: print("Brightness_NotOK")
def speed(): print("Hello from Python S") data = json.loads(sys.argv[2]) video_name = data['name'] sx = float(data['sx']) print(video_name) print(sx) try: video = VideoFileClip("./videos/" + video_name) newclip = (video.fx(vfx.speedx, factor=sx)) newclip.write_videofile("./videos/s_" + video_name) print("Speed_OK") except Exception: print("Speed_NotOK")
def run(): NUM_CLASSES = 2 image_shape = (160, 576) data_dir = './data' runs_dir = './runs' tests.test_for_kitti_dataset(data_dir) EPOCHS = 30 BATCH_SIZE = 8 # Download pretrained vgg model helper.maybe_download_pretrained_vgg(data_dir) # TF Placeholders truth_label = tf.placeholder( tf.float32, shape=[None, image_shape[0], image_shape[1], NUM_CLASSES], name="truth_lbls") learning_rate = tf.placeholder(tf.float32) with tf.Session() as sess: # Path to vgg model vgg_path = os.path.join(data_dir, 'vgg') # Create function to get batches get_batches_fn = helper.gen_batch_function( os.path.join(data_dir, 'data_road/training'), image_shape) # Build NN using load_vgg, layers, and optimize function input_image, keep_prob, layer3_out, layer4_out, layer7_out = load_vgg( sess, vgg_path) nn_last_layer = layers(layer3_out, layer4_out, layer7_out, NUM_CLASSES) logits, train_op, cross_entropy_loss = optimize( nn_last_layer, truth_label, learning_rate, NUM_CLASSES) # Train NN using the train_nn function train_nn(sess, EPOCHS, BATCH_SIZE, get_batches_fn, train_op, cross_entropy_loss, input_image, truth_label, keep_prob, learning_rate) # Save inference data using helper.save_inference_samples helper.save_inference_samples(runs_dir, data_dir, sess, image_shape, logits, keep_prob, input_image) # OPTIONAL: Apply the trained model to a video # See advanced lane finding from Term 1 clip1 = VideoFileClip(data_dir + '/challenge_video.mp4').subclip(1, 20) projectClip = clip1.fx(clip_find_lane, sess, logits, keep_prob, input_image, image_shape) projectClip.write_videofile(runs_dir + '/challenge_results.mp4', audio=False)
def make_video(animations, video, path, video_file_name): path = path background = VideoFileClip(path + video['name']) clips = [background] for index, animation in enumerate(animations): to_clip = path + animation['name'] start_time = animation['start_time'] clip = VideoFileClip(to_clip) masked_clip = clip.fx(vfx.mask_color, color=[0, 0, 0], thr=35, s=40) clips.append(masked_clip.set_start(start_time)) video = CompositeVideoClip(clips) video.write_videofile(f"{path}{video_file_name}.mp4") video.close()
def luminosity(): print("Hello from Python LM") data = json.loads(sys.argv[2]) video_name = data['name'] lbv = float(data['lbv']) lcv = float(data['lcv']) print(video_name) try: video = VideoFileClip("./videos/" + video_name) newclip = (video.fx(vfx.lum_contrast, lum=lbv, contrast=lcv, contrast_thr=127)) newclip.write_videofile("./videos/lm_" + video_name) print("Luminosity_OK") except Exception: print("Luminosity_NotOK")
def rotate(): print("Hello from Python R") data = json.loads(sys.argv[2]) video_name = data['name'] angle = float(data['rv']) print(video_name) print(angle) try: video = VideoFileClip("./videos/" + video_name) newclip = (video.fx(vfx.rotate, angle, unit='deg', resample='bicubic', expand=True)) newclip.write_videofile("./videos/r_" + video_name) print("Rotate_OK") except Exception: print("Rotate_NotOK")
def flip_folder(inputpath, outputpath): # 循环文件夹 for image_path in os.listdir(inputpath): if len(image_path.split('.')) > 1: print("开始转换" + image_path) # 拼接输入文件完整路径 full_path = os.path.join(inputpath, image_path) # 拼接输文件出路径 if inputpath == outputpath: full_output_path = os.path.join(outputpath, "out" + image_path) else: full_output_path = os.path.join(outputpath, image_path) # 区分图片/视频 suffix = image_path.split(".") if len(suffix) > 1: if suffix[1] == "mp4": # 视频 clip = VideoFileClip(full_path) reversed_clip = clip.fx(vfx.mirror_x) reversed_clip.write_videofile(full_output_path) elif suffix[1] == "png": # 图片 im = Image.open(full_path) im_mirror = ImageOps.mirror(im) im_mirror.save(full_output_path) elif suffix[1] == "jpg": # 图片 im = Image.open(full_path) im_mirror = ImageOps.mirror(im) im_mirror.save(full_output_path) elif suffix[1] == "gif": out_path = gif_split(full_path) out_path2 = gif_zh(out_path) gif_get2(out_path2, full_output_path) else: pass else: pass print("转换完成")
def speed_video(self, spd_factor = 5, path = "./videos/video_to_process*"): vtp = glob.glob(path) if vtp: # 1) Speed up the video, save as a copy vtp = vtp[0] file_ext_idx = vtp.find('.',1) tmp_vtp = vtp[:file_ext_idx] +"_COPY" + vtp[file_ext_idx:] in_loc = vtp out_loc = tmp_vtp # Import video clip clip = VideoFileClip(in_loc) # print("fps: {}".format(clip.fps)) # Modify the FPS #clip = clip.set_fps(clip.fps * spd_factor) clip = clip.set_fps(clip.fps * 1) # Apply speed up final = clip.fx(vfx.speedx, spd_factor) #print("fps: {}".format(final.fps)) # Save video clip final.write_videofile(out_loc) clip.close() # 2) Delete the original 1x speed video #camera = cv2.VideoCapture(vtp) #camera.release() os.remove(vtp) # 3) Rename the sped-up video to the original old_file_name = tmp_vtp new_file_name = vtp os.rename(old_file_name, new_file_name)
print "Checking movie file" for arg in sys.argv: print ("Argument: %s" % str(arg)) videofile = sys.argv[2] print ("Videofile %s" % str(videofile)) ### START MIRROR EFFECT if sys.argv[1] == '-m': from moviepy.editor import VideoFileClip, clips_array, vfx clip1 = VideoFileClip(sdir + videofile).margin(10) # add 10px contour clip2 = clip1.fx( vfx.mirror_x) clip3 = clip1.fx( vfx.mirror_y) clip4 = clip1.fx( vfx.mirror_y) clip4 = clip4.fx( vfx.mirror_x) #clip4 = clip1.resize(0.60) # downsize 60% final_clip = clips_array([[clip1, clip2], [clip3, clip4]]) final_clip.resize(width=1280).write_videofile(ddir + "mirror.mp4") ### END MIRROR EFFECT ### START EDIT MOVIE EDITS ALL IN ONE DIR CURRENTLY if sys.argv[1] == '-e':
def load_video(filename): # load clip from file name and resize it to 224px wide clip = VideoFileClip(filename).fx(resize, width=224) # desaturate the clip, center it vertically, then return it m = (224 - clip.h) / 2.0 return clip.fx(blackwhite).fx(margin, top=ceil(m), bottom=floor(m))