/
readm3u
executable file
·126 lines (103 loc) · 3.89 KB
/
readm3u
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
#!/usr/bin/env python3
r"""
Read m3u playlist with a given player.
EXAMPLES::
readm3u liste.m3u -p mplayer
readm3u liste.m3u -p omxplayer
.. TODO::
- Make sure it does not hangs on youtube urls
AUTHORS:
- Sebastien Labbe, january 2018, initial version
"""
from __future__ import absolute_import, print_function, division, unicode_literals
import sys,os,subprocess,argparse
import m3u8
def main():
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("input", type=str,
help="playlist file (.m3u)")
parser.add_argument("-p", "--player", type=str, default='omxplayer',
help="media player")
parser.add_argument("-v", "--verbose", default=False, action='store_true',
help="print filenames per pages")
args = parser.parse_args()
# Read the playlist
m3u8_obj = m3u8.load(args.input)
if args.verbose:
print("Files: ", m3u8_obj.files)
for segment in m3u8_obj.segments:
print("[{}s] Titre:{}".format(segment.duration, segment.title))
# March 25 2018: I keep this code in case the problem reappears
#if player == 'omxplayer':
# # https://www.raspberrypi.org/forums/viewtopic.php?t=116413
# # kill previous omxplayer to avoid the following error
# # tvservice-client: Failed to connect to TV service: -1
# p = subprocess.Popen(['killall', 'omxplayer.bin'],
# stdin=subprocess.PIPE,
# stdout=None,
# stderr=subprocess.PIPE)
# out, err = p.communicate()
# if not err == b'omxplayer.bin: no process found\n':
# print('some omxplayer were running: good thing we stop them')
# if args.verbose:
# print("out=",out)
# print("err=",err)
path = segment.absolute_uri
if path.count('\\'):
path = path.replace('\\','')
print(" file:{}".format(path))
if segment.base_path == 'https://www.youtube.com':
cmd = ['mpsyt', 'playurl', path]
elif args.player == 'omxplayer':
# https://github.com/popcornmix/omxplayer/issues/437
# the option --no-osd prevents omxplayer from hanging
cmd = [args.player, '--no-osd', path]
else:
cmd = [args.player, path]
if args.verbose:
print('cmd =',cmd)
# Option 0
# ret = os.system(' '.join(cmd))
# Option 1
# ret = subprocess.check_call(cmd)
# print('ret =',ret)
# Option 2
# ret = subprocess.check_call(' '.join(cmd), shell=True)
# print('ret =',ret)
# Option 3 (is better at least because the terminal is normal after
# interuption, there is a carriage return)
p = subprocess.Popen(cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=None)
out, err = p.communicate()
if args.verbose:
print("out=",out)
print("err=",err)
# NOTES:
#
# How to use ``subprocess.call`` and friends::
#
# >>> subprocess.check_call(['omxplayer', '/home/name/Musique/example.mp3'])
# Audio codec mp3 channels 1 samplerate 11025 bitspersample 16
# Subtitle count: 0, state: off, index: 1, delay: 0
# have a nice day ;)
# 0
#
# If command is a string, one needs to add ``shell=True``::
#
# >>> subprocess.check_call('omxplayer /home/name/Musique/example.mp3', shell=True)
# Audio codec mp3 channels 1 samplerate 11025 bitspersample 16
# Subtitle count: 0, state: off, index: 1, delay: 0
# have a nice day ;)
# 0
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)