import re

with open('stream.mpd', 'r', encoding='utf-8') as f:
    mpd = f.read()

# Rimuovi Widevine
mpd = re.sub(r'<ContentProtection[^>]*schemeIdUri="urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed"[^>]*>.*?</ContentProtection>', '', mpd, flags=re.DOTALL)
# Rimuovi PlayReady
mpd = re.sub(r'<ContentProtection[^>]*schemeIdUri="urn:uuid:9a04f079-9840-4286-ab92-e65be0885f95"[^>]*>.*?</ContentProtection>', '', mpd, flags=re.DOTALL)

clearkey_cp = '<ContentProtection schemeIdUri="urn:uuid:e2719d58-a985-b3c9-781a-214f90116bb0"></ContentProtection>'
mpd = re.sub(r'(<ContentProtection[^>]*value="cenc"[^>]*/>)', r'\1' + clearkey_cp, mpd)

with open('stream_clearkey.mpd', 'w', encoding='utf-8') as f:
    f.write(mpd)

print("Fatto")
