Skip to content

DRM API

Auto-generated reference for the DRM systems: Widevine, PlayReady, ClearKey, and related helpers. For configuring DRM as a user, see DRM & CDM Setup.

ClearKey

ClearKey(key, iv=None)

AES-128 ClearKey (HLS) DRM system.

Give the IV where possible. If you do not give an IV, unshackle sets it to of the same bit-size as the content key.

warn_clear

warn_clear(reason)

Warn once per track that unshackle passed a segment through undecrypted.

decrypt

decrypt(path)

Decrypt a Track with AES-128 ClearKey (HLS) DRM.

A segment that fails the PKCS7 unpad is kept as decrypted: some streams are block aligned with no padding.

from_m3u_key classmethod

from_m3u_key(m3u_key, session=None)

Load a ClearKey from an M3U(8) Playlist's EXT-X-KEY.

Parameters:

Name Type Description Default
m3u_key Key

A Key object parsed from a m3u(8) playlist using the m3u8 library.

required
session AnySession | None

Optional HTTP session used to request external URIs with. Useful to set headers, proxies, cookies, and so forth.

None

ClearKeyCENC

ClearKeyCENC(kids, laurl=None, content_keys=None, **kwargs)

W3C EME ClearKey (org.w3.clearkey) DRM System over MPEG-CENC tracks.

Distinct from the HLS AES-128 ClearKey class: a license server gives the keys here as a JWK Set keyed by KID, and the media is standard CENC (decrypted with shaka-packager/mp4decrypt KID:KEY pairs, same as Widevine).

kid property

kid

Get first Key ID, if any.

Exceptions

KIDNotFound

Bases: Exception

KID (Encryption Key ID) was not found.

CEKNotFound

Bases: ClickException

CEK (Content Encryption Key) for KID was not found in License.

EmptyLicense

Bases: ClickException

License returned no Content Encryption Keys.

to_dict

to_dict()

This DRM in the export form; see wire.drm_to_dict.

get_license_challenge

get_license_challenge()

Assemble the W3C EME ClearKey JSON license request for the unkeyed KIDs.

get_content_keys

get_content_keys(*, licence, session=None)

Get Content Keys for this DRM Instance from a ClearKey license server.

The licence param is a function. unshackle gives it the W3C JSON license request as challenge. It may return the JWK Set license as a dict, JSON str, or bytes. If it returns None and the manifest gave a Laurl, unshackle POSTs the challenge there directly instead.

decrypt

decrypt(path)

Decrypt the file at path in place with this track's ClearKey (standard CENC) content keys.

mp4decrypt_key_args

mp4decrypt_key_args(track_kids=None)

Build the mp4decrypt --key arguments for every content key, and by track ID for each track_kids KID.

decrypt_with_mp4decrypt

decrypt_with_mp4decrypt(path)

Decrypt with mp4decrypt.

decrypt_with_shaka_packager

decrypt_with_shaka_packager(path)

Decrypt with shaka-packager.

ClearKeyCTR

ClearKeyCTR(licence, iv, magic, skip=0, key=None)

AES-128-CTR over a whole file: the service gives the IV and returns the content key through licence.

The key resolver asks licence at the licence step of a download or of a --skip-dl run, so listing tracks costs no request. A cipher built with key holds its content key from the start. magic is the signature the decrypted file must start with; a mismatch raises and keeps the input. skip drops that many decrypted bytes from the start of the file: a header that sits in front of the media. rewrite_paged rewrites a decrypted file in the paged AAC container (PAGED_AAC).

take_key

take_key(key)

Hold and return a content key: the one the licence callback returned, or one another source licensed.

content_key

content_key()

The content key, licensed on first use.

decrypt

decrypt(path)

Decrypt the file to a new file that then replaces it.

The dropped header goes through the cipher too, so the keystream stays aligned with the file.

CustomDRM

CustomDRM(
    plugin,
    *,
    kids=(),
    label=None,
    display_id=None,
    data=None,
)

The DRM of a track whose system a plugin adds: plain data that can cross the wire and the export.

It holds the plugin id, its label, KIDs, a display id, the content keys and the plugin's own JSON data; never a callable or a path. A plugin id that no loaded plugin has stops the track. With no kids given, the plugin reads them from data.

kid property

kid

The first KID, which names the track's own content key.

key property

key

The content key of kid as hex.

license

license(cdm)

Fill kids and content_keys through the plugin's licence with cdm, a device of the plugin.

wire_data

wire_data()

The part of data that the plugin lets a peer and an export carry.

decrypt

decrypt(path)

Decrypt path with the plugin into a new file that then replaces it; a failure keeps path.

decrypt_to

decrypt_to(path, output)

Decrypt path with the plugin into output.

finish

finish(path, track)

Run the plugin's fix of a merged file that the plugin decrypted per segment; False when it proves no content key.

to_dict

to_dict()

This DRM in the export form; see wire.drm_to_dict.

PlayReady

PlayReady(pssh, kid=None, pssh_b64=None, **kwargs)

