Help with video shuffler code

import argparse
import random
parser = argparse.ArgumentParser()

parser.add_argument('--shuffle', '-s', action='store_true', default=False)  
args = parser.parse_args()


#extract mp4 files from current folder
from glob import glob
files = glob('*.mp4')

if args.shuffle:
  random.shuffle(files)


#play each file in omxplayer:
import os
for f in files:
  try:
    os.system('omxplayer -o local "%s"' % f)
    os.system('killall omxplayer.bin')
  except KeyboardInterrupt:
    #exit on ctrl+c (does not work, as ctrl+c is captured by omxplayer...)
    exit()

I’m trying to make omxplayer just play 1 video out of the folder randomly and then stop but it keeps going through all of the videos in the folder I thought

os.system('killall omxplayer.bin') ''' 

would stop it but it doesn’t, Any help is greatly appreciated

os.system() runs the program “on the foreground”. This means that the python process waits (automatically in the os.system() function) until the program finishes.

I do not understand how you want to recognize that the first video finished. The code below will let omxplayer running for 20 seconds then it asks it to terminate. Termination may not work if omxplayer spawns its own sub-processes. Also if you need to use stdin, stdout or stderr of omxplayer you would need a little bit more complex code.

import subprocess
import time

for file in files:
    process_omxplayer = subprocess.Popen(
            ('omxplayer', '-o', 'local', file),
            stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    time.sleep(20)
    process_omxplayer.terminate()

If the process does not terminate on signal 15, you can try signal 9: process_omxplayer.kill().

You do not need to catch KeyboardInterrupt. This exception terminates a python program also if you do not handle it. When you have a sub-process on foreground ignoring SIGINT (Ctrl+c) then catching the exception will not help.

By Václav Brožík via Discussions on Python.org at 09Jun2022 20:32:

You do not need to catch KeyboardInterrupt. This exception terminates
a python program also if you do not handle it. When you have a
sub-process on foreground ignoring SIGINT (Ctrl+c) then catching the
exception will not help.

This isn’t entirely true. You could:

  • set pid=None
  • catch SIGINT and kill pid if not None
  • dispatch the external programme and not wait for it, record the pid in
    pid
  • uncatch SIGINT after the external programme returns

Untested, but seems ok. It won’t work if the external programme actually
disables the terminal setting causing ^C to send SIGINT, but that is
very rare - more likely the external programme itself simple catches and
ignores SIGINT.

The KeyboardInterrupt exception presumably simply does not fire during
os.system. Also untested. And not necessarily true, just based on the
OP’s code not working that way.

Cheers,
Cameron Simpson cs@cskk.id.au

Yea it needs to be the exact time of the video since the videos have different lengths, with I could just use a sort -r