Last active
November 28, 2023 22:50
-
-
Save fulcrum6378/73890701587ad6df44dc1ed520be5e19 to your computer and use it in GitHub Desktop.
Windows Media Player M3U8 Lister - recursively lists all audio/video files for the new Windows Media Player in M3U8 files, starting from the current work directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mimetypes | |
import os | |
out_dir = 'Playlists' | |
if not os.path.isdir(out_dir): os.mkdir(out_dir) | |
def analyse_dir(root: str): | |
dir_name = os.path.basename(root) | |
m3u8_path = os.path.join(out_dir, dir_name + '.m3u8') | |
ignore: bool = False | |
got_sth: bool = False | |
if os.path.isfile(m3u8_path): | |
res = input('Rewrite \"' + m3u8_path + '\"? (y | n(ignore) | <new_name>) ') | |
if res == 'n': | |
ignore = True | |
elif res != 'y': | |
m3u8_path = os.path.join(out_dir, res + '.m3u8') | |
if not ignore: | |
m3u8 = open(m3u8_path, 'w', encoding='utf-8') | |
m3u8.write('#EXTM3U\n') | |
m3u8.write('#' + dir_name + '\n') | |
for f in os.listdir(root): | |
file = os.path.join(root, f) | |
if os.path.isdir(file): | |
analyse_dir(file) | |
continue | |
elif ignore: | |
continue | |
guess = mimetypes.guess_type(file)[0] | |
if guess is None or (not guess.startswith('audio') and not guess.startswith('video')): continue | |
# noinspection PyUnboundLocalVariable | |
m3u8.write(os.path.abspath(file) + '\n') | |
got_sth = True | |
if not ignore: | |
m3u8.close() | |
if not got_sth: | |
os.remove(m3u8_path) | |
analyse_dir(os.getcwd()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment