Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
for i in ./*; do
mplayer -ao alsa9 -vo xv -fs "${i}"
done
is there any way to randomise the "i" that is issued to mplayer? basically I want it to play all files in current dir in random order, mplayer has no randomiser. and if it did it closes after second file is played when issued several from a programming error.
generates random numbers in bash. it looks like you can use that first loop to put all the files in an array and then generate the random number in the second loop (with the mplayer command) and mod it with the total files in that folder. kinda like how one may do it in c/c++. i think bash uses % for the modulus operator (just like in c/c++)
hey thanx. sorry if it seems like I am trying to get someone to do it for me :-P
btw, are you a big shell scripter.. possibly with a little free time here and there (nothing big)?
if so I am looking for peoplke to help me develop my project (already quite far it is more than started) the url is at in my stuff under my msg.
maybe to 2 shell scripts. one that generates a playlist and the other one that takes in the playlist and plays them in random order. the first on is easy. something like:
Code:
#!/bin/bash
# accept a passed argument for naming the playlist file
# ie. ./gen_playlist $HOME/vids/playlist.txt
if [ -n "$1" ]
then
# here use the directory path to your playlist
ls $HOME/vids > "$1"
else
# use generic path to playlist and playlist name if none given
# this generates playlist in user's home directory
ls $HOME/vids > $HOME/playlist.txt
fi
exit 0
lemme work on the second part.
edit: nevermind this. it doesn't make any sense to make 2 shell scripts.
well, this shows how much i suck. the random number generation isn't really being all that random. it plays randomly but always in the same random order. i'm too tired to keep trying to figure this out. but here's a script. it works, but it's not bullet proof. this format is ./script_name path_to_video_media - the argument should be a directory. this doesn't check if the files in the directory are actual media files or any other checks what-so-ever. this one uses xine, but i guess you can sub that xine command with the mplayer command you want to use.
Code:
#!/bin/bash
# seed random number generator
RANDOM=$$$(date '+%s')
#declare arrays to be used
declare -a f_name_array
declare -a played_already
# get the files in your video directory
if [ -n "$1" ]
then
# user specified path to video media.
ls "$1" > "$1/playlist.txt"
# initialize generic counter
i=0
# populate array list
while read f_name
do
f_name_array[$i]="$f_name"
# inc counter
i=$(($i + 1))
done < "$1/playlist.txt"
# get total count - array starts at 0 so total count is i + 1
f_count=$(($i+1))
# play the list in random order
# initialize generic counter
i=0
while [ "$i" -lt "$f_count" ]
do
# get random index
index=$[ ( $RANDOM % $f_count ) ]
# check if index has been played already
has_played=0
for pl_index in "${played_already[@]}"
do
if [ "$pl_index" -eq "$index" ]
then
has_played=1
break
fi
done
# determine action from playlist comparison
if [ "$has_played" -eq 0 ]
then
# add the index to the played list
played_already[$i]="$index"
# play the file
xine "$1/${f_name_array[$index]}"
# inc. counter
i=$(($i+1))
fi
done
# debug crap
#echo "playlist: "
#for file in "${f_name_array[@]}"
#do
# echo "$file"
#done
#echo
#echo
#echo "played already: "
#for file in "${played_already[@]}"
#do
# echo "$file"
#done
else
echo "Did not pass path to video media argument"
fi
rm -f "$1/playlist.txt"
exit 0
this should work for you, but i'm by no means a bash scripting expert or even intermediate in bash experience. i guess the usual disclaimer of use at your own risk, blah, blah, blah...
while [ ${digits} -ge 2 ]; do
let "thenumbermin=((${thenumbermin})*10)"
let "thenumbermax=(9+((${thenumbermax})*10))"
let "digits=(${digits} - 1)"
done
let "thenumbermax2=(thenumbermax - thenumbermin)"
# make all possible variables
nummax="${thenumbermax}"
nummin="${thenumbermin}"
while (("${nummin}" <= "${nummax}")) ; do
alist[${nummin}]="nothing"
let "nummin=((${nummin})+1)"
done
for i in "${1}"/*; do
looptry()
{
anumber=$((RANDOM%thenumbermax2+thenumbermin))
if [ "${alist[${anumber}]}" == "nothing" ]; then
alist[${anumber}]="${i}"
echo "${anumber} ${alist[${anumber}]}"
else
looptry
fi
}
looptry
done
echo out
thenumberminx="${thenumbermin}"
#while (("${thenumberminx}" <= "${thenumbermax}")) ; do
# tester=`echo "${alist[${thenumbermin}]}"`
# if [ ! "${tester}" == "nothing" ]; then
# echo "${alist[${thenumbermin}]}" >> ~/testlist.txt
# fi
# let "thenumberminx=((${thenumbermin})+1)"
#done
thenumbermin="${thenumbermin}"
while (("${thenumbermin}" <= "${thenumbermax}")) ; do
tester=`echo "${alist[${thenumbermin}]}"`
if [ ! "${tester}" == "nothing" ]; then
`mplayer -fs -ao alsa9 -vo xv "${alist[${thenumbermin}]}"`
clear
echo ""
echo "press control+c to quit"
sleep 1s
if [ "$exiting" == "q" ]; then
exit 0
fi
fi
let "thenumbermin=((${thenumbermin})+1)"
done
exit 0
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.