Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:05 08 Jul 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: 9590
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: 4301
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: 419
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: 9590
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: 4036
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: 9590
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!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9590
Posted: 02:58am 30 May 2025
Copy link to clipboard 
Print this post

Just following up on this a bit more, and I can't make it work.    

I'm back in W10 for this testing, as the PI5 idea does not seem to want to work with video playback, which is a bit of a shame, but I digress.





I do get a quick black window popping up, but then it vanishes again, no error message, no VLC running in the background or anything like that.

I threw a handful of videos into C:\BLAH folder, and tried to point VLC there using the example posted above, but no dice.

I can play any of the files in BLAH, by just double-clicking them as you would expect, and they open and play just fine with VLC, so there is something I am not doing right here.  
Smoke makes things work. When the smoke gets out, it stops!
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2601
Posted: 03:33am 30 May 2025
Copy link to clipboard 
Print this post

Tried it and also just a blink on the screen. Removed the -and-exit from the command and this error message remained.
"ERROR: VLC media player could not start
either the command line options were invalid or no plugins were found"
Edited 2025-05-30 13:36 by phil99
 
Quazee137

Guru

Joined: 07/08/2016
Location: United States
Posts: 593
Posted: 05:48am 30 May 2025
Copy link to clipboard 
Print this post

I open VLC and select "media" "Open Directory"
  at botton you have "toggle Play list" "toggle Loop" "Random"


  I have a lot of play list and pick one then loop all, random.

  Some movies some videos some music and a few mixed.

  VLC is only thing on my Destop #4 VLC is alway on in the background
  while Im on Desktop #1 minecraft or Desktop#2 Geany / adruino / MMBasic
  Desktop #3 old EagleCAD 7

  Sometime Desktop #4 Firefox "paramountplus" or "Therokuchannel" for TV viewing.


  Have FUN
  Quazee137
Edited 2025-05-30 15:49 by Quazee137
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9590
Posted: 05:48am 30 May 2025
Copy link to clipboard 
Print this post

Good to know it's not just me then!  

@ Quazee137: Yes, but I need to be able to do this from the command-prompt.  I cannot start VLC and mouse-around to do this, I need to be able to issue the command, so that I can then build scheduled tasks around it.  
Edited 2025-05-30 15:49 by Grogster
Smoke makes things work. When the smoke gets out, it stops!
 
Quazee137

Guru

Joined: 07/08/2016
Location: United States
Posts: 593
Posted: 06:11am 30 May 2025
Copy link to clipboard 
Print this post

Debian linux has a perl program called randomplay that plays *.mp3 and files in other audio formats from one or more specified directories. I've used it for years.
For example, in my top level music directory, the following plays a randomized list of songs from all given directories:

randomplay artist1 artist2 artist3

Since it is perl, I presume it will run on any linux system when the dependencies are installed. No idea if it works on other OSs.

EDIT Just re-read the question and it sounds like the person might just want to play one random song.
In that case, the --tracks=n argument is useful:

randomplay --tracks=1 directory


found at https://askubuntu.com/questions/888987/play-a-random-mp3-file

might be of use for you.
Quazee137
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9590
Posted: 07:15am 30 May 2025
Copy link to clipboard 
Print this post

YES, this is it EXACTLY.......BUT......needs to work with video files like MP4/VOB/AVI/M4V etc.

I will play with your idea and see if I have any success.
The whole play random thing, MIGHT be limited to audio files - who knows.
Smoke makes things work. When the smoke gets out, it stops!
 
Quazee137

Guru

Joined: 07/08/2016
Location: United States
Posts: 593
Posted: 08:39am 30 May 2025
Copy link to clipboard 
Print this post

Have a play with

shuf

 it may make it easier in command line or bash to do what you want.

 I'll play with it too and see LOL.

 Quazee137

I missed that Nimue showed it being used.

One last thing randomplay can play ogg. And ogg can be music or video files.

So do a test convert a few mp4 to ogg and try randomplay.

 There are online converters.

I do this for mp3 to ogg in making my minecraft music packs.
Edited 2025-05-30 19:12 by Quazee137
 
mozzie
Senior Member

Joined: 15/06/2020
Location: Australia
Posts: 132
Posted: 07:04pm 30 May 2025
Copy link to clipboard 
Print this post

G'day Grogster,
Not sure if this is stating the obvious, but "SHUF" doesn't exist in CMD AFAIK

MMbasic for DOS is awesome for this kind of thing, below is the outline of a jukebox program written many years ago, could you use it as the basis of something useful?


'Program to shuffle music/video files
'Run from MMbasic prompt

Option default integer
Print MM.CmdLine$

System "dir C:\Testing\*.mp4 /b > C:\testing\files.txt"

Randomize Timer
Open "c:\testing\files.txt" For input As #1

A=0
Do While Eof(1)=0
   Line Input #1,B$
   A = A + 1
Loop

Close #1

A = A * Rnd

Open "c:\testing\files.txt" For input As #1

