smooth-dl: handle CustomAttributes in stream quality
[smooth-dl.git] / smooth-dl.py
index 36ba345..ac6e388 100755 (executable)
@@ -134,7 +134,7 @@ def get_manifest(base_url, dest_dir=tempfile.gettempdir(),
 
 def print_manifest_info(manifest):
 
-    streams = manifest.findall('//StreamIndex')
+    streams = manifest.findall('.//StreamIndex')
 
     for i, s in enumerate(streams):
         stream_type = s.attrib["Type"]
@@ -166,16 +166,25 @@ def download_chunks(base_url, manifest, stream_index, quality_level, dest_dir):
     if os.path.exists(dest_dir) == False:
         os.mkdir(dest_dir, 0755)
 
-    stream = manifest.findall('//StreamIndex')[stream_index]
+    stream = manifest.findall('.//StreamIndex')[stream_index]
 
     quality = stream.findall("QualityLevel")[quality_level]
     bitrate = quality.attrib["Bitrate"]
 
+    quality_attributes = quality.findall("CustomAttributes/Attribute")
+    custom_attributes = ""
+    for i in quality_attributes:
+        custom_attributes += "%s=%s," % (i.attrib["Name"], i.attrib["Value"])
+    custom_attributes = custom_attributes.rstrip(',')
+
     # Assume URLs are in this form:
     # Url="QualityLevels({bitrate})/Fragments(video={start time})"
+    # or
+    # Url="QualityLevels({bitrate},{CustomAttributes})/Fragments(video={start time})"
     url = stream.attrib["Url"]
 
     chunks_quality = url.split('/')[0].replace("{bitrate}", bitrate)
+    chunks_quality = chunks_quality.replace("{CustomAttributes}", custom_attributes)
     chunks_dest_dir = os.path.join(dest_dir, chunks_quality)
     if os.path.exists(chunks_dest_dir) == False:
         os.mkdir(chunks_dest_dir, 0755)
@@ -216,16 +225,25 @@ def rebuild_stream(manifest, stream_index, quality_level, src_dir,
     if final_dest_file == None:
         final_dest_file = dest_file_name
 
-    stream = manifest.findall('//StreamIndex')[stream_index]
+    stream = manifest.findall('.//StreamIndex')[stream_index]
 
     quality = stream.findall("QualityLevel")[quality_level]
     bitrate = quality.attrib["Bitrate"]
 
+    quality_attributes = quality.findall("CustomAttributes/Attribute")
+    custom_attributes = ""
+    for i in quality_attributes:
+        custom_attributes += "%s=%s," % (i.attrib["Name"], i.attrib["Value"])
+    custom_attributes = custom_attributes.rstrip(',')
+
     # Assume URLs are in this form:
     # Url="QualityLevels({bitrate})/Fragments(video={start time})"
+    # or
+    # Url="QualityLevels({bitrate},{CustomAttributes})/Fragments(video={start time})"
     url = stream.attrib["Url"]
 
     chunks_quality = url.split('/')[0].replace("{bitrate}", bitrate)
+    chunks_quality = chunks_quality.replace("{CustomAttributes}", custom_attributes)
     chunks_src_dir = os.path.join(src_dir, chunks_quality)
 
     dest_file = open(dest_file_name, "wb")
@@ -275,7 +293,7 @@ def rebuild_stream(manifest, stream_index, quality_level, src_dir,
 
 
 def calc_tracks_delay(manifest, stream1_index, stream2_index):
-    streams = manifest.findall('//StreamIndex')
+    streams = manifest.findall('.//StreamIndex')
 
     s1 = streams[stream1_index]
     s2 = streams[stream2_index]