โ† Back to home

Music Playlist creator by Genres

ยท

Python Script to filter offline music as per genre and create playlist

Outline

  1. It's a script that sorts the music files as per the genres given to it and make a playlist out of it, which can readily be opened with VLC, Poweramp, etc. offline music players in PC or Phones.

Problem

  1. I still love the days and the feel of owning the music than subscribing to Youtube music, spotify music, etc. where availibility of any song is at mercy of the Service.
  2. There is no filtering mechanism available in these online services. I can't filter songs which are Romantic+Sad+Retro.

Script

 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
from tinytag import TinyTag

file_list=os.listdir()
print(os.getcwd())

#SELECTING GENRES FROM USER
genres_list=["Romantic","Sad","Retro","Acoustic","Party","Ghazal","Rapping"]
genres_required=[]
counter=0
for genre_name in genres_list:
    counter+=1
    i=input(f"{counter} of {len(genres_list)}-{genre_name}? (y to select, else 'enter')")
    if(i=='y'):
        genres_required.append(genre_name)

print("---GENRES SELECTED = "+" - ".join(genres_required))

selected_list=[]
playlist_name="0_playlist_"+"_".join(genres_required)+".m3u"

if genres_required!=[]:
    for filename in file_list:
        print("Selected filename-"+filename)
        if '.m3u' in filename or '.py' in filename:
            continue
        if filename==playlist_name:
            print(filename)
            continue
        file_tinytag=TinyTag.get(filename)
        print(f"Genre: {file_tinytag.genre}",type(file_tinytag.genre),"\n")
        select_flag=1
        for i in genres_required:
            if file_tinytag.genre==None:
                print("Null genre")
                select_flag=0
                break
            if i in file_tinytag.genre:
                continue
            else:
                select_flag=0
        if select_flag==1:
            selected_list.append(filename)

print("\n-----------\n")
print("---SELECTED LIST---", selected_list)

if selected_list==[]:
    print("\nNo song matching the criteria!!")
    exit()
print("Now writing Playlist file-")
with open(playlist_name,mode="w") as playlistfile:
    contents=playlistfile.write("#EXTM3U\n#EXTM3U\n")

with open(playlist_name,mode="a",encoding="utf-8") as playlistfile:
    for name in selected_list:
        print(f"Writing {name} to playlist")
        contents=playlistfile.write(name+"\n")
This is the intentional end of this page.
โ† Previous Next โ†’