Bases: CencDrm

PlayReady DRM System.

from_pssh_b64 classmethod

from_pssh_b64(pssh_b64, kid=None)

Load a base64 PSSH box or PlayReady Object, or a bare UTF-16LE WRMHEADER that some packagers emit.

It wraps a bare WRMHEADER in a PlayReady Object and keeps that as pssh_b64, so remote CDMs and the REST API can parse it again.

from_pssh_box classmethod

from_pssh_box(box, kid)

Make the DRM from a parsed PlayReady pssh box and the track's own KID.

header_kids staticmethod

header_kids(header)

Return the KIDs one WRM header names, in header order.

custom_attribute_kids staticmethod

custom_attribute_kids(header)

Return the KIDs a WRM header lists under CUSTOMATTRIBUTES/KIDS, skipping a value that is not a KID.

absorb

absorb(*others)

Fold sibling PlayReady objects into this one so their headers and KIDs get licensed too.

A manifest can attach one PlayReady object per WRM header to the same track (ISM shares its Protection list across tracks; HLS emits one per EXT-X-KEY). The caller keeps this object's identity, which the decrypt path and drm_lock depend on.

distinct_headers

distinct_headers()

Return the WRM headers to license, deduplicated by the KID set each one names.

Two headers naming the same KIDs (a v4.0 header beside its v4.3 twin, or one PRH per rendition for one content key) cost one challenge. A header with no readable KID is kept: it may still be the only route to a content key nothing else names.

pro_b64_for staticmethod

pro_b64_for(header)

Build a base64 one-record PlayReady Object holding only this header.

Remote CDMs send the value given to set_pssh_b64 rather than the header passed to get_license_challenge, so a per-header challenge needs a per-header PRO.

extract_keys_from_cdm

extract_keys_from_cdm(cdm, session_id)

Map each licence key to its hex content key: pyplayready names the KID key_id, pywidevine (HttpCdm) kid.

get_content_keys

get_content_keys(cdm, certificate, licence)

License every distinct WRM header whose KIDs still lack a content key.

A v4.2+ header may list several KIDs and one response may stack several licences, so one challenge often covers everything. Content that ships several headers (ISM with one ProtectionHeader per track type, HLS with one PRH per playlist) gets one challenge per header that is not yet covered. Headers are sent as-is: Microsoft lets the server ignore the header, so a merged header could ask for keys it never grants.

A header that a round refused, or that yielded nothing, is not sent again by this object: every track of a title runs this method, and a repeat would only repeat the refusal.

detect_revocation staticmethod

detect_revocation(text)

Return the decoded error string if a revocation HRESULT is present, else None.

Reads the code from the raw SOAP body (0x8004C065) or from an exception message that carries it, so this method covers any service.

Widevine

Widevine(pssh, kid=None, **kwargs)

Bases: CencDrm

Widevine DRM System.

pssh property

pssh

Get Protection System Specific Header Box.

kids property

kids

Get all Key IDs from PSSH, falling back to the externally provided KID.

Exceptions

PSSHNotFound

Bases: Exception

PSSH (Protection System Specific Header) was not found.

KIDNotFound

Bases: Exception

KID (Encryption Key ID) was not found.

CEKNotFound

Bases: ClickException

CEK (Content Encryption Key) for KID was not found in License.

EmptyLicense

Bases: ClickException

License returned no Content Encryption Keys.

from_playready staticmethod

from_playready(pssh)

A Widevine PSSH naming a v1 box's own KIDs, then the KIDs the PlayReady class reads from it.

pywidevine's own conversion fails on a bare WRMHEADER and byte-swaps PlayReady KIDs.

from_pssh_box classmethod

from_pssh_box(box, kid)

Make the DRM from a parsed Widevine pssh box and the track's own KID.

get_content_keys

get_content_keys(cdm, certificate, licence)

Make a CDM Session and get Content Keys for this DRM Instance. The certificate and license params are functions. unshackle gives them the challenge and the session ID.

whole_file_cipher

whole_file_cipher(track)

The whole-file cipher of a track, with or without a content key.

custom_drm

custom_drm(track)

The plugin DRM of a track, with or without a content key.

is_null_key

is_null_key(key)

True for a blank or all-zero content key, the null key that no vault stores.

own_cipher

own_cipher(drm)

True for a cipher that is not CENC: it has no KID to probe and no fragment to check.

device_licensed

device_licensed(drm)

True for a plugin DRM that a plugin device licenses, which the server may license and the decode check judges.

held_key

held_key(drm)

True for a plugin DRM that holds its content key from the start, so no licence step and no server key apply.

drop_null_keys

drop_null_keys(drm)

Remove the null keys from a DRM object's content keys. A read-only key view stays as it is.

real_kids

real_kids(drm)

Return the KIDs a DRM object names, without the all-zero and test-pattern placeholders.

own_kids

own_kids(drm)

Return the real KIDs that identify the track's own content key, as the DRM object knows them.