Rust library for parsing, serializing and downloading media content from a DASH MPD manifest.
Перейти к файлу
HoLLy b43907848a Require implementors of ProgressObserver to be Send and Sync 2022-11-06 14:21:52 +01:00
.github/workflows Try to obtain a recent (v4.4) libav version via a ppa, to fix a build failure for ac-ffmpeg crate when using libav v4.2 2022-03-19 12:31:51 +01:00
examples Update examples and tests 2022-11-06 13:42:52 +01:00
src Require implementors of ProgressObserver to be Send and Sync 2022-11-06 14:21:52 +01:00
tests Update examples and tests 2022-11-06 13:42:52 +01:00
.gitignore Update gitignore to ignore files downloaded by tests 2022-11-06 13:43:26 +01:00
CHANGELOG.md Allow multiple @BaseURL attributes to allow failover (change from Option<> to Vec<> type) 2022-11-05 21:32:20 +01:00
Cargo.toml Update examples and tests 2022-11-06 13:42:52 +01:00
LICENSE-MIT New year 2022-01-13 10:58:19 +01:00
README.md Rationalize types of struct fields, in particular changing Option<Vec<>> to Vec<>. Apologies for breakage 2022-11-05 09:16:30 +01:00
rustfmt.toml Don't format to reduce diff 2022-11-06 13:23:10 +01:00

README.md

dash-mpd

A Rust library for parsing, serializing and downloading media content from a DASH MPD file, as used by video services such as on-demand replay of TV content and video streaming services like YouTube. Allows both parsing of a DASH manifest (XML format) to Rust structs (deserialization) and programmatic generation of an MPD manifest (serialization). The library also allows you to download media content from a streaming server.

Crates.io Released API docs CI Dependency status LICENSE

DASH (dynamic adaptive streaming over HTTP), also called MPEG-DASH, is a technology used for media streaming over the web, commonly used for video on demand (VOD) services. The Media Presentation Description (MPD) is a description of the resources (manifest or “playlist”) forming a streaming service, that a DASH client uses to determine which assets to request in order to perform adaptive streaming of the content. DASH MPD manifests can be used both with content encoded as H.264/MPEG and as WebM, and with file segments using either MPEG-2 Transport Stream (M2TS) container format or fragmented MPEG-4 (also called CFF). There is a good explanation of adaptive bitrate video streaming at howvideo.works.

This library provides a serde-based parser (deserializer) and serializer for the DASH MPD format, as formally defined in ISO/IEC standard 23009-1:2019. XML schema files are available for no cost from ISO. When MPD files in practical use diverge from the formal standard, this library prefers to interoperate with existing practice.

If the library feature fetch is enabled (which it is by default), the library also provides support for downloading content (audio or video) described by an MPD manifest. This involves selecting the alternative with the most appropriate encoding (in terms of bitrate, codec, etc.), fetching segments of the content using HTTP or HTTPS requests (this functionality depends on the reqwest crate) and muxing audio and video segments together.

If the library feature libav is enabled, muxing support (combining audio and video streams, which are often separated out in DASH streams) is provided by ffmpegs libav library, via the ac_ffmpeg crate. Otherwise, muxing is implemented by calling an external muxer, mkvmerge (from the MkvToolnix suite), ffmpeg or vlc as a subprocess. Note that these commandline applications implement a number of checks and workarounds to fix invalid input streams that tend to exist in the wild. Some of these workarounds are implemented here when using libav as a library, but not all of them, so download support tends to be more robust with the default configuration (using an external application as a subprocess).

The choice of external muxer depends on the filename extension of the path supplied to download_to() (will be .mp4 if you call download()):

  • .mkv: call mkvmerge first, then if that fails call ffmpeg
  • .mp4: call ffmpeg first, then if that fails call vlc
  • other: try ffmpeg, which supports many container formats

DASH features supported

  • VOD (static) stream manifests
  • Multi-period content
  • XLink elements (only with actuate=onLoad semantics), including resolve-to-zero
  • All forms of segment index info: SegmentBase@indexRange, SegmentTimeline, SegmentTemplate@duration, SegmentTemplate@index, SegmentList
  • Media containers of types supported by mkvmerge, ffmpeg or VLC (this includes Matroska, ISO-BMFF / CMAF / MP4, WebM, MPEG-2 TS)

