Panopticode
unknown
python
a year ago
5.4 kB
21
Indexable
import cv2 import time from ffpyplayer.player import MediaPlayer import random from numpy import * import os last_transition = None last_video = None last_message = None video_count = 0 demo_set = ["messaging/looking.mp4","messaging/recalibration_sequence.mp4","messaging/iseeyoutalk.mp4","messaging/blindspot.mp4","messaging/assimilate.mp4"] def get_random_segment_start_end(cap, min_segment_duration=5, max_segment_duration= 15): total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) fps = cap.get(cv2.CAP_PROP_FPS) if cap.get(cv2.CAP_PROP_FPS) > 0 else 1 total_duration = total_frames / fps # Total video duration in seconds max_segment_duration = min(max_segment_duration, total_duration) if total_duration <= min_segment_duration: return 0, total_duration start_time = random.uniform(0, max(0, total_duration - min_segment_duration)) end_time = min(start_time + random.uniform(min_segment_duration, max_segment_duration), total_duration) return start_time, end_time def video(file, start_time_sec, end_time_sec, play_whole_video=False): # Open video file cap = cv2.VideoCapture(file) if not cap.isOpened(): print(f"Failed to open video {file}") return if play_whole_video: # If playing the whole video, reset start and end times start_time_sec = 0 end_time_sec = cap.get(cv2.CAP_PROP_FRAME_COUNT) / cap.get(cv2.CAP_PROP_FPS) # Seek to start time cap.set(cv2.CAP_PROP_POS_MSEC, start_time_sec * 1000) player = MediaPlayer(file, ff_opts={'ss': start_time_sec, 'an':False}) cv2.namedWindow("Video", cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty("Video", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) # Start time for synchronization start_time = time.time() while cap.isOpened(): ret, frame = cap.read() if not ret: return 'end' current_time = time.time() elapsed = (current_time - start_time) if elapsed > (end_time_sec - start_time_sec): return 'end' audio_frame, val = player.get_frame(show=False) if val == 'eof': return 'end' frame_resized = cv2.resize(frame, (1920, 1080)) cv2.imshow("Video", frame_resized) play_time = int(cap.get(cv2.CAP_PROP_POS_MSEC)) target_elapsed = (play_time - start_time_sec * 1000) actual_elapsed = (current_time - start_time) * 1000 sleep = max(1, int(33 - (actual_elapsed - target_elapsed))) if cv2.waitKey(sleep) & 0xFF == ord("q"): return 'quit' player.close_player() cap.release() def main(videos, transitions, messaging, is_demo): global last_video global last_transition global last_message global video_count global demo_set excluded_videos = videos.copy(); if last_video != None: excluded_videos.remove(last_video) # make sure we don't get previous transition excluded_transitions = transitions.copy(); if last_transition != None: excluded_transitions.remove(last_transition) # make sure we don't get previous message excluded_messages = messaging.copy(); if(is_demo == False): if last_message != None: excluded_messages.remove(last_message) random_video = random.choice(excluded_videos); random_transition = random.choice(excluded_transitions); last_transition = random_transition result = None if(video_count == 4): random_message = random.choice(excluded_messages); if(is_demo): random_message = demo_set.pop() result = video(random_message, 0, 0, play_whole_video=True) last_message = random_message video_count = 0 else: cap = cv2.VideoCapture(random_video); start_time, end_time = get_random_segment_start_end(cap) cap.release() result = video(random_video, start_time, end_time) last_video = random_video video_count += 1 if result == 'quit': cv2.destroyAllWindows() return elif result == 'end': glre = video(random_transition, 0, 0, play_whole_video=True); if glre == 'quit': cv2.destroyAllWindows() return; elif glre == 'end': main(videos, transitions, messaging, is_demo) video_files = os.listdir("fear") video_paths = [os.path.join("fear", file) for file in video_files] transition_files = os.listdir("transitions") transition_paths = [os.path.join("transitions", file) for file in transition_files] messaging_files = os.listdir("messaging") messaging_paths = [os.path.join("messaging", file) for file in messaging_files] result = video('boot_1.mp4', 0, 0, play_whole_video=True) random_transition = random.choice(transition_paths) last_transition = random_transition if result == 'quit': cv2.destroyAllWindows() elif result == 'end': glre = video(random_transition, 0, 0, play_whole_video=True); if glre == 'quit': cv2.destroyAllWindows() elif glre == 'end': main(video_paths, transition_paths, messaging_paths, is_demo=False);
Editor is loading...
Leave a Comment