For C = 1 To A
   Line Input #1,B$
Next

d$="C:\progra~1\videolan\vlc\vlc.exe --play-and-exit --fullscreen c:\testing\"
System d$+b$
Print b$

End


Every time the program is run it plays a new video in VLC.
The original (if I still have it) wrote the name of the played tracks to another file and would check before playing to ensure no repeats.

Regards,
Lyle
 
Quazee137

Guru

Joined: 07/08/2016
Location: United States
Posts: 593
Posted: 07:11pm 30 May 2025
Copy link to clipboard 
Print this post

mozzie try shuf --help

 I have been using MMB4L as a batch bash thing so your idea looks FUN.

 Quazee137
Edited 2025-05-31 05:14 by Quazee137
 
mozzie
Senior Member

Joined: 15/06/2020
Location: Australia
Posts: 132
Posted: 07:30pm 30 May 2025
Copy link to clipboard 
Print this post

G'day Quazee,
I think we have our wires crossed, nothing unusual this end  

As far as I understand it, Grogster was playing in Linux but has gone back to WIN10 to test the VLC functionality, and I don't think the "SHUF" function exists in the Windows CMD prompt, its not on my WIN7 system anyway, possibly why it fails to open.

Its about time I got a Pi5 and had a crack at MMB4L myself, MMBASIC for DOS has been my goto for all sorts of wacky stuff for years and its Awesome  

Regards,
Lyle.
Edited 2025-05-31 05:31 by mozzie
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9590
Posted: 02:24am 31 May 2025
Copy link to clipboard 
Print this post

  mozzie said  As far as I understand it, Grogster was playing in Linux but has gone back to WIN10 to test the VLC functionality, and I don't think the "SHUF" function exists in the Windows CMD prompt, its not on my WIN7 system anyway, possibly why it fails to open.


Yes, that is 100% correct.
Sorry if I have caused any confusion on that.

ORIGINALLY I was going to us a PI5 and Raspian and VLC with CRON to do schedulled playback.  Seemed easy enough, and a chance for me to tinker with the latest Raspbian, PI5 board and booting from an SSD.  All that went REASONABLY painlessly, but then once I got it all up and running, found out that the PI5 board specifically, is actually inferior to the earlier boards when it comes to playing back video - something of a surprise to discover....

So, while I try to work out what I am going to do there, I figured I could still play around with the VLC command-line using VLC on my W10 machine, and that is where I am now.

So, I need ONLY commands you can use with VLC.  Currently, Linux command-line is on the back-burner, and I am playing in the Windows command-prompt.

I hope that clarifies the current situation.  I did find documentation on the VLC command-line commands, but the number of commands VLC can use is amazing.  It's a HUGE list, and my eyes started to glaze over at just how long it would take to read through them all to find what I want....    

Oh, and thanks for the MMB4D example - that might allow me to combine MMBASIC into the mix - that is a clever idea!  
Edited 2025-05-31 12:27 by Grogster
Smoke makes things work. When the smoke gets out, it stops!
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 419
Posted: 07:26am 31 May 2025
Copy link to clipboard 
Print this post

In my original Win11 example I also use CYGWIN  - which is a layer that adds Linux commands to Windows -- so shuf and others are useable in Windows.

My original example under Windows with CYGWIN should work as was.

N
Entropy is not what it used to be
 
Quazee137

Guru

Joined: 07/08/2016
Location: United States
Posts: 593
Posted: 06:32pm 31 May 2025
Copy link to clipboard 
Print this post

My Grandson uses this for his kids learning videos.
Random Play


He sets up a group of lessons and the kids do better when their are out of order
my great grandson gets bored with routines.


Hope this helps.
Quazee137
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6269
Posted: 03:21am 01 Jun 2025
Copy link to clipboard 
Print this post

I control VLC using the TCPIP interface.

Once VLC is running and listening to the chosen port,
I only need two commands, 'clear' and 'add'

PureBasic code extracted for my main program:
 VLC = "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe"
 VLCparameters = "--intf rc --rc-host localhost:4212"
 VLCport = 4212
 
 VLCprog = RunProgram(VLC,VLCparameters,"") ;  this starts VLC with a listening port open
Delay(1000)
VLClink = OpenNetworkConnection("localhost", VLCport, #PB_Network_TCP ) ; connect to VLC
Delay(200)
Result = SendNetworkString(VLClink, "clear"+#CRLF$ , #PB_Ascii ) ; clear playlist

; for each song:
  Result = SendNetworkString(VLClink, "clear"+#CRLF$ , #PB_Ascii )
   Delay(300)
   Result = SendNetworkString(VLClink, "add "+playlist(PlayPos)+#CRLF$ , #PB_Ascii ) ; where 'playlist(PlayPos)' is the chosen song
   Delay(300)

I also get the song playing time etc but that is not needed.

Jim
Edited 2025-06-01 13:22 by TassyJim
VK7JH
MMedit
 
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