From 3b8f3a1504b46ff6085617ea22e027668d45a503 Mon Sep 17 00:00:00 2001 From: Antti Ajanki Date: Mon, 5 Jan 2015 20:12:29 +0200 Subject: [PATCH 1/3] [downloader/f4m] is optional according to the F4M specs --- youtube_dl/downloader/f4m.py | 38 ++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/youtube_dl/downloader/f4m.py b/youtube_dl/downloader/f4m.py index f9f6f3e73..87d3150c7 100644 --- a/youtube_dl/downloader/f4m.py +++ b/youtube_dl/downloader/f4m.py @@ -187,24 +187,27 @@ def build_fragments_list(boot_info): return res -def write_flv_header(stream, metadata): - """Writes the FLV header and the metadata to stream""" +def write_flv_header(stream): + """Writes the FLV header to stream""" # FLV header stream.write(b'FLV\x01') stream.write(b'\x05') stream.write(b'\x00\x00\x00\x09') - # FLV File body stream.write(b'\x00\x00\x00\x00') - # FLVTAG - # Script data - stream.write(b'\x12') - # Size of the metadata with 3 bytes - stream.write(struct_pack('!L', len(metadata))[1:]) - stream.write(b'\x00\x00\x00\x00\x00\x00\x00') - stream.write(metadata) - # Magic numbers extracted from the output files produced by AdobeHDS.php - # (https://github.com/K-S-V/Scripts) - stream.write(b'\x00\x00\x01\x73') + + +def write_metadata_tag(stream, metadata): + """Writes optional metadata tag to stream""" + if metadata: + # Script data + stream.write(b'\x12') + # Size of the metadata with 3 bytes + stream.write(struct_pack('!L', len(metadata))[1:]) + stream.write(b'\x00\x00\x00\x00\x00\x00\x00') + stream.write(metadata) + # Magic numbers extracted from the output files produced by AdobeHDS.php + # (https://github.com/K-S-V/Scripts) + stream.write(b'\x00\x00\x01\x73') def _add_ns(prop): @@ -256,7 +259,11 @@ def real_download(self, filename, info_dict): bootstrap = self.ydl.urlopen(bootstrap_url).read() else: bootstrap = base64.b64decode(bootstrap_node.text) - metadata = base64.b64decode(media.find(_add_ns('metadata')).text) + metadata_node = media.find(_add_ns('metadata')) + if metadata_node is not None: + metadata = base64.b64decode(metadata_node.text) + else: + metadata = None boot_info = read_bootstrap_info(bootstrap) fragments_list = build_fragments_list(boot_info) @@ -269,7 +276,8 @@ def real_download(self, filename, info_dict): tmpfilename = self.temp_name(filename) (dest_stream, tmpfilename) = sanitize_open(tmpfilename, 'wb') - write_flv_header(dest_stream, metadata) + write_flv_header(dest_stream) + write_metadata_tag(dest_stream, metadata) # This dict stores the download progress, it's updated by the progress # hook From 2c322cc5d65550de10d70d812ac3cd6742452252 Mon Sep 17 00:00:00 2001 From: Antti Ajanki Date: Mon, 5 Jan 2015 20:22:17 +0200 Subject: [PATCH 2/3] [downloader/f4m] The last value in a tag is the tag length --- youtube_dl/downloader/f4m.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/youtube_dl/downloader/f4m.py b/youtube_dl/downloader/f4m.py index 87d3150c7..744bf91cb 100644 --- a/youtube_dl/downloader/f4m.py +++ b/youtube_dl/downloader/f4m.py @@ -187,6 +187,10 @@ def build_fragments_list(boot_info): return res +def write_unsigned_int(stream, val): + stream.write(struct_pack('!I', val)) + + def write_flv_header(stream): """Writes the FLV header to stream""" # FLV header @@ -198,6 +202,8 @@ def write_flv_header(stream): def write_metadata_tag(stream, metadata): """Writes optional metadata tag to stream""" + FLV_TAG_HEADER_LEN = 11 + if metadata: # Script data stream.write(b'\x12') @@ -205,9 +211,7 @@ def write_metadata_tag(stream, metadata): stream.write(struct_pack('!L', len(metadata))[1:]) stream.write(b'\x00\x00\x00\x00\x00\x00\x00') stream.write(metadata) - # Magic numbers extracted from the output files produced by AdobeHDS.php - # (https://github.com/K-S-V/Scripts) - stream.write(b'\x00\x00\x01\x73') + write_unsigned_int(stream, FLV_TAG_HEADER_LEN + len(metadata)) def _add_ns(prop): From f14f2a6d7909eae5cf0d6f447f8e8a3eb53752d4 Mon Sep 17 00:00:00 2001 From: Antti Ajanki Date: Mon, 5 Jan 2015 20:30:40 +0200 Subject: [PATCH 3/3] [downloader/f4m] Minor cleanup --- youtube_dl/downloader/f4m.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/youtube_dl/downloader/f4m.py b/youtube_dl/downloader/f4m.py index 744bf91cb..c460c167a 100644 --- a/youtube_dl/downloader/f4m.py +++ b/youtube_dl/downloader/f4m.py @@ -191,6 +191,10 @@ def write_unsigned_int(stream, val): stream.write(struct_pack('!I', val)) +def write_unsigned_int_24(stream, val): + stream.write(struct_pack('!I', val)[1:]) + + def write_flv_header(stream): """Writes the FLV header to stream""" # FLV header @@ -202,13 +206,12 @@ def write_flv_header(stream): def write_metadata_tag(stream, metadata): """Writes optional metadata tag to stream""" + SCRIPT_TAG = b'\x12' FLV_TAG_HEADER_LEN = 11 if metadata: - # Script data - stream.write(b'\x12') - # Size of the metadata with 3 bytes - stream.write(struct_pack('!L', len(metadata))[1:]) + stream.write(SCRIPT_TAG) + write_unsigned_int_24(stream, len(metadata)) stream.write(b'\x00\x00\x00\x00\x00\x00\x00') stream.write(metadata) write_unsigned_int(stream, FLV_TAG_HEADER_LEN + len(metadata))