这里的 Dash 可不是一加手机的快充技术,在使用 Youtube DL 的时候频繁的接触到 DASH 这个关键词,查了一下 DASH 是流媒体技术,全称是 Dynamic Adaptive Streaming over HTTP,自适应流媒体技术,通过 HTTP 服务传送流媒体,在 YouTube,Netflix,Hulu 等流媒体网站中被频繁应用,国内 Bilibili 也引入了该技术 1

该技术的大致实现原理是在服务端将视频分片,每个分片都有自身的编码方式,甚至不同的分辨率,码率等等,而在客户端根据当前网速或者设备自行选择需要播放的分片,可以实现不同画质内容无缝切换。所以在 YouTube 切换画质时完全不会黑屏,更不会影响观看。更加具体的原理解释可以参考这里

另外几个值得一说的功能是

  • 音频视频分离,在后台播放时可以只拉取音频
  • 支持多音轨,多视频轨,多字幕任意切换

DASH 技术与编码器无关,可以使用 H.265, H.264, VP9 等等任何编码器进行编码。

DASH 结构

DASH 音视频流标识文件被称为 Media Presentation Description,包含了一组结构化音频视频内容。

MP4Box 命令

MP4Box 命令 可以对 MP4 文件进行合并,切割,提取等操作。更多可以参考官网

总而言之, MP4Box 命令可以实现如下:

  • 操作 MP4,3GP 等 ISO 文件,从不同来源和不同格式添加,删除,复用音频,视频和字幕
  • 将 MPEG-4 XMT 或者 W3C SVG 等编解码为二进制格式 MPEG-4 BIFS 或者 LASeR
  • 将元数据附加到单个流或整个 ISO 文件以生成符合 MPEG-21 或混合的 MPEG-4 / MPEG-21 文件
  • 用于准备 HTTP Adaptive Streaming 内容
  • 包装和标记结果,用于在不同设备(例如电话,平板电脑)或不同软件(例如 iTunes)上进行流式传输,下载和回放。

用于内容打包

MP4Box 可以用于将现成内容打包到 ISO 媒体文件,比如 MP4,3GP 等文件中。需要注意的是 MP4Box 命令并不会重新编码音频,视频,图片文件。将 DivX 文件转变为 MP4 文件:

MP4Box -add file.avi new_file.mp4

或者添加第二条音轨到上一条命令输出的文件

MP4Box -add audio2.mp3 new_file.mp4

MP4Box 可以从现存的容器中获取资源,可以使用 -info 来查看媒体资源

MP4Box -info file.avi

然后使用如下类似方法导入文件的音轨

MP4Box -add file.avi#audio new_file.mp4

分发

MP4Box 可以用于准备各种协议的传输分发协议,主要是 HTTP 下载或者 RTP streaming。

To prepare a file for simple progressive HTTP download, the following instruction will interleave file data by chunks of 500 milliseconds in order to enable playback while downloading the file (HTTP FastStart):

MP4Box -inter 500 file.mp4

To prepare for RTP, the following instruction will create RTP hint tracks for the file. This enables classic streaming servers like DarwinStreamingServer or QuickTime Streaming Server to deliver the file through RTSP/RTP:

MP4Box -hint file.mp4

To prepare for adaptive streaming (MPEG-DASH), the following instruction will create the DASH manifest and associated files. For more information on DASH see this post:

MP4Box -dash 1000 file.mp4

Dash

查看 MP4Box dash 相关的帮助:

MP4Box -h dash
DASH Options:
 -mpd m3u8            converts HLS manifest (local or remote http) to MPD
                       Note: not compatible with other DASH options (except -out and -tmp) and does not convert associated segments
 -dash dur            enables DASH-ing of the file(s) with a segment duration of DUR ms
                       Note: the duration of a fragment (subsegment) is set
                                using the -frag switch.
                       Note: for onDemand profile, sets duration of a subsegment
 -dash-strict dur     [DEPRECATED, will behave like -dash]
 -dash-live[=F] dur   generates a live DASH session using dur segment duration, optionally writing live context to F
                       MP4Box will run the live session until 'q' is pressed or a fatal error occurs.
 -ddbg-live[=F] dur   same as -dash-live without time regulation for debug purposes.
 -frag time_in_ms     Specifies a fragment duration of time_in_ms.
                       * Note: By default, this is the DASH duration
 -out filename        specifies output MPD file name.
 -tmp dirname         specifies directory for temporary file creation
                       * Note: Default temp dir is OS-dependent
 -profile NAME        specifies the target DASH profile: "onDemand",
                       "live", "main", "simple", "full",
                       "hbbtv1.5:live", "dashavc264:live", "dashavc264:onDemand"
                       * This will set default option values to ensure conformance to the desired profile
                       * Default profile is "full" in static mode, "live" in dynamic mode
 -profile-ext STRING  specifies a list of profile extensions, as used by DASH-IF and DVB.
                       The string will be colon-concatenated with the profile used

比如随便拿一个 mp4 文件:

MP4Box -dash 2000 -rap -profile dashavc264:onDemand input.mp4

解释:

  • -dash 2000 按照 1s 来切
  • -rap 强制让分段从随机点开始
  • -profile dashavc264:onDemand 可以查看 dash specifications 来查看更多 profile 相关的信息

这个操作不会对视频文件进行重新编码,只是将视频进行切片,所以非常快。执行命令结束后会得到 .mpd 文件和 *_dashinit.mp4 两个额外的文件。生成的这两个文件放到 HTTP 服务器中就可以在支持 mdp 播放的播放器中播放。

一些支持 DASH 的播放器

reference

  1. https://www.bilibili.com/read/cv867888/