0

This question is specific to iMac and python and VLC.

I've written a short python script to play some random video files using VLC, like a slide show but for video. When I press "n" on the keyboard, I expect the script to start playing the next file. Pressing "q" quits the program.

This works perfectly, the first time I press "n" or "q". However, subsequent presses of "n" have no effect.

Anyone know what I'm doing wrong? Here is my code:

#!/usr/bin/env python3

import os
import random
import subprocess
from pynput import keyboard
from threading import Thread

video_directory = '/Volumes/T7/test'
video_files = [f for f in os.listdir(video_directory) if f.endswith(('.mp4', '.avi', '.mkv', '.mov', '.flv', '.wmv'))]
vlc_path = '/Applications/VLC.app/Contents/MacOS/VLC'
video_count = 20
random_videos = random.sample(video_files, video_count)
video_paths = [os.path.join(video_directory, video) for video in random_videos]

vlc_process = subprocess.Popen([vlc_path, '--video-on-top', '--fullscreen', video_paths[0]])
current_video = 0

def on_press(key):
    global vlc_process
    global current_video
    if key == keyboard.KeyCode.from_char('n'):
        vlc_process.terminate()
        current_video += 1
        if current_video < video_count:
            vlc_process = subprocess.Popen([vlc_path, '--video-on-top', '--fullscreen', video_paths[current_video]])
    elif key == keyboard.KeyCode.from_char('q'):
        vlc_process.terminate()
        return False

keyboard_listener = keyboard.Listener(on_press=on_press)
keyboard_thread = Thread(target=keyboard_listener.start)
keyboard_thread.start()

vlc_process.wait()
keyboard_listener.stop()
Brajesh
  • 271
  • 1
  • 3
  • 11

0 Answers0