- 1 year ago
Playing MP4 Fullscreen in Python
Comment
1 / 1
To play an MP4 video in fullscreen mode using Python, you can utilize the pygame
library. Here's an example of how you can achieve this:
Install Pygame:
- Open a Command Prompt or Terminal.
- Run the following command to install Pygame:plaintext
pip install pygame
Write the Python Code:
pythonimport pygame
def play_video(video_path):
pygame.init()
pygame.mixer.quit()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.set_caption('Fullscreen Video Player')
clock = pygame.time.Clock()
video = pygame.movie.Movie(video_path)
video_screen = pygame.Surface(video.get_size()).convert()
video.play()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if video.get_busy():
video_screen.blit(video.get_surface(), (0, 0))
screen.blit(pygame.transform.scale(video_screen, screen.get_size()), (0, 0))
pygame.display.flip()
clock.tick(30)
pygame.quit()
# Specify the path to your MP4 video file
video_path = "path/to/your/video.mp4"
play_video(video_path)
Replace "path/to/your/video.mp4" with the actual path to your MP4 video file.
Run the Python Script:
- Save the Python code in a file with a
.py
extension (e.g.,fullscreen_video.py
). - Open a Command Prompt or Terminal.
- Navigate to the directory where the Python script is located.
- Run the script using the following command:plaintext
python fullscreen_video.py
- Save the Python code in a file with a
The play_video
function initializes the Pygame library, sets the display mode to fullscreen, and creates a Pygame Movie
object to load and play the video file. The video is continuously blitted to the screen surface within the game loop, and the loop runs until the video finishes or the user closes the window.
By following these steps, you can play an MP4 video in fullscreen mode using Python and the pygame
library.