EasyVideoPlayer.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import cv2
  2. import os
  3. from pathlib import Path
  4. from ffpyplayer.player import MediaPlayer
  5. # User input for the name of the image file.
  6. video_name = input("Name of the video file that you want to play: ")
  7. # User input for the path of the image file.
  8. video_directory_guess = input("Directory that may contain the video: ")
  9. # This function finds your file. If you don't know the directory just type '/'
  10. def find_the_video(file_name, directory_name):
  11. files_found = []
  12. for path, subdirs, files in os.walk(directory_name):
  13. for name in files:
  14. if(file_name == name):
  15. file_path = os.path.join(path, name)
  16. files_found.append(file_path)
  17. print(files_found)
  18. return files_found[0] # Return the path.
  19. # Initialize the path of the image file.
  20. video_directory = Path(find_the_video(video_name, video_directory_guess))
  21. # Initialize the parent directory of the image path.
  22. new_working_directory = video_directory.parent
  23. # Change the working directory of the script.
  24. os.chdir(new_working_directory)
  25. video_path = find_the_video(video_name, video_directory_guess)
  26. def PlayVideo(video_path):
  27. video = cv2.VideoCapture(video_path)
  28. player = MediaPlayer(video_path)
  29. while True:
  30. grabbed, frame = video.read()
  31. audio_frame, val = player.get_frame()
  32. if not grabbed:
  33. print("End of video")
  34. break
  35. if cv2.waitKey(28) & 0xFF == ord("q"):
  36. break
  37. cv2.imshow("Video", frame)
  38. if val != 'eof' and audio_frame is not None:
  39. img, t = audio_frame
  40. video.release()
  41. cv2.destroyAllWindows()
  42. PlayVideo(video_path)