-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo-streaming.py
More file actions
86 lines (72 loc) · 3.65 KB
/
video-streaming.py
File metadata and controls
86 lines (72 loc) · 3.65 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
from mpegdash.parser import MPEGDASHParser
import urllib2
import os, xmltodict
class MPEGHelper():
def __init__(self):
self.mpd_url = 'http://yt-dash-mse-test.commondatastorage.googleapis.com/media/motion-20120802-manifest.mpd'
def readMPEGFile(self):
# mpd = MPEGDASHParser.parse(self.mpd_url)
# mpd = urllib2.urlopen(self.mpd_url).read()
data = None
file_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
file_name = 'TimesNowHD.mpd'
file_path = os.path.join(file_dir, file_name)
with open(file_path) as file:
data = xmltodict.parse(file.read())
file.close()
if data:
data = dict(data)
if data.has_key('MPD'):
if isinstance(data['MPD']['Period'], list):
periods = data['MPD']['Period']
for period in periods:
representations = self.getAdaptationSet(period['AdaptationSet'])
for representation in representations:
representation = dict(representation)
time_scale = dict(representation['SegmentTemplate'])['@timescale']
segmentTimeline = dict(representation['SegmentTemplate']['SegmentTimeline'])
# This is for getting last segments.
if isinstance(segmentTimeline['S'], list):
segment = dict(segmentTimeline['S'][-1])
else:
segment = dict(segmentTimeline['S'])
# time = segment['@t']
duration = segment['@d']
time = int(duration) / int(time_scale)
print 'time ',time
# This is last segment when video was last paused.
# here code for start process.
else:
period = dict(data['MPD']['Period'])
representations = self.getAdaptationSet(period['AdaptationSet'])
for representation in representations:
representation = dict(representation)
time_scale = dict(representation['SegmentTemplate'])['@timescale']
segmentTimeline = dict(representation['SegmentTemplate']['SegmentTimeline'])
# This is for getting last segments.
if isinstance(segmentTimeline['S'], list):
segment = dict(segmentTimeline['S'][-1])
else:
segment = dict(segmentTimeline['S'])
# time = segment['@t']
duration = segment['@d']
time = int(duration) / int(time_scale)
# This is last segment when video was last paused.
# here code for start process.
else:
print "No data found."
def getAdaptationSet(self, adaptationSet):
representations = []
try:
for adaptationSet in adaptationSet:
adaptationSet = dict(adaptationSet)
content_type = adaptationSet['@contentType']
if content_type != 'audio':
representations = adaptationSet['Representation']
except:
representations = []
return representations
if __name__ == "__main__":
# objM is an instance for MPEGHelper.
objM = MPEGHelper()
objM.readMPEGFile()