2025 Dec. 30.
2025 Sep. 15.
play-video-randomly.sh
#!/bin/bash
# 2025 Dec. 30.
# 2025 Sep. 15.
# 2017 Apr. 15.
# 2017 Apr. 08.
# 2017 Feb. 25.
# Play an audio-video file randomly in the directory recursively specified in the argument.
# Usage : command DIR
commandpath="$0"
if [ $# -ne 1 ]; then
echo "${commandpath##*/} DIR" >&2
exit 1
fi
which vlc 1> /dev/null 2> /dev/null
Result=$?
if [ $Result -ne 0 ]; then
echo "Install VLC media player." >&2
exit 1
fi
which gawk 1> /dev/null 2> /dev/null
Result=$?
if [ $Result -ne 0 ]; then
echo "Install gawk." >&2
exit 1
fi
dir="$1"
if [ ! -d "${dir}" ]; then
echo "${dir} is not directory." >&2
echo "${commandpath##*/} DIR" >&2
exit 1
fi
if [ '/' != ${dir: -1} ]; then
dir="${dir}/"
fi
countFiles=`find "${dir}" -type f -name '*.mp4' -o -name '*.flv' -o -name '*.ts' -o -name '*.mpg' -o -name '*.mpeg' | wc -l`
numFile="$(( $RANDOM % $countFiles ))"
MediaFile=`find "${dir}" \( -type f -name '*.mp4' -o -name '*.flv' -o -name '*.ts' -o -name '*.mpg' -o -name '*.mpeg' \) -print0 | gawk -v target="$numFile" 'BEGIN { RS="\0" } NR==target { print; exit }'`
echo "$MediaFile"
vlc --no-fullscreen "$MediaFile"
exit 0