from optparse import OptionParser
from urlparse import urlparse, urlunparse
from optparse import OptionParser
from urlparse import urlparse, urlunparse
def hexstring_to_bytes(hex_string):
res = ""
for i in range(0, len(hex_string), 2):
def hexstring_to_bytes(hex_string):
res = ""
for i in range(0, len(hex_string), 2):
+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 dest_file:
+ f = open(dest_file, mode)
+ f.write(data)
+ f.close()
+
+ 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"""
# if some intermediate client Manifest is used, like in Rai Replay
clip = manifest.find("Clip")
manifest_url = clip.attrib["Url"]
# if some intermediate client Manifest is used, like in Rai Replay
clip = manifest.find("Clip")
manifest_url = clip.attrib["Url"]
for i, s in enumerate(streams):
stream_type = s.attrib["Type"]
for i, s in enumerate(streams):
stream_type = s.attrib["Type"]
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)
url = stream.attrib["Url"]
chunks_quality = url.split('/')[0].replace("{bitrate}", bitrate)
url = stream.attrib["Url"]
chunks_quality = url.split('/')[0].replace("{bitrate}", bitrate)
def download_chunks(base_url, manifest, stream_index, quality_level, dest_dir):
def download_chunks(base_url, manifest, stream_index, quality_level, dest_dir):
chunks_quality = get_chunk_quality_string(stream, quality_level)
chunks_dest_dir = os.path.join(dest_dir, chunks_quality)
chunks_quality = get_chunk_quality_string(stream, quality_level)
chunks_dest_dir = os.path.join(dest_dir, chunks_quality)
os.mkdir(chunks_dest_dir, 0755)
chunks = stream.findall("c")
os.mkdir(chunks_dest_dir, 0755)
chunks = stream.findall("c")
for i, c in enumerate(chunks):
chunk_name = get_chunk_name_string(stream, c)
for i, c in enumerate(chunks):
chunk_name = get_chunk_name_string(stream, c)
- try:
- response = urllib2.urlopen(chunk_url)
- data = response.read()
-
- f = open(chunk_file, "wb")
- f.write(data)
- f.close()
- except Exception as e:
- print e
-
+ data = download_file(chunk_url, chunk_file, "wb")
def rebuild_stream(manifest, stream_index, quality_level, src_dir,
def rebuild_stream(manifest, stream_index, quality_level, src_dir,
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 download:
- download_chunks(url, manifest, video_stream_index,
- video_quality_level, chunks_dir)
- download_chunks(url, manifest, audio_stream_index,
- audio_quality_level, chunks_dir)
+ if download:
+ download_chunks(url, manifest, video_stream_index,
+ video_quality_level, chunks_dir)
+ download_chunks(url, manifest, audio_stream_index,
+ 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)
+ 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)
- rebuild_stream(manifest, audio_stream_index, audio_quality_level,
- chunks_dir, dest_audio, dest_audio + '.wav')
+ rebuild_stream(manifest, video_stream_index, video_quality_level,
+ chunks_dir, dest_video)
+ rebuild_stream(manifest, audio_stream_index, audio_quality_level,
+ chunks_dir, dest_audio, dest_audio + '.wav')
- # 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')
+ # 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')
usage = "usage: %prog [options] <manifest URL or file>"
parser = OptionParser(usage=usage, version=version,
usage = "usage: %prog [options] <manifest URL or file>"
parser = OptionParser(usage=usage, version=version,
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")
parser.add_option("-c", "--chunks-dir", metavar="<dir>",
dest="chunks_dir", default=None,
help="directory containing chunks, if different from destination dir")
parser.add_option("-c", "--chunks-dir", metavar="<dir>",
dest="chunks_dir", default=None,
help="directory containing chunks, if different from destination dir")
- parser.add_option("-v", "--video-stream", metavar="<n>",
+ parser.add_option("-v", "--video-stream", metavar="<n>",
type="int", dest="video_stream_index", default=0,
help="index of the video stream")
parser.add_option("-a", "--audio-stream", metavar="<n>",
type="int", dest="video_stream_index", default=0,
help="index of the video stream")
parser.add_option("-a", "--audio-stream", metavar="<n>",
parser = options_parser()
(options, args) = parser.parse_args()
parser = options_parser()
(options, args) = parser.parse_args()
- options.video_stream_index,
- options.audio_stream_index)
+ options.video_stream_index,
+ options.audio_stream_index)
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)
+
+
+if __name__ == "__main__":
+ main()