Files
vlchan/example.py
2025-10-09 16:31:57 +11:00

121 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""Example usage of VLChan video player with synchan synchronization."""
import time
import sys
from vlchan import VLChanPlayer
def main():
"""Example demonstrating VLChan usage."""
# Check command line arguments
if len(sys.argv) < 2:
print("Usage: python example.py <video_path> [synchan_url]")
print("Example: python example.py /path/to/video.mp4 http://localhost:3000")
sys.exit(1)
video_path = sys.argv[1]
synchan_url = sys.argv[2] if len(sys.argv) > 2 else "http://localhost:3000"
print(f"VLChan Example")
print(f"Video: {video_path}")
print(f"Synchan: {synchan_url}")
print("-" * 40)
# Create player
player = VLChanPlayer(synchan_url, video_path)
# Set up state change callback
def on_state_change(state):
print(f"Synchan update: playing={state.playing}, "
f"time={state.currentTime:.2f}s, "
f"duration={state.duration:.2f}s")
player.on_state_change = on_state_change
try:
# Start synchronization
print("Starting synchan synchronization...")
player.start_sync()
# Wait a moment for connection
time.sleep(1)
# Start playback
print("Starting video playback...")
player.play()
# Display controls
print("\nControls:")
print("- Press 'p' to pause/play")
print("- Press 's' to stop")
print("- Press 'f' to toggle fullscreen")
print("- Press 'v' to change volume")
print("- Press 'q' to quit")
print("- Press 'j' to seek forward 10s")
print("- Press 'k' to seek backward 10s")
print("\nPress 'q' to quit...")
# Interactive control loop
while True:
try:
# Check for keyboard input (non-blocking)
import select
import tty
import termios
if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
key = sys.stdin.read(1)
if key == 'q':
break
elif key == 'p':
if player.is_playing():
player.pause()
else:
player.play()
elif key == 's':
player.stop()
elif key == 'f':
player.set_fullscreen(not player.player.get_fullscreen())
elif key == 'v':
current_vol = player.get_volume()
new_vol = min(100, current_vol + 10)
player.set_volume(new_vol)
print(f"Volume: {new_vol}")
elif key == 'j':
current_pos = player.get_position()
player.seek(current_pos + 10)
print(f"Seeked forward to {current_pos + 10:.1f}s")
elif key == 'k':
current_pos = player.get_position()
player.seek(max(0, current_pos - 10))
print(f"Seeked backward to {max(0, current_pos - 10):.1f}s")
# Display current status
if player.is_playing():
pos = player.get_position()
dur = player.get_duration()
print(f"\rPlaying: {pos:.1f}s / {dur:.1f}s", end="", flush=True)
time.sleep(0.1)
except KeyboardInterrupt:
break
except Exception as e:
print(f"\nError: {e}")
break
except Exception as e:
print(f"Error: {e}")
finally:
print("\nCleaning up...")
player.cleanup()
print("Done!")
if __name__ == "__main__":
main()