smooth-dl.py: support downloading from a local file in download_file()
[smooth-dl.git] / smooth-dl.py
index 41dfc4c..1488e2c 100755 (executable)
@@ -94,12 +94,18 @@ def write_wav_header(out_file, fmt, codec_private_data, data_len):
 
 
 def download_file(src_url, dest_file, mode):
-    try:
-        response = urllib2.urlopen(src_url)
-        data = response.read()
-    except urllib2.HTTPError:
-        sys.stderr.write("Error while dowloading URL: %s" % src_url)
-        raise
+
+    if os.path.exists(src_url):
+        f = open(src_url, "rb")
+        data = f.read()
+        f.close()
+    else:
+        try:
+            response = urllib2.urlopen(src_url)
+            data = response.read()
+        except urllib2.HTTPError:
+            sys.stderr.write("Error while dowloading URL: %s" % src_url)
+            raise
 
     if dest_file:
         f = open(dest_file, mode)
@@ -109,11 +115,8 @@ def download_file(src_url, dest_file, mode):
     return data
 
 
-def get_manifest(url, dest_dir=tempfile.gettempdir()):
-    """Returns the manifest and the new URL if this is changed"""
-
-    if not os.path.exists(dest_dir):
-        os.mkdir(dest_dir, 0755)
+def get_manifest(url, dest_dir):
+    """Returns the manifest element and the base content URL"""
 
     # Remove the querystring if present
     manifest_url = urlunparse(urlparse(url)._replace(query=''))
@@ -121,11 +124,8 @@ def get_manifest(url, dest_dir=tempfile.gettempdir()):
     if not manifest_url.lower().endswith(('/manifest', '.ismc', '.csm')):
         manifest_url += '/Manifest'
 
-    if manifest_url.startswith('http://'):
-        local_manifest_path = os.path.join(dest_dir, 'Manifest')
-        download_file(manifest_url, local_manifest_path, "w")
-    else:
-        local_manifest_path = url
+    local_manifest_path = os.path.join(dest_dir, 'Manifest')
+    download_file(manifest_url, local_manifest_path, "w")
 
     manifest = etree.parse(local_manifest_path)
 
@@ -133,13 +133,18 @@ def get_manifest(url, dest_dir=tempfile.gettempdir()):
     if version != "2":
         raise Exception('Only Smooth Streaming version 2 supported')
 
-    try:
-        # if some intermediate client Manifest is used, like in Rai Replay
-        clip = manifest.find("Clip")
-        manifest_url = clip.attrib["Url"]
-        manifest = download_file(manifest_url, None, None)
-    except AttributeError:
-        pass
+    # if some intermediate client Manifest is used, like in Rai Replay
+    # then get the final manifest
+    clip = manifest.find("Clip")
+    if clip is not None and "Url" in clip.attrib:
+        tmp_manifest_url = clip.attrib["Url"]
+        try:
+            tmp_manifest = download_file(tmp_manifest_url, None, None)
+            # set the new values only if the dowload succeded
+            manifest_url = tmp_manifest_url
+            manifest = tmp_manifest
+        except urllib2.HTTPError:
+            pass
 
     manifest_pattern = re.compile("/manifest$", re.IGNORECASE)
     base_url = manifest_pattern.sub("", manifest_url)
@@ -199,19 +204,14 @@ def get_chunk_quality_string(stream, quality_level):
     return chunks_quality
 
 
-def get_chunk_name_string(stream, chunk):
-    t = chunk.attrib["t"]
+def get_chunk_name_string(stream, chunk_time):
     url = stream.attrib["Url"]
-    chunk_name = url.split('/')[1].replace("{start time}", t)
+    chunk_name = url.split('/')[1].replace("{start time}", str(chunk_time))
 
     return chunk_name
 
 
 def download_chunks(base_url, manifest, stream_index, quality_level, dest_dir):
-
-    if not os.path.exists(dest_dir):
-        os.mkdir(dest_dir, 0755)
-
     stream = manifest.findall('.//StreamIndex')[stream_index]
 
     chunks_quality = get_chunk_quality_string(stream, quality_level)
@@ -225,9 +225,17 @@ def download_chunks(base_url, manifest, stream_index, quality_level, dest_dir):
     print "\nDownloading Stream %d" % stream_index
     print "\tChunks %10d/%-10d" % (0, len(chunks)), "\r",
     sys.stdout.flush()
-    for i, c in enumerate(chunks):
 
-        chunk_name = get_chunk_name_string(stream, c)
+    stream_duration = 0
+    for i, chunk in enumerate(chunks):
+
+        if "t" in chunk.attrib:
+            chunk_time = chunk.attrib["t"]
+        elif "d" in chunk.attrib:
+            chunk_time = stream_duration
+            stream_duration = chunk_time + int(chunk.attrib["d"])
+
+        chunk_name = get_chunk_name_string(stream, chunk_time)
         chunk_file = os.path.join(dest_dir, chunks_quality, chunk_name)
 
         if not os.path.exists(chunk_file):
@@ -263,9 +271,17 @@ def rebuild_stream(manifest, stream_index, quality_level, src_dir,
     print "\nRebuilding Stream %d" % stream_index
     print "\tChunks %10d/%-10d" % (0, len(chunks)), "\r",
     sys.stdout.flush()
-    for i, c in enumerate(chunks):
 
-        chunk_name = get_chunk_name_string(stream, c)
+    stream_duration = 0
+    for i, chunk in enumerate(chunks):
+
+        if "t" in chunk.attrib:
+            chunk_time = chunk.attrib["t"]
+        elif "d" in chunk.attrib:
+            chunk_time = stream_duration
+            stream_duration = chunk_time + int(chunk.attrib["d"])
+
+        chunk_name = get_chunk_name_string(stream, chunk_time)
         chunk_file = os.path.join(chunks_src_dir, chunk_name)
 
         f = open(chunk_file, "rb")
@@ -308,9 +324,16 @@ def calc_tracks_delay(manifest, stream1_index, stream2_index):
     s1 = streams[stream1_index]
     s2 = streams[stream2_index]
 
+    if "TimeScale" not in s1 or "TimeScale" not in s2:
+        return 0
+
     s1_start_chunk = s1.find("c")
     s2_start_chunk = s2.find("c")
 
+    if "t" not in s1_start_chunk.attrib \
+       or "t" not in s2_start_chunk.attrib:
+        return 0
+
     s1_start_time = int(s1_start_chunk.attrib['t'])
     s2_start_time = int(s2_start_chunk.attrib['t'])
 
@@ -331,7 +354,7 @@ def get_clip_duration(manifest):
     return float(duration) / 10000000  # here is the default timescale
 
 
-def smooth_download(url, manifest, dest_dir=tempfile.gettempdir(),
+def smooth_download(url, manifest, dest_dir,
                     video_stream_index=0, audio_stream_index=1,
                     video_quality_level=0, audio_quality_level=0,
                     chunks_dir=None, download=True,
@@ -416,6 +439,9 @@ def main():
         parser.print_help()
         parser.exit(1)
 
+    if not os.path.exists(options.dest_dir):
+        os.mkdir(options.dest_dir, 0755)
+
     url = args[0]
     manifest, url = get_manifest(url, options.dest_dir)