I know it's a little bit off-topic but I can offer the following bash script. I use it on my Raspberry Pi and it works quite well.
#!/bin/sh
while [ true ];
do
searchresult=""
while [ -z "$searchresult" ]; do
rand=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-5};echo)
echo "$rand"
searchresult=$(googler -C -n 100 --np -x -w https://www.youtube.com "$rand")
done
urls=$(echo "$searchresult" | grep -o "https://www.youtube.com/watch?v=...........")
url=$(shuf -e -n 1 $urls)
echo "$url"
omxplayer -o hdmi $(youtube-dl -f mp4 -g "$url")
done
Prerequisites are that you have installed googler and youtube-dl.
My initial idea was to generate random 11 character strings and to append them to the base URL for YouTube videos. However, the number of possible permutations is enormous (26 capital letters + 26 small letters + 10 digits + hyphen + underscore = 64 characters; 64^11=7.38x10^19 permutations) and will be used up if every person on the Earth uploads 10 billion videos. That's why randomly generating the complete 11-character identifier is not a practicable approach. My script generates random 5-character strings instead (variable "rand") and passes them as a search query to googler. Googler searches the YouTube site for these random strings and in most cases it returns results (variable "searchresult"). In case there is no result, the search is repeated with another random string and so on till success. Usually it takes one to three tries. I tried with different lengths of the search string - 4 is maybe not unique enough and 6 is too long and it can take a lot of tries to get a result and I observed that if the script sends more than 10 search requests in short time, Google temporarily blocks me from further searches.
In the next step, the script extracts the hyperlinks to the YouTube videos from the search results and puts them in the variable "urls". Then one of them is randomly selected to be played (variable "url") and passed to the player - omxplayer in my case, but you can replace it with whichever player you want. Omxplayer is nice on the Raspberry Pi because it uses the hardware acceleration and outputs via hdmi directly to my TV set. If I change "-o hdmi" to "-o local" the audio is sent via the 3.5 mm stereo jack to an external amplifier. Pressing "q" during play stops playing the current video and the next random one starts automatically. It will go on forever till you press Ctrl-C to stop the script.
Additional tips
With some modifications you can get random videos on a given topic. For example, if we put "Deep Purple" as an additional search term we'll get random music videos by Deep Purple:
#!/bin/sh
while [ true ];
do
searchresult=""
while [ -z "$searchresult" ]; do
rand=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-2};echo)
echo "$rand"
searchresult=$(googler -C -n 10 --np -x -w https://www.youtube.com "$rand" "Deep Purple")
done
urls=$(echo "$searchresult" | grep -o "https://www.youtube.com/watch?v=...........")
url=$(shuf -e -n 1 $urls)
echo "$url"
omxplayer -o hdmi $(youtube-dl -f mp4 -g "$url")
done
Note that in the last example I have reduced the randomness factor to a 2-character string because it will be difficult to find a match for a 5-character string in the relatively small subset of all YouTube videos that contain the search term "Deep Purple". Also here I have limited the number of returned search results by googler to 10 to keep them relevant. With 100 I would also get less relevant results like videos of amateurs trying to play songs by Deep Purple.
The next script will play randomly only new videos that have been uploaded within the last 12 hours:
#!/bin/sh
while [ true ];
do
searchresult=$(googler -C -n 100 --np -t h12 -x -w https://www.youtube.com "")
urls=$(echo "$searchresult" | grep -o "https://www.youtube.com/watch?v=...........")
url=$(shuf -e -n 1 $urls)
echo "$url"
omxplayer -o hdmi $(youtube-dl -f mp4 -g "$url")
done
You can set different time constraints. See the documentation of googler for more details.