smooth-dl.py: fix some pep8 issue
authorAntonio Ospite <ao2@ao2.it>
Fri, 15 Jan 2016 09:06:34 +0000 (10:06 +0100)
committerAntonio Ospite <ao2@ao2.it>
Fri, 15 Jan 2016 09:06:34 +0000 (10:06 +0100)
  smooth-dl.py:38:1: E402 module level import not at top of file
  smooth-dl.py:39:1: E402 module level import not at top of file
  smooth-dl.py:40:1: E402 module level import not at top of file
  smooth-dl.py:41:1: E402 module level import not at top of file
  smooth-dl.py:42:1: E402 module level import not at top of file
  smooth-dl.py:43:1: E402 module level import not at top of file
  smooth-dl.py:44:1: E402 module level import not at top of file
  smooth-dl.py:45:1: E402 module level import not at top of file
  smooth-dl.py:46:1: E402 module level import not at top of file
  smooth-dl.py:57:5: E265 block comment should start with '# '
  smooth-dl.py:96:1: E302 expected 2 blank lines, found 1
  smooth-dl.py:111:1: E302 expected 2 blank lines, found 1
  smooth-dl.py:173:25: E128 continuation line under-indented for visual indent
  smooth-dl.py:195:80: E501 line too long (84 > 79 characters)
  smooth-dl.py:246:9: E128 continuation line under-indented for visual indent
  smooth-dl.py:248:24: E711 comparison to None should be 'if cond is None:'
  smooth-dl.py:320:13: E127 continuation line over-indented for visual indent
  smooth-dl.py:333:9: E128 continuation line under-indented for visual indent
  smooth-dl.py:334:9: E128 continuation line under-indented for visual indent
  smooth-dl.py:335:9: E128 continuation line under-indented for visual indent
  smooth-dl.py:336:9: E128 continuation line under-indented for visual indent
  smooth-dl.py:338:23: E711 comparison to None should be 'if cond is None:'
  smooth-dl.py:343:21: E128 continuation line under-indented for visual indent
  smooth-dl.py:345:21: E128 continuation line under-indented for visual indent
  smooth-dl.py:351:17: E128 continuation line under-indented for visual indent
  smooth-dl.py:353:17: E128 continuation line under-indented for visual indent
  smooth-dl.py:355:9: E265 block comment should start with '# '
  smooth-dl.py:358:17: E128 continuation line under-indented for visual indent
  smooth-dl.py:363:23: E128 continuation line under-indented for visual indent
  smooth-dl.py:364:23: E128 continuation line under-indented for visual indent
  smooth-dl.py:374:13: E128 continuation line under-indented for visual indent
  smooth-dl.py:426:17: E128 continuation line under-indented for visual indent
  smooth-dl.py:427:17: E128 continuation line under-indented for visual indent
  smooth-dl.py:437:13: E128 continuation line under-indented for visual indent
  smooth-dl.py:438:13: E128 continuation line under-indented for visual indent
  smooth-dl.py:439:13: E128 continuation line under-indented for visual indent

smooth-dl.py

index e3fa256..aa57c6e 100755 (executable)
 # basically, write a proper implementation of manifest parsing and chunk
 # downloading
 
 # basically, write a proper implementation of manifest parsing and chunk
 # downloading
 
-
-__description = "Download videos served using Smooth Streaming technology"
-__version = "0.x"
-__author_info = "Written by Antonio Ospite http://ao2.it"
-
 import os
 import re
 import sys
 import os
 import re
 import sys
@@ -45,6 +40,10 @@ import tempfile
 from optparse import OptionParser
 from urlparse import urlparse, urlunparse
 
 from optparse import OptionParser
 from urlparse import urlparse, urlunparse
 
+__description = "Download videos served using Smooth Streaming technology"
+__version = "0.x"
+__author_info = "Written by Antonio Ospite http://ao2.it"
+
 
 def get_chunk_data(data):
 
 
 def get_chunk_data(data):
 
@@ -54,7 +53,7 @@ def get_chunk_data(data):
     data_start = moof_size + 4 + len('mdat')
     data_size = mdat_size - 4 - len('mdat')
 
     data_start = moof_size + 4 + len('mdat')
     data_size = mdat_size - 4 - len('mdat')
 
