In this tutorial we will change the functionality of the player, so it will boot into a blank screen, and when we use certain keys, video will start, pause or stop. Find the Code on GitHub/JuliusCode/mp4museum
Install Instructions
To set up the mp4museum video player on your Raspberry Pi, follow these steps:
- Connect to your Raspberry Pi:
- Use SSH to connect to your Raspberry Pi with the following command:
ssh pi@<your_pi_ip_address>
- Enter the password:
mp4museum
.
- Configure the Raspberry Pi:
- Run the Raspberry Pi configuration tool:
sudo raspi-config
- Navigate to Performance Options and disable the overlay filesystem. After making changes, reboot your Raspberry Pi.
- Install Required Packages:
- Install the
keyboard
library using pip:sudo pip3 install keyboard
- Set Up the Script and Videos:
- Place this Python script and your video files in the
/home/pi
directory.
- Edit the .bashrc File:
- Open the
.bashrc
file for editing:nano /home/pi/.bashrc
- Update the lines at the end of the file to set up autostart:
# mp4museum autostart
setterm -cursor off
tput setaf 0
clear
sudo python3 /home/pi/mp4m-keyboard.py > /tmp/mp4museum.log 2>&1
setterm -cursor on
tput setaf 7
The script will have to run as the superuser “root” so it can use the keyboard library. This might be handled different on other systems.
- Save and exit the editor (Ctrl + X, then Y, then Enter).
- Reboot the Raspberry Pi:
- Finally, reboot your Raspberry Pi:
sudo reboot
- Control Playback:
- After rebooting, you can control video playback using the following keys:
- Press
ESC
to stop playback. - Press
SPACE
to toggle pause/play. - Press
A
,S
,D
, etc., to play corresponding videos. - Press
Q
to exit the program.
- Press
How to Add Videos to the Script Array
To add more videos to the script, you need to modify the key_map
dictionary in the code. Each key in the dictionary corresponds to a keyboard key that will trigger the playback of a specific video file.
Example:
To add a new video file named video4.mp4
that plays when the f
key is pressed, you would update the key_map
like this:
key_map = {
'a': 'video1.mp4',
's': 'video2.mp4',
'd': 'video3.mp4',
'f': 'video4.mp4', # New video added
# Add more key-video mappings as needed
}
Make sure that the video files are located in the same directory as the script (/home/pi
).
Code Breakdown
1. Importing Libraries
import time
import vlc
import os
import keyboard
- time: Used for adding delays in the code.
- vlc: A library to control VLC media player for video playback.
- os: Used for interacting with the operating system (e.g., checking file existence).
- keyboard: A library to listen for keyboard events.
2. Audio Device Configuration
audiodevice = "0"
if os.path.isfile('/boot/alsa.txt'):
with open('/boot/alsa.txt', 'r') as f:
audiodevice = f.read(1)
- This segment checks if the ALSA configuration file exists and reads the audio device configuration from it. The default audio device is set to “0”.
3. Media Playback Function
def vlc_play(source):
if ".loop." in source:
vlc_instance = vlc.Instance('--input-repeat=999999999 -q -A alsa --alsa-audio-device hw:' + audiodevice)
else:
vlc_instance = vlc.Instance('-q -A alsa --alsa-audio-device hw:' + audiodevice)
global player
player = vlc_instance.media_player_new()
media = vlc_instance.media_new(source)
player.set_media(media)
player.play()
time.sleep(1)
while player.get_state() in (vlc.State.Playing, vlc.State.Paused):
handle_key_event(None)
time.sleep(0.1)
media.release()
player.release()
- This function initializes a VLC media player instance and plays the specified video file. It also checks if the video should loop.
4. Key Event Handling Function
def handle_key_event(event):
global player
if keyboard.is_pressed('esc'):
if player.is_playing() or player.is_paused():
player.stop()
elif keyboard.is_pressed('space'):
if player.is_playing():
player.pause()
else:
player.play()
print("Playback resumed.")
elif keyboard.is_pressed('q'): # Use 'q' to exit the program
global running
running = False
else:
# Check if the key pressed is in the keys array
for key, video in key_map.items():
if keyboard.is_pressed(key):
vlc_play(video)
- This function listens for keyboard events and performs actions based on the key pressed:
- If
ESC
is pressed, it stops the video if it is currently playing or paused. - If
SPACE
is pressed, it toggles between play and pause. - If
Q
is pressed, it sets therunning
variable toFalse
, which will exit the main loop. - If any other key mapped in
key_map
is pressed (likeA
,S
,D
, etc.), it calls thevlc_play
function to play the corresponding video.
5. Key-Video Mapping
key_map = {
'a': 'video1.mp4',
's': 'video2.mp4',
'd': 'video3.mp4',
# Add more key-video mappings as needed
}
- This dictionary maps specific keyboard keys to video file names. When a key is pressed, the corresponding video file will be played.
6. Setting Up the Keyboard Listener
keyboard.on_press(handle_key_event)
- This line sets up a listener that calls the
handle_key_event
function whenever a key is pressed. This allows the program to respond to user input in real-time.
7. Main Loop
running = True
while running:
time.sleep(0.1) # Keep the loop running without consuming too much CPU
- This is the main loop of the program. It keeps running as long as the
running
variable isTrue
. Thetime.sleep(0.1)
call prevents the loop from consuming too much CPU by adding a small delay.
8. Cleanup on Exit
if player.is_playing():
player.stop()
print("Program exited.")
- When the main loop exits (when
running
is set toFalse
), this segment checks if the player is still playing and stops it if necessary. Finally, it prints a message indicating that the program has exited.
This documentation should provide a comprehensive overview of your Python script, including installation instructions, how to add videos, and a detailed breakdown of the code. If you have any further questions or need additional information, feel free to ask!