-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen_vlc_playlist.py
More file actions
181 lines (147 loc) · 5.98 KB
/
Copy pathgen_vlc_playlist.py
File metadata and controls
181 lines (147 loc) · 5.98 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import xml.etree.ElementTree as xml
import os
import pprint
#import inspect
#from itertools import groupby
import re
# **********************************************************************************
# Settings
ext_list = ['.mp4', '.mkv', '.avi', '.flv', '.mov', '.wmv', '.vob', '.mpg','.3gp', '.m4v', '.ts'] #List of extensions to be checked.
#
check_subdirectories = True #False #Set false to get files only from cwd.
check_sort_file_prefix4 = True # sort files with prefix ('1. file', `10. file', '100. file')
pat_sort_file_prefix4 = r"\/\d+\." # regex pattern
check_playlist_web = True
addr_playlist_web = 'http://192.168.1.1:8080'
# **********************************************************************************
class Playlist:
"""Build xml playlist."""
def __init__(self):
#Defines basic tree structure.
self.playlist = xml.Element('playlist')
self.tree = xml.ElementTree(self.playlist)
self.playlist.set('xmlns','http://xspf.org/ns/0/')
self.playlist.set('xmlns:vlc','http://www.videolan.org/vlc/playlist/ns/0/')
self.playlist.set('version', '1')
self.title = xml.Element('title')
self.playlist.append(self.title)
self.title.text = 'Playlist'
self.trackList = xml.Element('trackList')
self.playlist.append(self.trackList)
def add_track(self, path):
#Add tracks to xml tree (within trackList).
track = xml.Element('track')
location = xml.Element('location')
location.text = path
track.append(location)
self.trackList.append(track)
def get_playlist(self):
#Return complete playlist with tracks.
return self.playlist
class Videos:
"""Manage files (videos) to be added to the playlist."""
def __init__(self):
pass
def remove_nonvideo_files(self,file_list):
#Removes files whose extension is not mentioned in ext_list from list of files.
for index,file_name in enumerate(file_list[:]):
#if file_name.endswith(tuple(ext_list)) or file_name.endswith(tuple(ext_list.upper())) :
if file_name.endswith(tuple(ext_list)) or file_name.endswith(tuple(ext.upper() for ext in ext_list)):
pass
else:
file_list.remove(file_name)
return file_list
# `C:\Users` to `file:///C:/Users`
def edit_paths(self, video_files):
#Add path and prefix to files as required in vlc playlist file.
for index in range(len(video_files)):
video_files[index] =(
'file:///' + os.path.join(video_files[index])).replace('\\','/')
return video_files
def get_videos(self):
#Returns list of video files in the directory.
if check_subdirectories == True:
pathlist = [os.getcwd()] #List of all directories to be scanned.
for root, dirs, files in os.walk(os.getcwd()):
for name in dirs:
subdir_path = os.path.join(root, name)
if subdir_path.find('\.') != -1: #Excludes hidden directoriess.
pass
else:
pathlist.append(subdir_path)
videos = []
#Loops through files of root directory and every subdirectory.
for path in pathlist:
all_files = os.listdir(path)
for f in self.remove_nonvideo_files(all_files):
location = path+ '\\' + f
videos.append(location)
return videos
else:
videos = []
all_files = os.listdir()
for f in self.remove_nonvideo_files(all_files):
location = os.getcwd() + '\\' + f
videos.append(location)
return videos
# !!nk: different sorting
def sort_videos(self, video_files):
# sort files with prefix ('1. file', `10. file', '100. file')
if check_sort_file_prefix4:
mo = re.compile(pat_sort_file_prefix4)
# _T/www2/24. Предварител
# _ _ # exclude first and last when group
video_files.sort(key=lambda x: int(mo.search(x).group()[1:-1] ) )
#print(video_files)
return video_files
# `C:\Users\videos` to `http://addr:port/videos`
def web_paths(self, video_files):
#Add server addr to files as required in vlc playlist file.
web_files = []
# root directory for web server - current directory
webroot = ('file:///' + os.getcwd() ).replace('\\','/')
#
for index in range(len(video_files)):
file_url = (addr_playlist_web + video_files[index]).replace(webroot, '')
web_files.append(file_url)
return web_files
def printobj(xobj):
#ok
for item in xobj:
print(item)
def main():
playlist = Playlist()
playlist_web = Playlist()
videos = Videos()
video_files = videos.get_videos()
video_paths = videos.edit_paths(video_files)
video_sorted = videos.sort_videos(video_paths)
web_files = videos.web_paths(video_sorted)
# write song.xspf local playlist
for path in video_sorted: # video_paths
playlist.add_track(path)
#
playlist_xml = playlist.get_playlist()
with open('songs.xspf','w') as mf:
mf.write(xml.tostring(playlist_xml).decode('utf-8'))
# write songweb.xspf web playlist
for path in web_files: # video_paths
playlist_web.add_track(path)
#
playlist_xml = playlist_web.get_playlist()
with open('songsweb.xspf','w') as mf:
mf.write(xml.tostring(playlist_xml).decode('utf-8'))
main()
'''
playlist(ROOT)
title /title
trackList
track
location file:///path /location
title /title
image /image
duration /duration
/track
/tracklist
/playlist
'''