Limitations / unsupported features

  • Dynamic MPD manifests, that are used for live streaming/OTT TV
  • Encrypted content using DRM such as Encrypted Media Extensions (EME) and Media Source Extension (MSE)
  • Subtitles (eg. WebVTT and TTML streams)
  • XLink with actuate=onRequest

Usage

To parse the contents of an MPD manifest into Rust structs:

use std::time::Duration;
use dash_mpd::{MPD, parse};

fn main() {
    let client = reqwest::blocking::Client::builder()
        .timeout(Duration::new(10, 0))
        .gzip(true)
        .build()
        .expect("creating reqwest HTTP client");
    let xml = client.get("http://rdmedia.bbc.co.uk/dash/ondemand/testcard/1/client_manifest-events.mpd")
        .header("Accept", "application/dash+xml,video/vnd.mpeg.dash.mpd")
        .send()
        .expect("requesting MPD content")
        .text()
        .expect("fetching MPD content");
    let mpd: MPD = parse(&xml)
        .expect("parsing MPD");
    if let Some(pi) = mpd.ProgramInformation {
        if let Some(title) = pi.Title {
            println!("Title: {:?}", title.content);
        }
        if let Some(source) = pi.Source {
            println!("Source: {:?}", source.content);
        }
    }
    for p in mpd.periods {
        if let Some(d) = p.duration {
            println!("Contains Period of duration {:?}", d);
        }
    }
}

See example dash_stream_info.rs for more information.

To generate an MPD manifest programmatically:

use serde::ser::Serialize;
use quick_xml::writer::Writer;
use quick_xml::se::Serializer;
use dash_mpd::{MPD, ProgramInformation, Title};

fn main() {
   let mut buffer = Vec::new();
   let writer = Writer::new_with_indent(&mut buffer, b' ', 2);
   let mut ser = Serializer::with_root(writer, Some("MPD"));

   let pi = ProgramInformation {
       Title: Some(Title { content: Some("My serialization example".into()) }),
       lang: Some("eng".into()),
       moreInformationURL: Some("https://github.com/emarsden/dash-mpd-rs".into()),
       ..Default::default()
   };
   let mpd = MPD {
       mpdtype: Some("static".into()),
       xmlns: Some("urn:mpeg:dash:schema:mpd:2011".into()),
       ProgramInformation: Some(pi),
       ..Default::default()
   };

   mpd.serialize(&mut ser)
       .expect("serializing MPD struct");
   let xml = String::from_utf8(buffer.clone()).unwrap();
}

See example serialize.rs for more detail.

To download content from an MPD manifest:

use dash_mpd::fetch::DashDownloader;

let url = "https://storage.googleapis.com/shaka-demo-assets/heliocentrism/heliocentrism.mpd";
match DashDownloader::new(url)
       .worst_quality()
       .download()
{
   Ok(path) => println!("Downloaded to {:?}", path),
   Err(e) => eprintln!("Download failed: {:?}", e),
}

See example download_bbc.rs for a little more detail.

An application that provides a convenient commandline interface for the download functionality is available separately in the dash-mpd-cli crate.

Installation

Add to your Cargo.toml file:

[dependencies]
dash-mpd = "0.6"

If you dont need the download functionality and wish to reduce code size, use:

[dependencies]
dash-mpd = { version = "0.6", default-features = false }

Platforms

This crate is tested on the following platforms:

  • Linux, with default features (mkvmerge or ffmpeg or vlc as a subprocess) and libav support, on AMD64 and Aarch64 architectures
  • MacOS, only with default features (problems building the ac-ffmpeg crate against current ffmpeg)
  • Microsoft Windows 10, only with default features
  • Android 11 on Aarch64 via termux (you'll need to install the rust, binutils and ffmpeg packages)
  • OpenBSD/AMD64, only with default features (libav support not tested)

License

This project is licensed under the MIT license. For more information, see the LICENSE-MIT file.