1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-23 22:52:25 +01:00
pywb/pywb/rewrite/rewrite_hls.py
Ilya Kreymer d8b67319e1 rewrite refactoring:
- rewrite headers after content to ensure content-length/content-encoding rewritten if content modified
- header rewriter: remove proxyrewriter, set default rule to 'prefix' or 'keep' if url rewriting or not
- set is_content_rw if record.content_stream(), assume content is modified
- add BufferedRewriter as base for dash, hls, amf rewriting which processes the full stream
- should_rw_content() determines if should attempt content rewriting
- support banner-only insert mode: added HTMLInsertOnlyRewriter, enable if no custom JS rules
- test: enable banner-only test mode
2017-05-22 18:52:17 -07:00

43 lines
1.1 KiB
Python

import re
from io import BytesIO
from pywb.rewrite.content_rewriter import BufferedRewriter
# ============================================================================
class RewriteHLS(BufferedRewriter):
EXT_INF = re.compile('#EXT-X-STREAM-INF:(?:.*[,])?BANDWIDTH=([\d]+)')
def rewrite_stream(self, stream):
buff = stream.read()
lines = buff.decode('utf-8').split('\n')
best = None
indexes = []
count = 0
best_index = None
for line in lines:
m = self.EXT_INF.match(line)
if m:
indexes.append(count)
bandwidth = int(m.group(1))
if not best or bandwidth > best:
best = bandwidth
best_index = count
count = count + 1
if indexes and best_index is not None:
indexes.remove(best_index)
for index in reversed(indexes):
del lines[index + 1]
del lines[index]
buff_io = BytesIO()
buff_io.write('\n'.join(lines).encode('utf-8'))
buff_io.seek(0)
return buff_io