-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_playlist.py
More file actions
43 lines (34 loc) · 1.63 KB
/
Copy pathgenerate_playlist.py
File metadata and controls
43 lines (34 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import urllib.parse
def main():
music_dir = "mp3files"
playlist_file = "playlist.m3u"
extensions = (".mp3", ".wav", ".m4a", ".flac", ".ogg")
playlist_content = ["#EXTM3U\n"]
if os.path.exists(music_dir):
all_files = []
for root, dirs, files in os.walk(music_dir):
for file in files:
if file.lower().endswith(extensions):
full_path = os.path.join(root, file)
# Convert windows backslashes to forward slashes for URLs
relative_path = os.path.relpath(full_path, start=".").replace("\\", "/")
all_files.append(relative_path)
# Sort files alphabetically so they play in a predictable order
all_files.sort()
for path in all_files:
# Clean title (no extension, just filename)
filename = os.path.basename(path)
title = os.path.splitext(filename)[0]
# Use the raw path with literal spaces and special characters.
# This allows VLC to open the files locally (offline) from disk,
# while VLC's HTTP/HTTPS network client will automatically encode URLs when streaming.
playlist_content.append(f"#EXTINF:-1,{title}\n")
playlist_content.append(f"{path}\n")
# Write the playlist.m3u file with UTF-8 encoding
with open(playlist_file, "w", encoding="utf-8") as f:
f.writelines(playlist_content)
songs_count = len(playlist_content) // 2
print(f"Generated {playlist_file} successfully with {songs_count} song(s).")
if __name__ == "__main__":
main()