commit eb42e2bd254f2f7e9fc514039b43c2f58176f113 Author: Justin Lin Date: Thu Mar 14 13:16:52 2024 +0800 feat: use python-vlc to play a video diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..400635b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +.venv +*.swp diff --git a/omx.py b/omx.py new file mode 100644 index 0000000..2bf8842 --- /dev/null +++ b/omx.py @@ -0,0 +1,16 @@ +from omxplayer.player import OMXPlayer +import time + +v1 = '/home/pi/Videos/nicechord-original.mp4' +v2 = '/home/pi/Videos/new_trunk for Alice(290)_1.mp4' + +p1 = OMXPlayer(v1, args=['--display=2'], dbus_name='org.mpris.MediaPlayer2.omxplayer1') +p2 = OMXPlayer(v2, args=['--display=7'], dbus_name='org.mpris.MediaPlayer2.omxplayer2') + +try: + time.sleep(10) +except KeyboardInterrupt: + print('exit') +finally: + p1.quit() + p2.quit() diff --git a/player.py b/player.py new file mode 100644 index 0000000..c8242aa --- /dev/null +++ b/player.py @@ -0,0 +1,31 @@ +import vlc +import time + +# Path to your video file +video_path = "/home/wancat/mind-blowing.mp4" + +# Creating a VLC instance +player = vlc.Instance() + +# Creating a Media Player +media_player = player.media_player_new() + +# Creating a new Media +media = player.media_new(video_path) + +# Setting media to media player +media_player.set_media(media) + +# Play the media +media_player.play() + +# Wait for the video to play +time.sleep(10) # Waits for 5 seconds; adjust or use a different method to wait for the video to finish + +# Stop playing +media_player.stop() + +# Note: The time.sleep() here is just to prevent the script from ending immediately. +# For a real application, you'll need a more robust way to check if the video is still playing. +# This could be done by polling the media player's state or setting up event handlers. + diff --git a/relay.py b/relay.py new file mode 100644 index 0000000..15bf1bb --- /dev/null +++ b/relay.py @@ -0,0 +1,41 @@ +#! /usr/bin/python3 +import RPi.GPIO as GPIO +from pathlib import Path +from omxplayer.player import OMXPlayer +import time + +# Config those variables +v1 = Path('/home/pi/Videos/2.mp4') +v2 = Path('/home/pi/Videos/2.mp4') +PIN = 12 +SWITCH_AT = 9*60 + 16 +VOLUME = 1.5 + +def play(): + print('play') + try: + player = OMXPlayer(v1, args=['--display=2'], dbus_name='org.mpris.MediaPlayer2.omxplayer1') + p2 = OMXPlayer(v2, args=['--display=7'], dbus_name='org.mpris.MediaPlayer2.omxplayer2') + LENGTH = player.duration() + player.set_volume(VOLUME) + time.sleep(SWITCH_AT) + GPIO.output(PIN, GPIO.LOW) + time.sleep(LENGTH - SWITCH_AT) + return True + except KeyboardInterrupt: + return False + finally: + GPIO.output(PIN, GPIO.HIGH) + player.quit() + p2.quit() + +def main(): + while play(): + pass + GPIO.cleanup() + +if __name__ == '__main__': + GPIO.setmode(GPIO.BOARD) + #GPIO.setwarnings(False) + GPIO.setup(PIN, GPIO.OUT, initial=GPIO.HIGH) + main()