|
| 1 | +import cv2 ## Used for the video. |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | +import numpy as np |
| 5 | +from ffpyplayer.player import MediaPlayer ## Used for the audio. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +video_name = input("Please enter the name of the video file that you want to play: ") ## User input for the name of the image file. |
| 10 | +video_directory_guess = input("Please enter the directory that may contain the video: ") ## User input for the path of the image file. |
| 11 | + |
| 12 | +## This function looks for and finds the desired file. You can specify a parent directory for the fundtion to look for, however if you have no idea where a file is; this functio will find it for you, just slower. If you have no idea where a file is, just type "/". |
| 13 | +def find_the_video(file_name, directory_name): |
| 14 | + files_found = [] |
| 15 | + for path, subdirs, files in os.walk(directory_name): |
| 16 | + for name in files: |
| 17 | + if(file_name == name): |
| 18 | + file_path = os.path.join(path,name) |
| 19 | + files_found.append(file_path) |
| 20 | + |
| 21 | + print(files_found) |
| 22 | + return files_found[0] ## Return the path. |
| 23 | + |
| 24 | + |
| 25 | +video_directory = Path(find_the_video(video_name, video_directory_guess)) ## Initialize the path of the image file. |
| 26 | +new_working_directory = video_directory.parent ## Initialize the parent directory of the image path. |
| 27 | +os.chdir(new_working_directory) ## Change the working directory of the script to the parent directory of the image path. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +video_path=find_the_video(video_name, video_directory_guess) |
| 32 | + |
| 33 | +def PlayVideo(video_path): |
| 34 | + |
| 35 | + video=cv2.VideoCapture(video_path) |
| 36 | + player = MediaPlayer(video_path) |
| 37 | + |
| 38 | + while True: |
| 39 | + grabbed, frame=video.read() |
| 40 | + audio_frame, val = player.get_frame() |
| 41 | + if not grabbed: |
| 42 | + print("End of video") |
| 43 | + break |
| 44 | + if cv2.waitKey(28) & 0xFF == ord("q"): |
| 45 | + break |
| 46 | + cv2.imshow("Video", frame) |
| 47 | + if val != 'eof' and audio_frame is not None: |
| 48 | + #audio |
| 49 | + img, t = audio_frame |
| 50 | + video.release() |
| 51 | + cv2.destroyAllWindows() |
| 52 | + |
| 53 | + |
| 54 | +PlayVideo(video_path) |
0 commit comments