

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# `SegmentTemplate` 中的 `duration` 属性
<a name="segtemp-format-duration"></a>

在默认 DASH 清单中，`SegmentTemplate` 包含一个 `SegmentTimeline`。时间轴描述 `Representation` 中的所有段，包括其持续时间和开始时间。对于直播活动，当时间轴从您的编码器接收区段时，会将其 AWS Elemental MediaPackage 添加到时间轴中。要了解新推出的片段，播放设备必须定期向其请求更新的清单 MediaPackage。

如果表示形式中的所有区段都具有相同的持续时间，则可以通过启用 MediaPackage 移除`SegmentTimeline`对象来帮助减少延迟并缩短清单。取而代之 MediaPackage 的是，为`duration`属性添加一个`SegmentTemplate`属性。播放设备使用 `duration` 和 `startNumber` 计算段何时可用。由于播放设备不必依靠更新的清单以了解段，因此，它不必经常请求更新以保持播放。有关 `duration` 属性的工作方式的信息，请参阅以下几节。

**主题**
+ [`duration` 属性的工作原理](#how-stemp-dur-works)
+ [具有压缩 DASH 清单的 `duration` 属性](#stemp-dur-combos)

## `duration` 属性的工作原理
<a name="how-stemp-dur-works"></a>

通过 DASH 端点上的**段模板格式**设置启用 `$duration$` 变量，如 [创建 DASH 端点](endpoints-dash.md) 中所述。这是使用清单完成的：

1.  AWS Elemental MediaPackage 生成 DASH 清单时，它会将`duration`属性添加到`SegmentTemplate`对象中，如以下示例所示：  
**Example**  

   ```
   <SegmentTemplate timescale="30000" media="index_video_1_0_$Number$.mp4?m=1535562908" initialization="index_video_1_0_init.mp4?m=1535562908" startNumber="175032" duration="90000" presentationTimeOffset="62061"/>
   ```

   段时间轴和各个段描述不包含在段模板中。
**重要**  
除了最终段以外，段与持续时间值的偏差不能超过 50%。持续时间为 90000 时，段必须介于 45000 和 135000 之间（时间刻度为 30000 时，介于 1.5 和 4.5 秒之间）。  
**Example**  

   以下是在段模板中使用 `duration` 的自适应集示例：

   ```
   <AdaptationSet mimeType="video/mp4" segmentAlignment="true" subsegmentAlignment="true" startWithSAP="1" subsegmentStartsWithSAP="1" bitstreamSwitching="true">
     <Representation id="1" width="852" height="480" frameRate="30/1" bandwidth="1200000" codecs="avc1.4D401F">
       <SegmentTemplate timescale="30000" media="index_video_1_0_$Number$.mp4?m=1535562908" initialization="index_video_1_0_init.mp4?m=1535562908" startNumber="175032" duration="90000" presentationTimeOffset="62061"/>
     </Representation>
     <Representation id="2" width="640" height="360" frameRate="30/1" bandwidth="800000" codecs="avc1.4D401E">
       <SegmentTemplate timescale="30000" media="index_video_3_0_$Number$.mp4?m=1535562908" initialization="index_video_3_0_init.mp4?m=1535562908" startNumber="175032" duration="90000" presentationTimeOffset="62061"/>
     </Representation>
     <Representation id="3" width="320" height="240" frameRate="30/1" bandwidth="499968" codecs="avc1.4D400D">
       <SegmentTemplate timescale="30000" media="index_video_5_0_$Number$.mp4?m=1535562908" initialization="index_video_5_0_init.mp4?m=1535562908" startNumber="175032" duration="90000" presentationTimeOffset="62061"/>
     </Representation>
   </AdaptationSet>
   ```

1. 播放设备使用 `media` 属性中定义的 URL 请求段。在 URL 中，它将 `$Number$` 变量替换为段编号，从 `SegmentTemplate` 中的 `startNumber` 值（第一个段）开始。

1. 如果您的播放设备需要确定最近的段，则使用以下公式：

   ((时钟时间 - `availabilityStartTime` ) / (`duration` / `timescale` )) \+ `startNumber`  
**Example**  

   播放设备使用以下值计算最近的段：
   + 播放设备的挂钟时间：2018-11-16T19:18:30Z
   + `availabilityStartTime`：来自清单的 `MPD` 对象的属性：2018-11-16T19:08:30Z
   + `duration`：来自清单的 `SegmentTemplate` 对象的属性：90000
   + `timescale`：来自 `SegmentTemplate` 的属性：30000
   + `startNumber`：来自 `SegmentTemplate` 的属性：175032

   它使用的计算是 ((2018-11-16T19:18:30Z - 2018-11-16T19:08:30Z) / (90000/30000)) \+ 175032

   然后，该计算变为 (600 秒播放时间) / (3 秒段持续时间) = 200 个播放的段。将这些段与 175032 起始段相加，从而得出最近的段为 175232。

## `duration` 属性限制
<a name="stemp-limitations"></a>

为确保正确播放并帮助防止出现片段时长冲突的问题，请对该 AWS Elemental MediaPackage 属性强制执行以下限制：`duration`
+ 您只能在创建终端节点时启用该功能。

  您无法以后修改终端节点，以将 `duration` 属性添加到 DASH 清单中。这包括从一种段模板格式更改为 `duration` 使用的格式。例如，您无法创建一个将 `$Time$` 变量与 `SegmentTimeline` 一起使用的终端节点，然后编辑该终端节点以将 `$Number$` 变量与 `duration` 一起使用。
+ 您必须保留在创建终端节点时设置的 **segment duration (段持续时间)** 值。

  您无法编辑终端节点以修改段持续时间。
+ 您必须从使用 `duration` 的终端节点中生成单周期 DASH 清单。

  您不能将多周期 DASH 与 `duration` 属性一起使用。
+ 您的摄取流必须使用常规分段节奏。
+ 您不能在摄取流中使用可变段长度。例如，SCTE-35 相关分段的结果。

## 具有压缩 DASH 清单的 `duration` 属性
<a name="stemp-dur-combos"></a>

将压缩清单与 `duration` 属性组合使用将进一步减小清单大小，但不会减小太多。对于每个适应集，压缩清单具有一个 `SegmentTemplate` 和 `SegmentTimeline`。使用该`duration`属性时， AWS Elemental MediaPackage 会移除区段时间轴。对于这两种处理，清单在每个适应集中具有一个 `SegmentTemplate`，而没有 `SegmentTimeline`。请见以下 示例。

有关压缩清单的更多信息，请参阅[压缩 DASH 清单](compacted.md)。

**重要**  
如果 representation 中的段有意设置为不同的段大小，请不要使用 `duration` 属性。只有在段具有一致的大小时，这种处理才有效。

**Example**    
下面是一个压缩清单的示例：  

```
<AdaptationSet mimeType="video/mp4" segmentAlignment="true" subsegmentAlignment="true" startWithSAP="1" subsegmentStartsWithSAP="1" bitstreamSwitching="true">
   <SegmentTemplate timescale="30000" media="index_video_$RepresentationID$_0_$Number$.mp4?m=1543947824" initialization="index_video_$RepresentationID$_0_init.mp4?m=1543947824" startNumber="1">
     <SegmentTimeline>
       <S t="62000" d="60000" r="9"/>
     </SegmentTimeline>
   </SegmentTemplate>
   <Representation id="1" width="640" height="360" frameRate="30/1" bandwidth="749952" codecs="avc1.640029"/>
   <Representation id="2" width="854" height="480" frameRate="30/1" bandwidth="1000000" codecs="avc1.640029"/>
   <Representation id="3" width="1280" height="720" frameRate="30/1" bandwidth="2499968" codecs="avc1.640029"/>
</AdaptationSet>
```  
下面是一个压缩清单的示例（具有 `duration` 属性）：  

```
<AdaptationSet mimeType="video/mp4" segmentAlignment="true" subsegmentAlignment="true" startWithSAP="1" subsegmentStartsWithSAP="1" bitstreamSwitching="true">
   <SegmentTemplate timescale="30000" media="index_video_$RepresentationID$_0_$Number$.mp4?m=1543947824" initialization="index_video_$RepresentationID$_0_init.mp4?m=1543947824" startNumber="1" duration="60000"/>
   <Representation id="1" width="640" height="360" frameRate="30/1" bandwidth="749952" codecs="avc1.640029"/>
   <Representation id="2" width="854" height="480" frameRate="30/1" bandwidth="1000000" codecs="avc1.640029"/>
   <Representation id="3" width="1280" height="720" frameRate="30/1" bandwidth="2499968" codecs="avc1.640029"/>
</AdaptationSet>
```