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 get_manifest(base_url, dest_dir=tempfile.gettempdir()):
+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()):
- manifest_url = base_url
- if not manifest_url.lower().endswith(('/manifest', '.ismc', '.csm')):
- manifest_url += '/Manifest'
+ if not manifest_url.lower().endswith(('/manifest', '.ismc', '.csm')):
+ manifest_url += '/Manifest'
- response = urllib2.urlopen(manifest_url)
- data = response.read()
-
- manifest_path = os.path.join(dest_dir, 'Manifest')
- f = open(manifest_path, "w")
- f.write(data)
- f.close()
+ if manifest_url.startswith('http://'):
+ local_manifest_path = os.path.join(dest_dir, 'Manifest')
+ download_file(manifest_url, local_manifest_path, "w")
try:
# if some intermediate client Manifest is used, like in Rai Replay
clip = manifest.find("Clip")
try:
# if some intermediate client Manifest is used, like in Rai Replay
clip = manifest.find("Clip")
- actual_manifest_url = clip.attrib["Url"]
- base_url = actual_manifest_url.lower().replace("/manifest", "")
- except:
+ manifest_url = clip.attrib["Url"]
+ manifest = download_file(manifest_url, None, None)
+ except AttributeError:
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)
return chunk_name
def download_chunks(base_url, manifest, stream_index, quality_level, dest_dir):
return chunk_name
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")
print "\nDownloading Stream %d" % stream_index
print "\tChunks %10d/%-10d" % (0, len(chunks)), "\r",
sys.stdout.flush()
print "\nDownloading Stream %d" % stream_index
print "\tChunks %10d/%-10d" % (0, len(chunks)), "\r",
sys.stdout.flush()
- chunk_name = get_chunk_name_string(stream, c)
- chunk_file = os.path.join(dest_dir, chunks_quality, chunk_name)
+ stream_duration = 0
+ for i, chunk in enumerate(chunks):
- if os.path.exists(chunk_file) == False:
- chunk_url = base_url + '/' + chunks_quality + '/' + chunk_name
- response = urllib2.urlopen(chunk_url)
- data = response.read()
+ 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"])
- f = open(chunk_file, "wb")
- f.write(data)
- f.close()
+ 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):
+ chunk_url = base_url + '/' + chunks_quality + '/' + chunk_name
+ 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,
print "\nRebuilding Stream %d" % stream_index
print "\tChunks %10d/%-10d" % (0, len(chunks)), "\r",
sys.stdout.flush()
print "\nRebuilding Stream %d" % stream_index
print "\tChunks %10d/%-10d" % (0, len(chunks)), "\r",
sys.stdout.flush()
- 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")
chunk_file = os.path.join(chunks_src_dir, chunk_name)
f = open(chunk_file, "rb")
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()