-    #print len(data[data_start:]), \
+    # print len(data[data_start:]), \
     #        len(data[data_start:data_start + data_size]), data_size
 
     assert(len(data[data_start:]) == data_size)
     #        len(data[data_start:data_start + data_size]), data_size
 
     assert(len(data[data_start:]) == data_size)
@@ -93,6 +92,7 @@ def write_wav_header(out_file, fmt, codec_private_data, data_len):
     out_file.write("data")
     out_file.write(struct.pack('<L', data_len))
 
     out_file.write("data")
     out_file.write(struct.pack('<L', data_len))
 
+
 def download_file(src_url, dest_file, mode):
     try:
         response = urllib2.urlopen(src_url)
 def download_file(src_url, dest_file, mode):
     try:
         response = urllib2.urlopen(src_url)
@@ -108,6 +108,7 @@ def download_file(src_url, dest_file, mode):
 
     return data
 
 
     return data
 
+
 def get_manifest(url, dest_dir=tempfile.gettempdir()):
     """Returns the manifest and the new URL if this is changed"""
 
 def get_manifest(url, dest_dir=tempfile.gettempdir()):
     """Returns the manifest and the new URL if this is changed"""
 
@@ -169,8 +170,9 @@ def print_manifest_info(manifest):
                 channels = q.attrib["Channels"]
                 sampling_rate = q.attrib["SamplingRate"]
                 bits_per_sample = q.attrib["BitsPerSample"]
                 channels = q.attrib["Channels"]
                 sampling_rate = q.attrib["SamplingRate"]
                 bits_per_sample = q.attrib["BitsPerSample"]
-                print "\t%2s: %4s %sHz %sbits %sch @ %7s bps" % (i, fourcc,
-                        sampling_rate, bits_per_sample, channels, bitrate)
+                print "\t%2s: %4s %sHz %sbits %sch @ %7s bps" % \
+                    (i, fourcc, sampling_rate, bits_per_sample, channels,
+                     bitrate)
 
     print
 
 
     print
 
@@ -192,7 +194,8 @@ def get_chunk_quality_string(stream, quality_level):
     url = stream.attrib["Url"]
 
     chunks_quality = url.split('/')[0].replace("{bitrate}", bitrate)
     url = stream.attrib["Url"]
 
     chunks_quality = url.split('/')[0].replace("{bitrate}", bitrate)
-    chunks_quality = chunks_quality.replace("{CustomAttributes}", custom_attributes)
+    chunks_quality = chunks_quality.replace("{CustomAttributes}",
+                                            custom_attributes)
 
     return chunks_quality
 
 
     return chunks_quality
 
@@ -243,9 +246,9 @@ def download_chunks(base_url, manifest, stream_index, quality_level, dest_dir):
 
 
 def rebuild_stream(manifest, stream_index, quality_level, src_dir,
 
 
 def rebuild_stream(manifest, stream_index, quality_level, src_dir,
-        dest_file_name, final_dest_file=None):
+                   dest_file_name, final_dest_file=None):
 
 
-    if final_dest_file == None:
+    if final_dest_file is None:
         final_dest_file = dest_file_name
 
     stream = manifest.findall('.//StreamIndex')[stream_index]
         final_dest_file = dest_file_name
 
     stream = manifest.findall('.//StreamIndex')[stream_index]
@@ -317,7 +320,7 @@ def calc_tracks_delay(manifest, stream1_index, stream2_index):
 
     # calc difference in seconds
     delay = s2_start_time / s2_timescale - \
 
     # calc difference in seconds
     delay = s2_start_time / s2_timescale - \
-            s1_start_time / s1_timescale
+        s1_start_time / s1_timescale
 
     return delay
 
 
     return delay
 
