Shaka Packager Documentation

repository·main·Indexed 25 days ago

https://github.com/shaka-project/shaka-packager

A media packaging tool and SDK used to prepare, package, and encrypt media content for DASH and HLS streaming. It supports CENC and SAMPLE-AES encryption standards, various DRM systems including Widevine, PlayReady, FairPlay, and Marlin, and provides utilities like pssh-box.py for generating and parsing PSSH boxes. The SDK is compatible with Linux, Mac, Windows, and ARM platforms.

Tokens
30.9K
Snippets
78
Records
154
Agent score
79%

What's inside Shaka Packager

  1. Overview of Shaka Packager capabilities

    main

    Shaka Packager is a tool and media packaging SDK designed for packaging and encrypting media for DASH and HLS streaming. It supports both Video-On-Demand (VOD) and Live streaming scenarios.

    Supported Key Systems (DRM)

    • Widevine
    • PlayReady (Limited support)
    • FairPlay (Limited support)
    • Marlin (Limited support)

    Supported Encryption Standards

    • CENC
    • SAMPLE-AES

    Supported Platforms

    • Linux
    • Mac
    • Windows
    • ARM (via cross-compilation)
  2. Use segment template formatting for URL construction

    main

    Shaka Packager supports template-based segment URL construction following ISO/IEC 23009-1:2014. You can use specific identifiers enclosed in $ characters to dynamically construct segment URLs. Identifier matching is case-sensitive.

    Supported Identifiers

    IdentifierDescriptionFormatting Notes
    $$Escape sequence: replaces $$ with a single $N/A
    $Number$Substitutes the segment numberSupports printf-style formatting (see below).
    $Time$Substitutes the value of the SegmentTimeline@t attributeSupports printf-style formatting (see below).

    Note: You can use either $Number$ or $Time$, but not both in the same template. $RepresentationID$ and $Bandwidth$ are currently not supported.

    Formatting with printf-style tags

    Identifiers can be suffixed with a format tag inside the enclosing $ characters using the prototype %0[width]d. The width parameter is an unsigned integer specifying the minimum number of characters. If the value is shorter than the width, it is padded with zeros. Values are not truncated if they exceed the width.

    Example syntax: $Number$%04d (will result in 4-digit zero-padded numbers like 0001).

  3. Understand the public header structure for libpackager

    main
    The include/ directory contains the public headers for libpackager. When developing against or extending the library, note that these headers are strictly decoupled from the internal implementation. They only reference other public headers or standard system headers. They do not have access to internal headers located in packager/... or third-party dependency headers located in packager/third_party/....
  4. How CPIX maps keys to streams

    main

    Shaka Packager uses the ContentKeyUsageRuleList within the CPIX document to determine which keys apply to which streams:

    • Track Type Mapping: Rules with an intendedTrackType attribute map keys to streams where the DRM label matches the attribute (e.g., AUDIO, SD, HD, UHD1, UHD2).
    • Audio Filtering: Rules with an AudioFilter map keys specifically to audio streams.
    • Video Filtering: Rules with VideoFilter pixel ranges map keys to video labels (SD/HD/UHD1/UHD2) based on the thresholds defined by --max_sd_pixels, --max_hd_pixels, and --max_uhd1_pixels.
    • Combined Constraints: If both intendedTrackType and a filter are present, the stream must satisfy both.
    • Default Mapping: A rule with no filters, or a document containing a single key and no usage rules, maps that key to all streams.
    • Unused Keys: Keys not referenced by any usage rule are ignored.

    Note: DRM signaling is derived from the document's DRMSystemList and is authoritative. Key rotation via --crypto_period_duration is not supported when using CPIX.

  5. Configure the Teletext heartbeat mechanism

    main

    Because Teletext subtitles are 'sparse' (data only exists when text is displayed), Shaka Packager uses a 'heartbeat' mechanism to ensure continuous text segments and proper alignment with video segments. This mechanism is automatic when processing MPEG-TS files containing both video and teletext streams.

    To control the timing offset between video timestamps and text segment generation, use the --ts_ttx_heartbeat_shift parameter.

    • Default: 90000 (at 90kHz timescale, equivalent to 1 second).
    • When to increase: If text segments are generated too late (later than video segments).
    • When to decrease: If some text cues are missing from the output.

    Note: For best results and automatic segment alignment, always include the video and teletext streams from the same MPEG-TS source in the same packager invocation.

    # Example with custom heartbeat shift (3 seconds)
    $ packager \
      --ts_ttx_heartbeat_shift 270000 \
      'in=input.ts,stream=video,init_segment=v/init.mp4,segment_template=v/$Number$.m4s' \
      'in=input.ts,stream=audio,init_segment=a/init.mp4,segment_template=a/$Number$.m4s' \
      'in=input.ts,stream=text,cc_index=888,lang=en,init_segment=t/init.mp4,segment_template=t/$Number$.m4s'
  6. Understand Shaka Packager core concepts

    main

    Shaka Packager is a tool and SDK for DASH and HLS packaging and encryption. It can transmux media between different containers.

    Key Concepts:

    • No Transcoding: Shaka Packager does not perform transcoding. All media content must be pre-encoded before being passed to the packager.
    • Streams and Stream Descriptors: The packager operates on streams, which are defined by stream_descriptors. Streams can originate from the same file or different sources, such as regular files, pipes, or UDP streams.
  7. Implement an LL-DASH streaming pipeline

    main

    An LL-DASH pipeline requires three components working in sync:

    1. Packager: Uses --low_latency_dash_mode=true and uploads content via HTTP chunked transfer encoding.
    2. Delivery System: A server or proxy that can handle and chain together aggregating responses (e.g., chunked transfer encoding under HTTP/1.1, or HTTP/2/3). Examples include AWS MediaStore, s3-upload-proxy, or go-chunked-streaming-server.
    3. Player: A DASH client that understands the availabilityTimeOffset MPD value and can handle throughput estimation/ABR challenges in low-latency regimes. Examples include Shaka Player and dash.js.
  8. Optimize SegmentTimeline for live profiles

    main

    Use the --allow_approximate_segment_timeline flag for live profiles.

    When enabled, segments with nearly identical durations (difference < one sample) are treated as having the same duration. This allows the MPD generator to create fewer SegmentTimeline entries. If all segments except the last one have the same duration, the packager may optimize further by using SegmentTemplate@duration and omitting the SegmentTimeline entirely.

    Warning: This flag is ignored if $Time is used in the segment template, as $Time requires an accurate Segment Timeline.

  9. Format stream descriptors for Shaka Packager

    main

    Stream descriptors define how input media is processed and mapped to output files. You can provide multiple descriptors to handle different streams (audio, video, text) from the same or different input files.

    Each descriptor follows the syntax:

    <field>=<value>[,<field>=<value>]...

    Example of a basic descriptor structure:

    in=input.mp4,stream=video,out=video.mp4,segment=segment_$Number$.mp4
    <field>=<value>[,<field>=<value>]...
  10. How Teletext processing is synchronized with video/audio

    main

    When processing MPEG-TS input with DVB-Teletext, Shaka Packager uses a specialized pipeline to ensure text segments align perfectly with video/audio segment boundaries. This is achieved through a coordination mechanism:

    1. Mp2tMediaParser: Provides a "heartbeat" (video PTS timestamps) to the teletext parser to drive segment generation even when no subtitle data is present.
    2. EsParserTeletext: Parses DVB-Teletext PES packets and emits samples with roles like CueStart, CueEnd, TextHeartBeat, and MediaHeartBeat.
    3. SegmentCoordinator: Acts as an N-to-N handler. It replicates SegmentInfo from the video/audio streams to the teletext streams. This ensures that the text segment boundaries match the video segment boundaries exactly.
    4. TextChunker: In "coordinator mode", it uses the SegmentInfo events received from the SegmentCoordinator to determine segment boundaries, rather than relying on the timestamps within the text samples themselves.