From: Antonio Ospite Date: Fri, 23 Mar 2012 15:32:01 +0000 (+0100) Subject: smooth-dl: factor out some common code X-Git-Url: https://git.ao2.it/smooth-dl.git/commitdiff_plain/65b7b56639ca9a2da6042778e9f0ada370b16a01?ds=sidebyside smooth-dl: factor out some common code get_chunk_quality_string() and get_chunk_name_string() abstract some code used in multiple places. --- diff --git a/smooth-dl.py b/smooth-dl.py index ac6e388..16ee966 100755 --- a/smooth-dl.py +++ b/smooth-dl.py @@ -161,13 +161,7 @@ def print_manifest_info(manifest): print -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] - +def get_chunk_quality_string(stream, quality_level): quality = stream.findall("QualityLevel")[quality_level] bitrate = quality.attrib["Bitrate"] @@ -185,6 +179,27 @@ def download_chunks(base_url, manifest, stream_index, quality_level, dest_dir): chunks_quality = url.split('/')[0].replace("{bitrate}", bitrate) chunks_quality = chunks_quality.replace("{CustomAttributes}", custom_attributes) + + return chunks_quality + + +def get_chunk_name_string(stream, chunk): + t = chunk.attrib["t"] + url = stream.attrib["Url"] + chunk_name = url.split('/')[1].replace("{start time}", t) + + return chunk_name + + +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] + + chunks_quality = get_chunk_quality_string(stream, quality_level) + chunks_dest_dir = os.path.join(dest_dir, chunks_quality) if os.path.exists(chunks_dest_dir) == False: os.mkdir(chunks_dest_dir, 0755) @@ -195,9 +210,8 @@ def download_chunks(base_url, manifest, stream_index, quality_level, dest_dir): print "\tChunks %10d/%-10d" % (0, len(chunks)), "\r", sys.stdout.flush() for i, c in enumerate(chunks): - t = c.attrib["t"] - chunk_name = url.split('/')[1].replace("{start time}", t) + chunk_name = get_chunk_name_string(stream, c) chunk_file = os.path.join(dest_dir, chunks_quality, chunk_name) if os.path.exists(chunk_file) == False: @@ -227,23 +241,8 @@ def rebuild_stream(manifest, stream_index, quality_level, src_dir, stream = manifest.findall('.//StreamIndex')[stream_index] - quality = stream.findall("QualityLevel")[quality_level] - bitrate = quality.attrib["Bitrate"] + chunks_quality = get_chunk_quality_string(stream, quality_level) - 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") @@ -254,9 +253,8 @@ def rebuild_stream(manifest, stream_index, quality_level, src_dir, print "\tChunks %10d/%-10d" % (0, len(chunks)), "\r", sys.stdout.flush() for i, c in enumerate(chunks): - t = c.attrib["t"] - chunk_name = url.split('/')[1].replace("{start time}", t) + chunk_name = get_chunk_name_string(stream, c) chunk_file = os.path.join(chunks_src_dir, chunk_name) f = open(chunk_file, "rb") @@ -269,6 +267,7 @@ def rebuild_stream(manifest, stream_index, quality_level, src_dir, # Add a nice WAV header if stream.attrib['Type'] == "audio": + quality = stream.findall("QualityLevel")[quality_level] codec_private_data = quality.attrib['CodecPrivateData'] fmt = {}