-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist.py
executable file
·53 lines (42 loc) · 1.52 KB
/
playlist.py
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
#!/usr/bin/python3
'''
AUTHOR: Reprise
DATE: 05.18.17
Version: 1.0.3
PURPOSE:
This program is meant to create .m3u entries to assist in the creation of playlists.
It is meant to be called from 'ytdl.sh', which will give it a path as an argument.
the path should be where the playlist is to be created.
'''
import os
import sys
import re
from mutagen.mp3 import MP3
FORMAT_DESCRIPTOR = "#EXTM3U"
FILE_MARKER = "#EXTINF"
PATH = sys.argv[1] # This should be given by ytdl.
filename = "DATA.m3u"
# Check for path in which to find files to make into a playlist.
if len(sys.argv) < 2:
print("Invalid path to directory.")
exit(0)
# Create, open, and set header for playlist file.
playlist = open(filename, 'w')
playlist.write(FORMAT_DESCRIPTOR + "\n\n")
for track in os.listdir(PATH):
path_track = PATH + "/" + track
# DATA.m3u will be in this directory, we don't want to process it.
if track != filename and track.endswith(".mp3"):
# Write entry as it should appear in playlist
playlist.write(FILE_MARKER + ":")
# Write RUNTIME
audio = MP3(path_track)
runtime = audio.info.length
playlist.write(str(runtime)[0:3])
playlist.write("," + track + "\n")
# write path.
playlist.write(path_track + "\n")
print("entry created: " + track)
playlist.close()
# TODO : Do not record paths from root dir. If this walks into sub-folders, start recording path from cwd.
# TODO : Find music in recursive directories (walk), and understand their path from argv[1] Dir.