@@ -330,39 +333,39 @@ def get_clip_duration(manifest):
 
 
 def smooth_download(url, manifest, dest_dir=tempfile.gettempdir(),
 
 
 def smooth_download(url, manifest, dest_dir=tempfile.gettempdir(),
-        video_stream_index=0, audio_stream_index=1,
-        video_quality_level=0, audio_quality_level=0,
-        chunks_dir=None, download=True,
-        out_video_file='_video.vc1', out_audio_file='_audio.raw'):
+                    video_stream_index=0, audio_stream_index=1,
+                    video_quality_level=0, audio_quality_level=0,
+                    chunks_dir=None, download=True,
+                    out_video_file='_video.vc1', out_audio_file='_audio.raw'):
 
 
-        if chunks_dir == None:
+        if chunks_dir is None:
             chunks_dir = dest_dir
 
         if download:
             download_chunks(url, manifest, video_stream_index,
             chunks_dir = dest_dir
 
         if download:
             download_chunks(url, manifest, video_stream_index,
-                    video_quality_level, chunks_dir)
+                            video_quality_level, chunks_dir)
             download_chunks(url, manifest, audio_stream_index,
             download_chunks(url, manifest, audio_stream_index,
-                    audio_quality_level, chunks_dir)
+                            audio_quality_level, chunks_dir)
 
         dest_video = os.path.join(dest_dir, out_video_file)
         dest_audio = os.path.join(dest_dir, out_audio_file)
 
         rebuild_stream(manifest, video_stream_index, video_quality_level,
 
         dest_video = os.path.join(dest_dir, out_video_file)
         dest_audio = os.path.join(dest_dir, out_audio_file)
 
         rebuild_stream(manifest, video_stream_index, video_quality_level,
-                chunks_dir, dest_video)
+                       chunks_dir, dest_video)
         rebuild_stream(manifest, audio_stream_index, audio_quality_level,
         rebuild_stream(manifest, audio_stream_index, audio_quality_level,
-                chunks_dir, dest_audio, dest_audio + '.wav')
+                       chunks_dir, dest_audio, dest_audio + '.wav')
 
 
-        #duration = get_clip_duration(manifest)
+        # duration = get_clip_duration(manifest)
 
         delay = calc_tracks_delay(manifest, video_stream_index,
 
         delay = calc_tracks_delay(manifest, video_stream_index,
-                audio_stream_index)
+                                  audio_stream_index)
 
         # optionally encode audio to vorbis:
         # ffmpeg -i _audio.raw.wav -acodec libvorbis -aq 60 audio.ogg
         mux_command = ("ffmpeg -i %s \\\n" +
 
         # optionally encode audio to vorbis:
         # ffmpeg -i _audio.raw.wav -acodec libvorbis -aq 60 audio.ogg
         mux_command = ("ffmpeg -i %s \\\n" +
-                      "  -itsoffset %f -async 1 -i %s \\\n" +
-                      "  -vcodec copy -acodec copy ffout.mkv") % \
-                      (dest_video, delay, dest_audio + '.wav')
+                       "  -itsoffset %f -async 1 -i %s \\\n" +
+                       "  -vcodec copy -acodec copy ffout.mkv") % \
+            (dest_video, delay, dest_audio + '.wav')
 
         print mux_command
 
 
         print mux_command
 
@@ -371,7 +374,7 @@ def options_parser():
     version = "%%prog %s" % __version
     usage = "usage: %prog [options] <manifest URL or file>"
     parser = OptionParser(usage=usage, version=version,
     version = "%%prog %s" % __version
     usage = "usage: %prog [options] <manifest URL or file>"
     parser = OptionParser(usage=usage, version=version,
-            description=__description, epilog=__author_info)
+                          description=__description, epilog=__author_info)
     parser.add_option("-i", "--info",
                       action="store_true", dest="info_only",
                       default=False, help="print Manifest info and exit")
     parser.add_option("-i", "--info",
                       action="store_true", dest="info_only",
                       default=False, help="print Manifest info and exit")
@@ -423,8 +426,8 @@ if __name__ == "__main__":
 
     if options.sync_delay:
         print calc_tracks_delay(manifest,
 
     if options.sync_delay:
         print calc_tracks_delay(manifest,
-                options.video_stream_index,
-                options.audio_stream_index)
+                                options.video_stream_index,
+                                options.audio_stream_index)
         parser.exit(0)
 
     if options.info_only:
         parser.exit(0)
 
     if options.info_only:
@@ -434,6 +437,6 @@ if __name__ == "__main__":
     print_manifest_info(manifest)
 
     smooth_download(url, manifest, options.dest_dir,
     print_manifest_info(manifest)
 
     smooth_download(url, manifest, options.dest_dir,
-            options.video_stream_index, options.audio_stream_index,
-            options.video_quality_level, options.audio_quality_level,
-            options.chunks_dir, options.download)
+                    options.video_stream_index, options.audio_stream_index,
+                    options.video_quality_level, options.audio_quality_level,
+                    options.chunks_dir, options.download)