Page 1 of 1

play fixed video between random videos

Posted: Fri Sep 15, 2017 2:13 pm
by Warzon3
I'm currently using the following code wich continuously play any videos that are on a usb stick in a random order.

Code: Select all

#!/usr/bin/python
import os
import glob
from subprocess import Popen
import random

video = glob.glob(os.path.join('/media/usb/', '*.mp4'))

while(1):
	player = Popen(['omxplayer','-b',random.choice(video)])
	player.wait()
What I'd like to do is play a few random video's and play 1 fixed video after wich I start over agian and play random videos before playing another fixed video etc etc.
I can't just add another line as that opens another instance of omxplayer and results in no output at all.

Can someone point me in the right direction?

Re: play fixed video between random videos

Posted: Fri Sep 15, 2017 3:04 pm
by OutoftheBOTS
You can add a for loop to play a set number of random videos then play the fixed video something like this

Code: Select all

number_of_random_vids = 5 #set to the number pof randeom vids to play

while(1):
	for count in range(number_of_random_vids):
	         player = Popen(['omxplayer','-b',random.choice(video)])
	         player.wait()
	 
	player = Popen(['omxplayer','-b',set video)
	

Re: play fixed video between random videos

Posted: Fri Sep 15, 2017 3:55 pm
by Warzon3
Thanks for the suggestion I'll try it out.

I got it working for now using a bash script like the following

Code: Select all

#!/bin/sh
dir='/media/usb/'
file=`/bin/ls -1 "$dir" | sort --random-sort | head -1`
path=`readlink --canonicalize "$dir/$file"` # Converts to full path
echo "The randomly-selected file is: $path"

'omxplayer' '-b' $path

dir='/media/usb/'
file=`/bin/ls -1 "$dir" | sort --random-sort | head -1`
path=`readlink --canonicalize "$dir/$file"` # Converts to full path
echo "The randomly-selected file is: $path"

'omxplayer' '-b' $path

dir='/media/usb/fixvid/'
file=`/bin/ls -1 "$dir" | sort --random-sort | head -1`
path=`readlink --canonicalize "$dir/$file"` # Converts to full path
echo "The randomly-selected file is: $path"

'omxplayer' '-b' $path

bash videoplayer.sh
This basically plays 2 videos at random and then pulls the third video from another directory before firing off the same script again.

the problem with this is that it sometimes tries to run the system volume information file but it just throws error and goes onto the next file wich isn't to big of a problem

It's not the best code but for now it works :)