I need a command that plays a random mp3 from a directory. So far I have tried
ls *.mp3 | shuf -n 1 | omxplayer
Every different player just acts like it has not received a filename and spits out the help. Thanks for the help!
command linemp3
I need a command that plays a random mp3 from a directory. So far I have tried
ls *.mp3 | shuf -n 1 | omxplayer
Every different player just acts like it has not received a filename and spits out the help. Thanks for the help!
Best Answer
Firstly, I dislike Bash. Piping paths to processes isn't so nice, and causes all sorts of weirdness when not done 'just so'. That being said, many of the things that you are trying to do in Bash that are not kind with working or operating can be done with (unfortunately) more code, but can work the way you want it to, in other languages.
So, being somewhat bored and interested in creating something for this, I went and wrote a (very rough) Python script that can do what you're looking for. It may look complex, but it works pretty well, and I've put comments wherever or explained this below.
NOTE: I have only tested this with VLC player and Rhythmbox on my system, and with
xdg-open
which opens the GUI's default handler for the given file. In my case, VLC is the default thatxdg-open
calls. If you are on a GUI and just want to use the default Media player for the MP3 file, usexdg-open
for "player".Package Requirements On Your System:
python
(Python 2.6 or higher, but not Python 3)python-dev
(Python libraries of importance)Script Installation Process:
Not really much work here. But to make it simpler, follow these steps:
bin
folder in your home directory:mkdir /home/$USER/bin
cd /home/$USER/bin
randommp3
. Copy-and-paste the code from the "Code/Script" section below into this file with a text editor. Save said file.chmod +x /home/$USER/bin/randommp3
oxmplayer
would be what you would put in place ofplayer
when you execute the file./home/$USER/Music
(where$USER
is the currently logged in user), then you also have to specify the full directory path with the--dir
argument (or one of its aliases as explained in the "usage" section below). If the folder path contains any spaces at all, you must wrap it in single quotes (for example, for the "My Music" directory in a given path, you would enter it as/path/to/My Music
to the--dir
argument).Example execution:
Open a random MP3 file from the user's Music folder in their Home directory, in the GUI VLC Player
Open a random MP3 file from an external drive called "MusicDrive" mounted at
Music Drive
in the/media
folder, in the media player calledoxmplayer
Usage
Code: (or a link that you can save if you are really that lazy)