Skip to content

Instantly share code, notes, and snippets.

@sengiv
Created April 18, 2024 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sengiv/1fb06983ca42da14a06f35b197e8c412 to your computer and use it in GitHub Desktop.
Save sengiv/1fb06983ca42da14a06f35b197e8c412 to your computer and use it in GitHub Desktop.
Randomly selects and plays shows from multiple local file location. Nice for rewatching same series shows, but from random points.
import os
import random
import subprocess
import json
def get_mp4_files(paths, depth=1):
mp4_files = []
for path in paths:
if depth < 0:
return mp4_files
for root, dirs, files in os.walk(path):
if root.count(os.sep) - path.count(os.sep) < depth:
for file in files:
if file.endswith('.mkv'):
mp4_files.append(os.path.join(root, file))
return mp4_files
def play_random_file(files, history_file):
with open(history_file, 'r') as f:
history = json.load(f)
file_to_play = random.choice([file for file in files if file not in history])
history.append(file_to_play)
with open(history_file, 'w') as f:
json.dump(history, f)
vlc_path = 'C:\\Program Files\\VideoLAN\\VLC\\vlc.exe' # replace with your VLC path if different
subprocess.call([vlc_path, file_to_play])
def main():
paths_to_scan = ['C:\\Users\\ASUS\\Downloads\\How.I.Met.Your.Mother.COMPLETE.SERIES.720p.2CH.x265.[HashMiner]',
'D:\\The Simpsons',
'D:\Friends.Complete.Series.720p.BluRay.2CH.x265.HEVC-PSA'
] # replace with your folder paths
depth = 2
history_file = 'history.json'
if not os.path.exists(history_file):
with open(history_file, 'w') as f:
json.dump([], f)
mp4_files = get_mp4_files(paths_to_scan, depth)
if mp4_files:
play_random_file(mp4_files, history_file)
else:
print('No mp4 files found in the specified directories.')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment