Wanna Play a Random Movie? No Problem!
I’m back again with a short script that I just figured out using a few Python libraries including “subprocess”, “random”, and “glob”. It’s been a while since I’ve delved into Python, and with no surprise to me, I forgot most everything I knew about the language. Again. The only thing I haven’t forgotten is my ability to Google-fu anything I need to find, so figuring it out and refreshing my brain on something so simple wasn’t too hard to do. It works by using the glob library to grab all of the files in my Movies folder, as well as all of the subdirectories for things like movie series, etc., find the different file types associated with video files (.avi, .mp4, .mkv), using the random library to make a random selection from the acrued list, and the subprocess library to run the bash command to open VLC and play the random selection.
Moving forward I plan to expand the script by adding more functionality by being able to make a playlist of a certain amount of movies, and also be able to see if the movie is part of a series (based on it’s parent folder) so it can either play the first one or add all of the related movies to the playlist. The script can also be modified to show music, pdf, really any kind of file you are looking for, and open it, or use that information to do any number of imaginable things.
import glob
import random
import subprocessproject_files = glob.glob(‘/media/rawrface//My Passport/Movies/**/*.mp4’) + glob.glob(‘/media/rawrface//My Passport/Movies/**/*.avi’) + glob.glob(‘/media/rawrface//My Passport/**/*.mkv’)
play = random.choice(project_files)
subprocess.run([“vlc”, play, “–fullscreen”])
#in order to use glob.glob() to see things in directories and sub directories you do “directory/**/*.extension”
#in subprocess.run the bash command goes in “”, and after a comma you can run a python variable
