KBKarma
KBKarma

Reputation: 132

How to play a shuffled list of videos in omxplayer

To start: I'm a novice with Python and bash - I did some bashscripting many years ago, and I've only ever dabbled in Python. And I think I'm slightly out of my depth.

What I have

What I want

I'm trying to set up something for my grandmother - she plugs in the Pi, it boots, and begins playing the videos in a folder on the external drive in a random order in a loop. I want it to be as simple as possible, as she's in her late 80s, not very technical, and lives 9,211km from me. As a result, any changes will have to be made by the more techy family members over there, who are not familiar with Linux or Python at all.

What I've got

I've got the fstab set up:

UUID="XXXXXXX" /media/videoDrive auto nosuid,nodev,nofail 0 0

That works grand.

I tried to play the videos via the command line in VLC, as it has support for playing files in a directory shuffled in a loop. And it worked... sort of. The videos were incredibly choppy the larger the video player was, and basically didn't play at full screen. I found out that omxplayer would play mp4s fine - but some of the videos were webm, which it refused to play. I converted those, though, so that's OK.

I found a bashscript online, which was as follows:

#!/bin/bash

setterm -cursor off
setterm -clear

for file in $1/*
do
 omxplayer "$file" > /dev/null
done

setterm -cursor on

Using that, I wrote the following Python script:

#Autoplay Script
import os, random

#playlist path
path = "/media/videoDrive/vids"

videoList = os.listdir(path)
random.shuffle(videoList)
for video in videoList:
    target = os.path.join(path, video)
    os.system('omxplayer "{}" > /dev/null'.format(target))

All I need to do is look up what file to add the script call to (I think it's initrc), then write instructions, package it up, and hand it over to my parents to take over there.

... Once I've got the playback issue sorted.

What the problem is

Anyone with any Python knowledge may have figured out that the Python script will run until every video has been played (I only just realised it wouldn't even loop - dang). Which means that shutting down the Pi becomes quite difficult - you need to kill the Python process, then kill all omxplayer processes, and can only do that when the video isn't fullscreen, which happens with a few of them (I'd have fixed it when I first noticed the issue, but then I'd have to pull the plug on the Pi, which might not agree with the external).

I'd like to have some way to run the player, playing videos at random for eternity until some sort of input happens, which kills the loop and omxplayer. Alternatively, if anyone can figure out how to get VLC to play these videos without issue, that'll be perfect.

Upvotes: 0

Views: 3345

Answers (1)

Basil Eric Rabi
Basil Eric Rabi

Reputation: 343

I created a "play all random" script for my daughter using R:

#!/usr/bin/Rscript
setwd("/home/pi/Desktop/MyDir")

fl <- list.files(".")
fl <- sample(x = fl, size = length(fl))
len <- length(fl)
i <- 1L
while(i <= len) {
  tmpCMD <- paste("omxplayer -b \"", fl[i], "\"", sep = "")
  cat(tmpCMD,"\n")
  tmpLog <- system(command = tmpCMD)
  if (tmpLog == 2)
    break
  i <- i + 1L
}

And using the script, I also created a .desktop file with icon so I can make it clickable in the application launch bar:

[Desktop Entry]
Name=PlayAll
GenericName=Play All
Exec=/home/pi/bin/PlayAll.R
Icon=/home/pi/Pictures/PlayAll.png
Terminal=true
Type=Application
Categories=AudioVideo;Video;Audio;Music;
Comment=Video Playlist

Like omxplayer, you can press q to play the next video and CTRL+c to stop playing.

Upvotes: 0

Related Questions