Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:28 02 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Play a random file from the command-line...

Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9458
Posted: 12:53pm 24 Apr 2025
Copy link to clipboard 
Print this post

Is there any media-player that can take a command-line input, and play ONE randomly selected video file from a folder that you point to, play it, and then stop?

Have been trying to work out how to do this with ANY media-player, but I have not found anything that works.

Happy to look at ANY mediaplayer software - VLC, MS MediaPlayer, KODI, ZOOM player....anything that can accept a command-line.

I found the Google AI that said you can do this with VLC on the command line using:

vlc --random --fullscreen --video-on-top <directory\*.mp4>


or...

vlc \directory\*.mp4 --random --fullscreen --video-on-top


...tried variatons of both, but VLC moans that it cannot find or open the location, even though I am 100% sure the path and command is as one of the above.

Anyone got any pointers?

Perhaps another mediaplayer application that I am not aware of.

It would be nice to use a Raspberry Pi 5 for this task, along with CRONTAB, which I have been studying tonight, and it is really easy to setup to run VLC playlists, but rather then a pre-defined playlist, I just want VLC(or whatever) to just pick a file at random and play it then stop.

Simple enough concept, but do you think I can find any mediaplayer that can do that?
Smoke makes things work. When the smoke gets out, it stops!
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4244
Posted: 01:06pm 24 Apr 2025
Copy link to clipboard 
Print this post

Courtesy of AI this script will select a random file in a directory, you should be able to build what you want from it:


#!/bin/bash

# Check if a directory path is provided
if [ $# -eq 0 ]; then
   echo "Please provide a directory path."
   exit 1
fi

# Store the directory path
dir_path="$1"

# Check if the provided path is a directory
if [ ! -d "$dir_path" ]; then
   echo "Error: '$dir_path' is not a valid directory."
   exit 1
fi

# Get a list of files in the directory (excluding subdirectories)
mapfile -d $'\0' files < <(find "$dir_path" -maxdepth 1 -type f -printf "%f\0")

# Check if there are any files in the directory
if [ ${#files[@]} -eq 0 ]; then
   echo "No files found in the directory."
   exit 0
fi

# Get a random index
random_index=$((RANDOM % ${#files[@]}))

# Print the randomly selected file name
echo "${files[random_index]}


Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 369
Posted: 02:37pm 24 Apr 2025
Copy link to clipboard 
Print this post

Hi

CYGWIN user - but the bash shell should work the same.

Tested for .mp3, but should work the same for .mp4


vlc --play-and-exit "$(find /path/to/files -type f | shuf -n 1)"


So in my case, where I executed vlc from the same directory as 1000 mp3's this:

vlc --play-and-exit "$(find -type f | shuf -n 1)"


selects one at random, plays it and exits vlc.

How it works:

find /path - type f  - lists all the files
| pipes this into shuf (shuffle)
shuf -n 1  - outputs 1 file only

All of this passes into vlc one and only one file to play.

Interestingly, I use this with at present with "-I dummy --dummy-quiet" also in vlc to play a random mp3 without loading any of the vlc gui >>

vlc -I dummy --dummy-quiet --play-and-exit "$(find -type f | shuf -n 1)"


As said, tested and currently working in CYGWIN/W11 - should work under any bash shell.
Entropy is not what it used to be
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9458
Posted: 06:26am 25 Apr 2025
Copy link to clipboard 
Print this post

Excellent, thanks, I will try that out, but sounds like what I need!
If I can make it do it at the terminal, then I can copy that into CRONTAB and schedule the playback - which is the ultimate goal.  I'll keep the thread updated.
Smoke makes things work. When the smoke gets out, it stops!
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3988
Posted: 07:03pm 25 Apr 2025
Copy link to clipboard 
Print this post

From crontab it may not work without some tweaks, as the process(es) will be started without a tty/console.

But try it and see.

John
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9458
Posted: 03:58am 27 Apr 2025
Copy link to clipboard 
Print this post

Here is the video I was looking at that starts VLC from crontab, but I have looked at others and read more about it.

Crontab...

Skip to time-index 8:10, if the video does not take you right there.
Seems simple enough....

In this example, he's calling up playlists, but I just want to play ONE random file, using Crontab to schedule it, so that is what I am working towards.  My Pi5 is on-order.  I have not had a chance to play with a Pi5 for ages, so this seems like a good an excuse as any.
Smoke makes things work. When the smoke gets out, it stops!
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025