• info@robertoaiello.net

Python: Script to read and show random videos from your raspberry

Python: Script to read and show random videos from your raspberry

Hi everyone,

in this article, I write a minimal script in python to read and show random videos (mp4,etc…) from your raspberry . This script use omxplayer.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import random
import subprocess
#from subprocess import Popen
path = ‘my directory
files = os.listdir(path)
n = len(files)
print(‘Element found: ‘ + str(n))
for file in files:
print(file)
#os.system(‘omxplayer -b -o local “‘ + path + ‘/’ + files[rand] + ‘”‘)
#omxp = Popen([“omxplayer”,”-b”,”-o”,”local”, path + “/” + files[rand]])
while True:
rand=random.randint(0,n-1)
print(‘Element random select: ‘+ files[rand])
subprocess.call([“omxplayer”,”-b”,”-o”,”local”, path + “/” + files[rand]])
Please change “my directory” with your directory videos (es: /home/video/).
Save the script in file py (es: showVideos.py) and launch from terminal $ ./showVideos.py
In the second example send the directory from argument
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import random
import subprocess
import sys
#from subprocess import Popen
path = sys.argv[1]
files = os.listdir(path)
n = len(files)
print(‘Element found: ‘ + str(n))
for file in files:
print(file)
#os.system(‘omxplayer -b -o local “‘ + path + ‘/’ + files[rand] + ‘”‘)
#omxp = Popen([“omxplayer”,”-b”,”-o”,”local”, path + “/” + files[rand]])
while True:
rand=random.randint(0,n-1)
print(‘Element random select: ‘+ files[rand])
subprocess.call([“omxplayer”,”-b”,”-o”,”local”, path + “/” + files[rand]])
Save the script in file py (es: showVideos.py) and launch from terminal $ ./showVideos.py myDirectory
In short, I will upload the sources into the github
enjoy
robi.aiello@gmail.com

Leave a Comment