Mercurial > hg > config
comparison python/example/m3u.py @ 752:e6fb1a8fe66b
add example for m3u playlists [status: unknown]
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 03 Jul 2015 14:33:01 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
751:753b1c2bd64c | 752:e6fb1a8fe66b |
---|---|
1 #!/usr/bin/env python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 m3u playlist manipulator | |
6 | |
7 See: | |
8 - https://pypi.python.org/pypi/m3u8 | |
9 - http://gonze.com/playlists/playlist-format-survey.html#M3U | |
10 - http://www.assistanttools.com/articles/m3u_playlist_format.shtml | |
11 - http://en.wikipedia.org/wiki/M3U | |
12 - http://tools.ietf.org/html/draft-pantos-http-live-streaming-08#section-3.3.1 | |
13 """ | |
14 | |
15 # imports | |
16 import argparse | |
17 import m3u8 | |
18 import os | |
19 import subprocess | |
20 import sys | |
21 | |
22 # module globals | |
23 __all__ = ['main', 'M3UParser'] | |
24 here = os.path.dirname(os.path.realpath(__file__)) | |
25 string = (str, unicode) | |
26 | |
27 class Playlist(object): | |
28 def __init__(self, playlists=(), base_path=None): | |
29 self.clips = [] | |
30 for playlist in playlists: | |
31 assert os.path.isfile(playlist) | |
32 _base_path = os.path.abspath(base_path or os.path.dirname(os.path.abspath(playlist))) # TODO: this doesn't fix URLs | |
33 content = open(playlist).read() | |
34 m3u = m3u8.M3U8(content, base_path=_base_path) | |
35 for segment in m3u.segments: | |
36 path = segment.uri | |
37 title = segment.title | |
38 self.clips.append((title, path)) | |
39 | |
40 def __str__(self): | |
41 retval = [] | |
42 for title, path in self.clips: | |
43 if title: | |
44 line = '{} # {}'.format(os.path.abspath(path), title) | |
45 else: | |
46 line = os.path.abspath(path) | |
47 retval.append(line) | |
48 return '\n'.join(retval) | |
49 | |
50 class M3UParser(argparse.ArgumentParser): | |
51 """CLI option parser""" | |
52 def __init__(self, **kwargs): | |
53 kwargs.setdefault('description', __doc__) | |
54 argparse.ArgumentParser.__init__(self, **kwargs) | |
55 self.add_argument('playlists', metavar='playlist', nargs='+', | |
56 help="playlist to parse") | |
57 self.add_argument('-b', '--base', dest='base_path', default=os.getcwd(), | |
58 help="base path [DEFAULT: %(default)s]") | |
59 self.options = None | |
60 | |
61 def parse_args(self, *args, **kw): | |
62 options = argparse.ArgumentParser.parse_args(self, *args, **kw) | |
63 self.validate(options) | |
64 self.options = options | |
65 return options | |
66 | |
67 def validate(self, options): | |
68 """validate options""" | |
69 if not os.path.exists(options.base_path): | |
70 self.error("Base path '{}' does not exist".format(options.base_path)) | |
71 missing = [i for i in options.playlists | |
72 if not os.path.exists(i)] | |
73 if missing: | |
74 self.error("Playlist not found: {}".format(', '.join(missing))) | |
75 | |
76 def main(args=sys.argv[1:]): | |
77 """CLI""" | |
78 | |
79 # parse command line options | |
80 parser = M3UParser() | |
81 options = parser.parse_args(args) | |
82 | |
83 # create playlist | |
84 playlist = Playlist(options.playlists, options.base_path) | |
85 | |
86 # print out some things | |
87 print ('{}'.format(playlist)) | |
88 | |
89 if __name__ == '__main__': | |
90 main() |