Skip to content

Instantly share code, notes, and snippets.

@LloydW93
Created April 25, 2017 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LloydW93/2514edae1d45c5fef9342b2f28ab84f0 to your computer and use it in GitHub Desktop.
Save LloydW93/2514edae1d45c5fef9342b2f28ab84f0 to your computer and use it in GitHub Desktop.
def get_last_fragments_for_manifest(manifest_url):
try:
segment_duration = 3.84
data = requests.get(manifest_url).text
xml = ET.fromstring(data)
length = isodate.parse_duration(xml.attrib["mediaPresentationDuration"]).seconds
last_segment = str(int(math.ceil(length/segment_duration)))
base_url = '/'.join(manifest_url.split('/')[:-1]) + '/'
last_audio_url = base_url + xml[0][1][2].attrib["media"].replace("$RepresentationID$", "audio_1=96000").replace("$Number$", last_segment)
try:
last_video_url = base_url + xml[0][2][2][0].attrib["media"].replace("$RepresentationID$", "video=5070000").replace("$Number$", last_segment)
except Exception as e:
last_video_url = base_url + xml[0][2][1].attrib["media"].replace("$RepresentationID$", "video=1604000").replace("$Number$", last_segment)
last_audio_url = base_url + xml[0][1][2].attrib["media"].replace("$RepresentationID$", "audio=96000").replace("$Number$", last_segment)
return (last_audio_url, last_video_url)
except Exception as e:
print "Could not read " + manifest_url
traceback.print_exc()
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment