Created
January 19, 2018 14:47
-
-
Save amurzeau/5923059ccf97a022ba6295d8f2aac780 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from xml.etree import ElementTree | |
import sys | |
from urllib.parse import urlparse | |
import os | |
# Input is a HTTP Smooth streaming manifest URL (root XML tag is SmoothStreamingMedia) | |
# This script takes a XML manifest as input and output to stdout audio stream raw data | |
# Use like this: python3 parse_hss_audio.py http://host.com/stream.isml/Manifest | |
manifestUrl = urlparse(sys.argv[1]) | |
baseUrl = manifestUrl.scheme + "://" + manifestUrl.netloc + os.path.dirname(manifestUrl.path) | |
lastAudioTime = 0 | |
while True: | |
response = requests.get(manifestUrl.geturl()) | |
root = ElementTree.fromstring(response.text) | |
timeScale = root.get('TimeScale') | |
audioStreamUrl = root.find("./StreamIndex[@Type='audio']").get("Url") | |
audioBitrates = [ e.get("Bitrate") for e in root.findall("./StreamIndex[@Type='audio']/QualityLevel") ] | |
audioFirstTimestamp = int(root.find("./StreamIndex[@Type='audio']/c[1][@t]").get("t")) | |
audioDurations = [ int(e.get("d")) for e in root.findall("./StreamIndex[@Type='audio']/c") ] | |
audioTimes = [audioFirstTimestamp] | |
for duration in audioDurations: | |
audioTimes.append(audioTimes[-1] + duration) | |
fragmentUrl = baseUrl + "/" + audioStreamUrl | |
bitrate = audioBitrates[0] | |
for audioTime in audioTimes: | |
if lastAudioTime >= audioTime: | |
continue | |
lastAudioTime = audioTime | |
dataRequest = requests.get(fragmentUrl.format(**{'bitrate':bitrate, 'Bitrate':bitrate, 'start time':audioTime, 'start_time':audioTime})) | |
sys.stdout.buffer.write(dataRequest.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment