通过自定义 MES 预设执行高级编码Perform advanced encoding by customizing MES presets

概述Overview

本主题演示如何自定义 Media Encoder Standard 预设。This topic shows how to customize Media Encoder Standard presets. 通过使用自定义预设的 Media Encoder Standard 进行编码主题演示如何使用 .NET 创建编码任务和执行此任务的作业。The Encoding with Media Encoder Standard using custom presets topic shows how to use .NET to create an encoding task and a job that executes this task. 自定义预设后,请将自定义预设提供给编码任务。Once you customize a preset, supply the custom presets to the encoding task.

如果使用的是 XML 预设,请务必保留元素顺序,如下面的 XML 示例所示(例如,KeyFrameInterval 应在 SceneChangeDetection 前面)。If using an XML preset, make sure to preserve the order of elements, as shown in XML samples below (for example, KeyFrameInterval should precede SceneChangeDetection).

Note

Media Encoder Standard 的许多高级媒体服务 v2 功能目前在 v3 中不可用。Many of the advanced Media Services v2 features of the Media Encoder Standard are currently not available in v3. 有关详细信息,请参阅功能差距For more information, see feature gaps.

支持相对大小Support for relative sizes

生成缩略图时,不需始终以像素为单位指定输出宽度和高度。When generating thumbnails, you do not need to always specify output width and height in pixels. 你可以以百分比的方式在 [1%, …, 100%] 范围内对其进行指定。You can specify them in percentages, in the range [1%, …, 100%].

JSON 预设JSON preset

"Width": "100%",
"Height": "100%"

XML 预设XML preset

<Width>100%</Width>
<Height>100%</Height>

生成缩略图Generate thumbnails

本部分说明如何自定义生成缩略图的预设。This section shows how to customize a preset that generates thumbnails. 下面定义的预设包含有关如何对文件编码的信息,以及生成缩略图所需的信息。The preset defined below contains information on how you want to encode your file as well as information needed to generate thumbnails. 可使用此部分所述的任何 MES 预设,并添加生成缩略图的代码。You can take any of the MES presets documented this section and add code that generates thumbnails.

Note

如果要编码为单比特率视频,以下预设中的 SceneChangeDetection 设置只能设置为 true。The SceneChangeDetection setting in the following preset can only be set to true if you are encoding to a single bitrate video. 如果要编码为多比特率视频并将 SceneChangeDetection 设置为 true,则编码器返回错误。If you are encoding to a multi-bitrate video and set SceneChangeDetection to true, the encoder returns an error.

有关架构的信息,请参阅主题。For information about schema, see this topic.

请务必仔细阅读 注意事项 部分。Make sure to review the Considerations section.

JSON 预设JSON preset

{
  "Version": 1.0,
  "Codecs": [
    {
      "KeyFrameInterval": "00:00:02",
      "SceneChangeDetection": "true",
      "H264Layers": [
        {
          "Profile": "Auto",
          "Level": "auto",
          "Bitrate": 4500,
          "MaxBitrate": 4500,
          "BufferWindow": "00:00:05",
          "Width": 1280,
          "Height": 720,
          "ReferenceFrames": 3,
          "EntropyMode": "Cabac",
          "AdaptiveBFrame": true,
          "Type": "H264Layer",
          "FrameRate": "0/1"

        }
      ],
      "Type": "H264Video"
    },
    {
      "JpgLayers": [
        {
          "Quality": 90,
          "Type": "JpgLayer",
          "Width": 640,
          "Height": 360
        }
      ],
      "Start": "{Best}",
      "Type": "JpgImage"
    },
    {
      "PngLayers": [
        {
          "Type": "PngLayer",
          "Width": 640,
          "Height": 360,
        }
      ],
      "Start": "00:00:01",
      "Step": "00:00:10",
      "Range": "00:00:58",
      "Type": "PngImage"
    },
    {
      "BmpLayers": [
        {
          "Type": "BmpLayer",
          "Width": 640,
          "Height": 360
        }
      ],
      "Start": "10%",
      "Step": "10%",
      "Range": "90%",
      "Type": "BmpImage"
    },
    {
      "Channels": 2,
      "SamplingRate": 48000,
      "Bitrate": 128,
      "Type": "AACAudio"
    }
  ],
  "Outputs": [
    {
      "FileName": "{Basename}_{Index}{Extension}",
      "Format": {
        "Type": "JpgFormat"
      }
    },
    {
      "FileName": "{Basename}_{Index}{Extension}",
      "Format": {
        "Type": "PngFormat"
      }
    },
    {
      "FileName": "{Basename}_{Index}{Extension}",
      "Format": {
        "Type": "BmpFormat"
      }
    },
    {
      "FileName": "{Basename}_{Width}x{Height}_{VideoBitrate}.mp4",
      "Format": {
        "Type": "MP4Format"
      }
    }
  ]
}

XML 预设XML preset

<?xml version="1.0" encoding="utf-16"?>
<Preset xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" Version="1.0" xmlns="https://www.windowsazure.com/media/encoding/Preset/2014/03">
  <Encoding>
    <H264Video>
      <KeyFrameInterval>00:00:02</KeyFrameInterval>
      <SceneChangeDetection>true</SceneChangeDetection>
      <H264Layers>
        <H264Layer>
          <Bitrate>4500</Bitrate>
          <Width>1280</Width>
          <Height>720</Height>
          <FrameRate>0/1</FrameRate>
          <Profile>Auto</Profile>
          <Level>auto</Level>
          <BFrames>3</BFrames>
          <ReferenceFrames>3</ReferenceFrames>
          <Slices>0</Slices>
          <AdaptiveBFrame>true</AdaptiveBFrame>
          <EntropyMode>Cabac</EntropyMode>
          <BufferWindow>00:00:05</BufferWindow>
          <MaxBitrate>4500</MaxBitrate>
        </H264Layer>
      </H264Layers>
    </H264Video>
    <AACAudio>
      <Profile>AACLC</Profile>
      <Channels>2</Channels>
      <SamplingRate>48000</SamplingRate>
      <Bitrate>128</Bitrate>
    </AACAudio>
    <JpgImage Start="{Best}">
      <JpgLayers>
        <JpgLayer>
          <Width>640</Width>
          <Height>360</Height>
          <Quality>90</Quality>
        </JpgLayer>
      </JpgLayers>
    </JpgImage>
    <BmpImage Start="10%" Step="10%" Range="90%">
      <BmpLayers>
        <BmpLayer>
          <Width>640</Width>
          <Height>360</Height>
        </BmpLayer>
      </BmpLayers>
    </BmpImage>
    <PngImage Start="00:00:01" Step="00:00:10" Range="00:00:58">
      <PngLayers>
        <PngLayer>
          <Width>640</Width>
          <Height>360</Height>
        </PngLayer>
      </PngLayers>
    </PngImage>
  </Encoding>
  <Outputs>
    <Output FileName="{Basename}_{Width}x{Height}_{VideoBitrate}.mp4">
      <MP4Format />
    </Output>
    <Output FileName="{Basename}_{Index}{Extension}">
      <JpgFormat />
    </Output>
    <Output FileName="{Basename}_{Index}{Extension}">
      <BmpFormat />
    </Output>
    <Output FileName="{Basename}_{Index}{Extension}">
      <PngFormat />
    </Output>
  </Outputs>
</Preset>

注意事项Considerations

请注意以下事项:The following considerations apply:

  • 为 Start/Step/Range 使用的显式时间戳假设输入源的长度至少为 1 分钟。The use of explicit timestamps for Start/Step/Range assumes that the input source is at least 1 minute long.

  • Jpg/Png/BmpImage 元素包含 Start、Step 和 Range 字符串属性 - 这些属性解释如下:Jpg/Png/BmpImage elements have Start, Step, and Range string attributes - these can be interpreted as:

    • 帧数(如果为非负整数),例如,"Start":"120";Frame Number if they are non-negative integers, for example "Start": "120",

    • 相对于源持续时间(如果以 % 为后缀表示),例如:"Start":"15%",或者Relative to source duration if expressed as %-suffixed, for example "Start": "15%", OR

    • 时间戳(如果以 HH:MM:SS...Timestamp if expressed as HH:MM:SS… 格式表示),例如:"Start":"00:01:00"format, for example "Start" : "00:01:00"

      可以随意混搭使用表示法。You can mix and match notations as you please.

      此外,Start 还支持特殊的宏 {Best},它会尝试确定第一个“有意义”的内容帧。请注意:(Start 设置为 {Best} 时,将忽略 Step 与 Range)Additionally, Start also supports a special Macro:{Best}, which attempts to determine the first "interesting" frame of the content NOTE: (Step and Range are ignored when Start is set to {Best})

    • 默认值:Start:{Best}Defaults: Start:{Best}

  • 需要显式提供每个图像格式的输出格式:Jpg/Png/BmpFormat。Output format needs to be explicitly provided for each Image format: Jpg/Png/BmpFormat. 提供时,MES 会将 JpgVideo 与 JpgFormat 进行匹配,依此类推。When present, MES matches JpgVideo to JpgFormat and so on. OutputFormat 引入了新的图像编解码器特定宏 {Index},需要为图像输出格式提供该宏一次(且只需一次)。OutputFormat introduces a new image-codec specific Macro: {Index}, which needs to be present (once and only once) for image output formats.

剪裁视频(剪切)Trim a video (clipping)

本部分说明如何修改编码器预设,以裁剪或修剪其输入为所谓的夹层文件或按需文件的输入视频。This section talks about modifying the encoder presets to clip or trim the input video where the input is a so-called mezzanine file or on-demand file. 也可以使用编码器来剪切或剪裁从实时流捕获或存档的资产 - 此博客提供了详细信息。The encoder can also be used to clip or trim an asset, which is captured or archived from a live stream - the details for this are available in this blog.

若要裁剪视频,可以使用此部分所述的任何 MES 预设,并修改“Sources” 元素(如下所示)。To trim your videos, you can take any of the MES presets documented this section and modify the Sources element (as shown below). StartTime 的值需与输入视频的绝对时间戳匹配。The value of StartTime needs to match the absolute timestamps of the input video. 例如,如果输入视频第一帧的时间戳为 12:00:10.000,则 StartTime 应大于或等于 12:00:10.000。For example, if the first frame of the input video has a timestamp of 12:00:10.000, then StartTime should be at least 12:00:10.000 and greater. 在以下示例中,假设输入视频的起始时间戳为零。In the example below, we assume that the input video has a starting timestamp of zero. Sources 应位于预设的开始处。Sources should be placed at the beginning of the preset.

JSON 预设JSON preset

{
  "Version": 1.0,
  "Sources": [
    {
      "StartTime": "00:00:04",
      "Duration": "00:00:16"
    }
  ],
  "Codecs": [
    {
      "KeyFrameInterval": "00:00:02",
      "StretchMode": "AutoSize",
      "H264Layers": [
        {
          "Profile": "Auto",
          "Level": "auto",
          "Bitrate": 3400,
          "MaxBitrate": 3400,
          "BufferWindow": "00:00:05",
          "Width": 1280,
          "Height": 720,
          "BFrames": 3,
          "ReferenceFrames": 3,
          "AdaptiveBFrame": true,
          "Type": "H264Layer",
          "FrameRate": "0/1"
        },
        {
          "Profile": "Auto",
          "Level": "auto",
          "Bitrate": 2250,
          "MaxBitrate": 2250,
          "BufferWindow": "00:00:05",
          "Width": 960,
          "Height": 540,
          "BFrames": 3,
          "ReferenceFrames": 3,
          "AdaptiveBFrame": true,
          "Type": "H264Layer",
          "FrameRate": "0/1"
        },
        {
          "Profile": "Auto",
          "Level": "auto",
          "Bitrate": 1500,
          "MaxBitrate": 1500,
          "BufferWindow": "00:00:05",
          "Width": 960,
          "Height": 540,
          "BFrames": 3,
          "ReferenceFrames": 3,
          "AdaptiveBFrame": true,
          "Type": "H264Layer",
          "FrameRate": "0/1"
        },
        {
          "Profile": "Auto",
          "Level": "auto",
          "Bitrate": 1000,
          "MaxBitrate": 1000,
          "BufferWindow": "00:00:05",
          "Width": 640,
          "Height": 360,
          "BFrames": 3,
          "ReferenceFrames": 3,
          "AdaptiveBFrame": true,
          "Type": "H264Layer",
          "FrameRate": "0/1"
        },
        {
          "Profile": "Auto",
          "Level": "auto",
          "Bitrate": 650,
          "MaxBitrate": 650,
          "BufferWindow": "00:00:05",
          "Width": 640,
          "Height": 360,
          "BFrames": 3,
          "ReferenceFrames": 3,
          "AdaptiveBFrame": true,
          "Type": "H264Layer",
          "FrameRate": "0/1"
        },
        {
          "Profile": "Auto",
          "Level": "auto",
          "Bitrate": 400,
          "MaxBitrate": 400,
          "BufferWindow": "00:00:05",
          "Width": 320,
          "Height": 180,
          "BFrames": 3,
          "ReferenceFrames": 3,
          "AdaptiveBFrame": true,
          "Type": "H264Layer",
          "FrameRate": "0/1"
        }
      ],
      "Type": "H264Video"
    },
    {
      "Profile": "AACLC",
      "Channels": 2,
      "SamplingRate": 48000,
      "Bitrate": 128,
      "Type": "AACAudio"
    }
  ],
  "Outputs": [
    {
      "FileName": "{Basename}_{Width}x{Height}_{VideoBitrate}.mp4",
      "Format": {
        "Type": "MP4Format"
      }
    }
  ]
}

XML 预设XML preset

若要剪裁视频,可以使用此处所述的任何 MES 预设,并修改“Sources” 元素(如下所示)。To trim your videos, you can take any of the MES presets documented here and modify the Sources element (as shown below).

<?xml version="1.0" encoding="utf-16"?>
<Preset xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" Version="1.0" xmlns="https://www.windowsazure.com/media/encoding/Preset/2014/03">
  <Sources>
    <Source StartTime="PT4S" Duration="PT14S"/>
  </Sources>
  <Encoding>
    <H264Video>
      <KeyFrameInterval>00:00:02</KeyFrameInterval>
      <H264Layers>
        <H264Layer>
          <Bitrate>3400</Bitrate>
          <Width>1280</Width>
          <Height>720</Height>
          <FrameRate>0/1</FrameRate>
          <Profile>Auto</Profile>
          <Level>auto</Level>
          <BFrames>3</BFrames>
          <ReferenceFrames>3</ReferenceFrames>
          <Slices>0</Slices>
          <AdaptiveBFrame>true</AdaptiveBFrame>
          <EntropyMode>Cabac</EntropyMode>
          <BufferWindow>00:00:05</BufferWindow>
          <MaxBitrate>3400</MaxBitrate>
        </H264Layer>
        <H264Layer>
          <Bitrate>2250</Bitrate>
          <Width>960</Width>
          <Height>540</Height>
          <FrameRate>0/1</FrameRate>
          <Profile>Auto</Profile>
          <Level>auto</Level>
          <BFrames>3</BFrames>
          <ReferenceFrames>3</ReferenceFrames>
          <Slices>0</Slices>
          <AdaptiveBFrame>true</AdaptiveBFrame>
          <EntropyMode>Cabac</EntropyMode>
          <BufferWindow>00:00:05</BufferWindow>
          <MaxBitrate>2250</MaxBitrate>
        </H264Layer>
        <H264Layer>
          <Bitrate>1500</Bitrate>
          <Width>960</Width>
          <Height>540</Height>
          <FrameRate>0/1</FrameRate>
          <Profile>Auto</Profile>
          <Level>auto</Level>
          <BFrames>3</BFrames>
          <ReferenceFrames>3</ReferenceFrames>
          <Slices>0</Slices>
          <AdaptiveBFrame>true</AdaptiveBFrame>
          <EntropyMode>Cabac</EntropyMode>
          <BufferWindow>00:00:05</BufferWindow>
          <MaxBitrate>1500</MaxBitrate>
        </H264Layer>
        <H264Layer>
          <Bitrate>1000</Bitrate>
          <Width>640</Width>
          <Height>360</Height>
          <FrameRate>0/1</FrameRate>
          <Profile>Auto</Profile>
          <Level>auto</Level>
          <BFrames>3</BFrames>
          <ReferenceFrames>3</ReferenceFrames>
          <Slices>0</Slices>
          <AdaptiveBFrame>true</AdaptiveBFrame>
          <EntropyMode>Cabac</EntropyMode>
          <BufferWindow>00:00:05</BufferWindow>
          <MaxBitrate>1000</MaxBitrate>
        </H264Layer>
        <H264Layer>
          <Bitrate>650</Bitrate>
          <Width>640</Width>
          <Height>360</Height>
          <FrameRate>0/1</FrameRate>
          <Profile>Auto</Profile>
          <Level>auto</Level>
          <BFrames>3</BFrames>
          <ReferenceFrames>3</ReferenceFrames>
          <Slices>0</Slices>
          <AdaptiveBFrame>true</AdaptiveBFrame>
          <EntropyMode>Cabac</EntropyMode>
          <BufferWindow>00:00:05</BufferWindow>
          <MaxBitrate>650</MaxBitrate>
        </H264Layer>
        <H264Layer>
          <Bitrate>400</Bitrate>
          <Width>320</Width>
          <Height>180</Height>
          <FrameRate>0/1</FrameRate>
          <Profile>Auto</Profile>
          <Level>auto</Level>
          <BFrames>3</BFrames>
          <ReferenceFrames>3</ReferenceFrames>
          <Slices>0</Slices>
          <AdaptiveBFrame>true</AdaptiveBFrame>
          <EntropyMode>Cabac</EntropyMode>
          <BufferWindow>00:00:05</BufferWindow>
          <MaxBitrate>400</MaxBitrate>
        </H264Layer>
      </H264Layers>
    </H264Video>
    <AACAudio>
      <Profile>AACLC</Profile>
      <Channels>2</Channels>
      <SamplingRate>48000</SamplingRate>
      <Bitrate>128</Bitrate>
    </AACAudio>
  </Encoding>
  <Outputs>
    <Output FileName="{Basename}_{Width}x{Height}_{VideoBitrate}.mp4">
      <MP4Format />
    </Output>
  </Outputs>
</Preset>

创建覆盖层Create an overlay

Media Encoder Standard 允许在现有视频上覆盖图像。The Media Encoder Standard allows you to overlay an image onto an existing video. 目前支持以下格式:png、jpg、gif 和 bmp。Currently, the following formats are supported: png, jpg, gif, and bmp. 下面定义的预设是视频覆盖层的基本示例。The preset defined below is a basic example of a video overlay.

除了定义预设文件外,还必须让媒体服务知道资产中的哪个文件是覆盖层图像,哪个文件是要在其上覆盖图像的源视频。In addition to defining a preset file, you also have to let Media Services know which file in the asset is the overlay image and which file is the source video onto which you want to overlay the image. 视频文件必须是 文件。The video file has to be the primary file.

如果要使用 .NET,请将以下两个函数添加到此主题 中定义的 .NET 示例。If you are using .NET, add the following two functions to the .NET example defined in this topic. “UploadMediaFilesFromFolder” 函数从文件夹上传文件(例如 BigBuckBunny.mp4 和 Image001.png),并将 mp4 文件设置为资产中的主文件。The UploadMediaFilesFromFolder function uploads files from a folder (for example, BigBuckBunny.mp4 and Image001.png) and sets the mp4 file to be the primary file in the asset. “EncodeWithOverlay” 函数使用传递给它的自定义预设文件(例如,下面的预设)来创建编码任务。The EncodeWithOverlay function uses the custom preset file that was passed to it (for example, the preset that follows) to create the encoding task.

static public IAsset UploadMediaFilesFromFolder(string folderPath)
{
    IAsset asset = _context.Assets.CreateFromFolder(folderPath, AssetCreationOptions.None);

    foreach (var af in asset.AssetFiles)
    {
        // The following code assumes 
        // you have an input folder with one MP4 and one overlay image file.
        if (af.Name.Contains(".mp4"))
            af.IsPrimary = true;
        else
            af.IsPrimary = false;

        af.Update();
    }

    return asset;
}

static public IAsset EncodeWithOverlay(IAsset assetSource, string customPresetFileName)
{
    // Declare a new job.
    IJob job = _context.Jobs.Create("Media Encoder Standard Job");
    // Get a media processor reference, and pass to it the name of the 
    // processor to use for the specific task.
    IMediaProcessor processor = GetLatestMediaProcessorByName("Media Encoder Standard");

    // Load the XML (or JSON) from the local file.
    string configuration = File.ReadAllText(customPresetFileName);

    // Create a task
    ITask task = job.Tasks.AddNew("Media Encoder Standard encoding task",
        processor,
        configuration,
        TaskOptions.None);

    // Specify the input assets to be encoded.
    // This asset contains a source file and an overlay file.
    task.InputAssets.Add(assetSource);

    // Add an output asset to contain the results of the job. 
    task.OutputAssets.AddNew("Output asset",
        AssetCreationOptions.None);

    job.StateChanged += new EventHandler<JobStateChangedEventArgs>(JobStateChanged);
    job.Submit();
    job.GetExecutionProgressTask(CancellationToken.None).Wait();

    return job.OutputMediaAssets[0];
}

Note

当前限制:Current limitations:

不支持覆盖层不透明度设置。The overlay opacity setting is not supported.

源视频文件和覆盖层图像文件必须位于相同的资产中,而且视频文件需要设置为此资产中的主文件。Your source video file and the overlay image file have to be in the same asset, and the video file needs to be set as the primary file in this asset.

JSON 预设JSON preset

{
  "Version": 1.0,
  "Sources": [
    {
      "Streams": [],
      "Filters": {
        "VideoOverlay": {
          "Position": {
            "X": 100,
            "Y": 100,
            "Width": 100,
            "Height": 50
          },
          "AudioGainLevel": 0.0,
          "MediaParams": [
            {
              "OverlayLoopCount": 1
            },
            {
              "IsOverlay": true,
              "OverlayLoopCount": 1
            }
          ],
          "Source": "Image001.png",
          "Clip": {
            "Duration": "00:00:05"
          },
          "FadeInDuration": {
            "Duration": "00:00:01"
          },
          "FadeOutDuration": {
            "StartTime": "00:00:03",
            "Duration": "00:00:04"
          }
        }
      },
      "Pad": true
    }
  ],
  "Codecs": [
    {
      "KeyFrameInterval": "00:00:02",
      "H264Layers": [
        {
          "Profile": "Auto",
          "Level": "auto",
          "Bitrate": 1045,
          "MaxBitrate": 1045,
          "BufferWindow": "00:00:05",
          "ReferenceFrames": 3,
          "EntropyMode": "Cavlc",
          "AdaptiveBFrame": true,
          "Type": "H264Layer",
          "Width": "640",
          "Height": "360",
          "FrameRate": "0/1"
        }
      ],
      "Type": "H264Video"
    },
    {
      "Type": "CopyAudio"
    }
  ],
  "Outputs": [
    {
      "FileName": "{Basename}{Extension}",
      "Format": {
        "Type": "MP4Format"
      }
    }
  ]
}

XML 预设XML preset

<?xml version="1.0" encoding="utf-16"?>
<Preset xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" Version="1.0" xmlns="https://www.windowsazure.com/media/encoding/Preset/2014/03">
  <Sources>
    <Source>
      <Streams />
      <Filters>
        <VideoOverlay>
          <Source>Image001.png</Source>
          <Clip Duration="PT5S" />
          <FadeInDuration Duration="PT1S" />
          <FadeOutDuration StartTime="PT3S" Duration="PT4S" />
          <Position X="100" Y="100" Width="100" Height="50" />
          <Opacity>0</Opacity>
          <AudioGainLevel>0</AudioGainLevel>
          <MediaParams>
            <MediaParam>
              <IsOverlay>false</IsOverlay>
              <OverlayLoopCount>1</OverlayLoopCount>
            </MediaParam>
            <MediaParam>
              <IsOverlay>true</IsOverlay>
              <OverlayLoopCount>1</OverlayLoopCount>
            </MediaParam>
          </MediaParams>
        </VideoOverlay>
      </Filters>
      <Pad>true</Pad>
    </Source>
  </Sources>
  <Encoding>
    <H264Video>
      <KeyFrameInterval>00:00:02</KeyFrameInterval>
      <H264Layers>
        <H264Layer>
          <Bitrate>1045</Bitrate>
          <Width>640</Width>
          <Height>360</Height>
          <FrameRate>0/1</FrameRate>
          <Profile>Auto</Profile>
          <Level>auto</Level>
          <BFrames>0</BFrames>
          <ReferenceFrames>3</ReferenceFrames>
          <Slices>0</Slices>
          <AdaptiveBFrame>true</AdaptiveBFrame>
          <EntropyMode>Cavlc</EntropyMode>
          <BufferWindow>00:00:05</BufferWindow>
          <MaxBitrate>1045</MaxBitrate>
        </H264Layer>
      </H264Layers>
    </H264Video>
    <CopyAudio />
  </Encoding>
  <Outputs>
    <Output FileName="{Basename}{Extension}">
      <MP4Format />
    </Output>
  </Outputs>
</Preset>

在输入不包含音频时插入静音曲目Insert a silent audio track when input has no audio

默认情况下,如果要向编码器发送仅包含视频而不包含音频的输入,则输出资产包含仅有视频数据的文件。By default, if you send an input to the encoder that contains only video, and no audio, then the output asset contains files that contain only video data. 某些播放器可能无法处理此类输出流。Some players may not be able to handle such output streams. 对于这种方案,可以使用此设置来强制编码器将静音曲目添加到输出。You can use this setting to force the encoder to add a silent audio track to the output in that scenario.

若要强制编码器在输入不包含音频时生成包含静音曲目的资产,请指定“InsertSilenceIfNoAudio”值。To force the encoder to produce an asset that contains a silent audio track when input has no audio, specify the "InsertSilenceIfNoAudio" value.

可使用 此部分 中所述的任何 MES 预设,并进行以下修改:You can take any of the MES presets documented in this section, and make the following modification:

JSON 预设JSON preset

{
  "Channels": 2,
  "SamplingRate": 44100,
  "Bitrate": 96,
  "Type": "AACAudio",
  "Condition": "InsertSilenceIfNoAudio"
}

XML 预设XML preset

<AACAudio Condition="InsertSilenceIfNoAudio">
  <Channels>2</Channels>
  <SamplingRate>44100</SamplingRate>
  <Bitrate>96</Bitrate>
</AACAudio>

禁用自动取消隔行扫描Disable auto de-interlacing

如果客户想要将隔行扫描内容自动取消隔行扫描,不需要执行任何操作。Customers don't need to do anything if they like the interlace contents to be automatically de-interlaced. 当自动取消隔行扫描打开(默认设置)时,MES 将自动检测隔行扫描帧,并且只将标记为隔行扫描的帧取消隔行扫描。When the auto de-interlacing is on (default) the MES does the auto detection of interlaced frames and only de-interlaces frames marked as interlaced.

可以关闭自动取消隔行扫描。You can turn the auto de-interlacing off. 但不建议这样做。This option is not recommended.

JSON 预设JSON preset

"Sources": [
{
 "Filters": {
    "Deinterlace": {
      "Mode": "Off"
    }
  },
}
]

XML 预设XML preset

<Sources>
<Source>
  <Filters>
    <Deinterlace>
      <Mode>Off</Mode>
    </Deinterlace>
  </Filters>
</Source>
</Sources>

仅音频预设Audio-only presets

本节演示了两个仅用于音频的 MES 预设:AAC 音频和 AAC 优质音频。This section demonstrates two audio-only MES presets: AAC Audio and AAC Good Quality Audio.

AAC 音频AAC Audio

{
  "Version": 1.0,
  "Codecs": [
    {
      "Profile": "AACLC",
      "Channels": 2,
      "SamplingRate": 48000,
      "Bitrate": 128,
      "Type": "AACAudio"
    }
  ],
  "Outputs": [
    {
      "FileName": "{Basename}_AAC_{AudioBitrate}.mp4",
      "Format": {
        "Type": "MP4Format"
      }
    }
  ]
}

AAC 优质音频AAC Good Quality Audio

{
  "Version": 1.0,
  "Codecs": [
    {
      "Profile": "AACLC",
      "Channels": 2,
      "SamplingRate": 48000,
      "Bitrate": 192,
      "Type": "AACAudio"
    }
  ],
  "Outputs": [
    {
      "FileName": "{Basename}_AAC_{AudioBitrate}.mp4",
      "Format": {
        "Type": "MP4Format"
      }
    }
  ]
}

连接两个或更多个视频文件Concatenate two or more video files

以下示例演示如何生成预设来连接两个或更多个视频文件。The following example illustrates how you can generate a preset to concatenate two or more video files. 最常见的应用场景:你想在主视频中添加标题或预告片。The most common scenario is when you want to add a header or a trailer to the main video. 预期使用场合:当一起编辑的视频文件共享属性(视频分辨率、帧速率、音频曲目计数等)时。The intended use is when the video files being edited together share properties (video resolution, frame rate, audio track count, etc.). 务必注意不要混合使用不同帧速率或不同音轨数的视频。You should take care not to mix videos of different frame rates, or with different number of audio tracks.

Note

串联功能的当前设计要求输入视频剪辑在分辨率、帧率等方面保持一致。The current design of the concatenation feature expects that the input video clips are consistent in terms of resolution, frame rate etc.

要求和注意事项Requirements and considerations

  • 输入视频应只有一个音轨。Input videos should only have one audio track.

  • 输入视频的帧速率应该都相同。Input videos should all have the same frame rate.

  • 必须将视频上传到不同的资产,并将视频设置为每个资产中的主文件。You must upload your videos into separate assets and set the videos as the primary file in each asset.

  • 需要知道视频的持续时间。You need to know the duration of your videos.

  • 以下预设示例假设所有输入视频的起始时间戳都为零。The preset examples below assumes that all the input videos start with a timestamp of zero. 如果视频具有不同的起始时间戳(通常是实时存档的情况),则需要修改 StartTime 值。You need to modify the StartTime values if the videos have different starting timestamp, as is typically the case with live archives.

  • JSON 预设会显式引用输入资产的 AssetID 值。The JSON preset makes explicit references to the AssetID values of the input assets.

  • 示例代码假设 JSON 预设已保存到本地文件(例如“C:\supportFiles\preset.json”)。The sample code assumes that the JSON preset has been saved to a local file, such as "C:\supportFiles\preset.json". 同时假设已通过上传两个视频文件创建了两个资产,并且你知道生成的 AssetID 值。It also assumes that two assets have been created by uploading two video files, and that you know the resultant AssetID values.

  • 代码片段和 JSON 预设显示连接两个视频文件的示例。The code snippet and JSON preset shows an example of concatenating two video files. 可以将其扩展至两个以上的视频,方法是:You can extend it to more than two videos by:

    1. 重复调用 task. InputAssets.Add() 以便依次添加更多视频。Calling task.InputAssets.Add() repeatedly to add more videos, in order.
    2. 通过按相同顺序添加更多条目,对 JSON 中的“Sources”元素进行相应编辑。Making corresponding edits to the "Sources" element in the JSON, by adding more entries, in the same order.

.NET 代码.NET code

IAsset asset1 = _context.Assets.Where(asset => asset.Id == "nb:cid:UUID:606db602-efd7-4436-97b4-c0b867ba195b").FirstOrDefault();
IAsset asset2 = _context.Assets.Where(asset => asset.Id == "nb:cid:UUID:a7e2b90f-0565-4a94-87fe-0a9fa07b9c7e").FirstOrDefault();

// Declare a new job.
IJob job = _context.Jobs.Create("Media Encoder Standard Job for Concatenating Videos");
// Get a media processor reference, and pass to it the name of the
// processor to use for the specific task.
IMediaProcessor processor = GetLatestMediaProcessorByName("Media Encoder Standard");

// Load the XML (or JSON) from the local file.
string configuration = File.ReadAllText(@"c:\supportFiles\preset.json");

// Create a task
ITask task = job.Tasks.AddNew("Media Encoder Standard encoding task",
    processor,
    configuration,
    TaskOptions.None);

// Specify the input videos to be concatenated (in order).
task.InputAssets.Add(asset1);
task.InputAssets.Add(asset2);
// Add an output asset to contain the results of the job.
// This output is specified as AssetCreationOptions.None, which
// means the output asset is not encrypted.
task.OutputAssets.AddNew("Output asset",
    AssetCreationOptions.None);

job.StateChanged += new EventHandler<JobStateChangedEventArgs>(JobStateChanged);
job.Submit();
job.GetExecutionProgressTask(CancellationToken.None).Wait();

JSON 预设JSON preset

使用想连接的资产 ID 以及每个视频的适当时间段,更新自定义预设。Update your custom preset with ids of the assets that you want to concatenate, and with the appropriate time segment for each video.

{
  "Version": 1.0,
  "Sources": [
    {
      "AssetID": "606db602-efd7-4436-97b4-c0b867ba195b",
      "StartTime": "00:00:01",
      "Duration": "00:00:15"
    },
    {
      "AssetID": "a7e2b90f-0565-4a94-87fe-0a9fa07b9c7e",
      "StartTime": "00:00:02",
      "Duration": "00:00:05"
    }
  ],
  "Codecs": [
    {
      "KeyFrameInterval": "00:00:02",
      "SceneChangeDetection": true,
      "H264Layers": [
        {
          "Level": "auto",
          "Bitrate": 1800,
          "MaxBitrate": 1800,
          "BufferWindow": "00:00:05",
          "BFrames": 3,
          "ReferenceFrames": 3,
          "AdaptiveBFrame": true,
          "Type": "H264Layer",
          "Width": "640",
          "Height": "360",
          "FrameRate": "0/1"
        }
      ],
      "Type": "H264Video"
    },
    {
      "Channels": 2,
      "SamplingRate": 48000,
      "Bitrate": 128,
      "Type": "AACAudio"
    }
  ],
  "Outputs": [
    {
      "FileName": "{Basename}_{Width}x{Height}_{VideoBitrate}.mp4",
      "Format": {
        "Type": "MP4Format"
      }
    }
  ]
}

使用 Media Encoder Standard 裁剪视频Crop videos with Media Encoder Standard

请参阅使用 Media Encoder Standard 剪辑视频主题。See the Crop videos with Media Encoder Standard topic.

在输入不包含视频时插入视频轨迹Insert a video track when input has no video

默认情况下,如果要向编码器发送仅包含音频而不包含视频的输入,则输出资产包含仅有音频数据的文件。By default, if you send an input to the encoder that contains only audio, and no video, then the output asset contains files that contain only audio data. 某些播放器(包括 Azure 媒体播放器)(请参阅此处)可能无法处理这样的流。Some players, including Azure Media Player (see this) may not be able to handle such streams. 在该方案中,可使用此设置来强制编码器将单色视频轨道添加到输出。You can use this setting to force the encoder to add a monochrome video track to the output in that scenario.

Note

强制编码器插入输出视频轨迹会增加输出资产的大小,从而增加编码任务的相关成本。Forcing the encoder to insert an output video track increases the size of the output Asset, and thereby the cost incurred for the encoding Task. 应运行测试来验证此成本增加对每月费用的影响不大。You should run tests to verify that this resultant increase has only a modest impact on your monthly charges.

仅以最低比特率插入视频Inserting video at only the lowest bitrate

假设要使用多比特率编码预设(如 “H264 多比特率 720p”)对整个输入目录进行编码以实现流式处理,且输入目录中混合了视频文件和仅音频文件。Suppose you are using a multiple bitrate encoding preset such as "H264 Multiple Bitrate 720p" to encode your entire input catalog for streaming, which contains a mix of video files and audio-only files. 在此方案中,如果输入不包含视频,用户可能想要强制编码器仅以最低比特率插入单色视频轨迹,而不是按每个输出比特率插入视频。In this scenario, when the input has no video, you may want to force the encoder to insert a monochrome video track at just the lowest bitrate, as opposed to inserting video at every output bitrate. 为此,需要使用“InsertBlackIfNoVideoBottomLayerOnly” 标志。To achieve this, you need to use the InsertBlackIfNoVideoBottomLayerOnly flag.

可使用此部分中所述的任何 MES 预设,并进行以下修改:You can take any of the MES presets documented in this section, and make the following modification:

JSON 预设JSON preset

{
      "KeyFrameInterval": "00:00:02",
      "StretchMode": "AutoSize",
      "Condition": "InsertBlackIfNoVideoBottomLayerOnly",
      "H264Layers": [
      …
      ]
}

XML 预设XML preset

使用 XML 时,请使用 Condition="InsertBlackIfNoVideoBottomLayerOnly" 作为“H264Video” 元素的属性,并使用 Condition="InsertSilenceIfNoAudio" 作为“AACAudio” 的属性。When using XML, use Condition="InsertBlackIfNoVideoBottomLayerOnly" as an attribute to the H264Video element and Condition="InsertSilenceIfNoAudio" as an attribute to AACAudio.

. . .
<Encoding>
  <H264Video Condition="InsertBlackIfNoVideoBottomLayerOnly">
    <KeyFrameInterval>00:00:02</KeyFrameInterval>
    <SceneChangeDetection>true</SceneChangeDetection>
    <StretchMode>AutoSize</StretchMode>
    <H264Layers>
      <H264Layer>
        . . .
      </H264Layer>
    </H264Layers>
    <Chapters />
  </H264Video>
  <AACAudio Condition="InsertSilenceIfNoAudio">
    <Profile>AACLC</Profile>
    <Channels>2</Channels>
    <SamplingRate>48000</SamplingRate>
    <Bitrate>128</Bitrate>
  </AACAudio>
</Encoding>
. . .

按所有输出比特率插入视频Inserting video at all output bitrates

假设要使用多比特率编码预设(如 “H264 多比特率 720p”)对整个输入目录进行编码以实现流式处理,且输入目录中混合了视频文件和仅音频文件。Suppose you are using a multiple bitrate encoding preset such as "H264 Multiple Bitrate 720p to encode your entire input catalog for streaming, which contains a mix of video files and audio-only files. 在此方案中,如果输入不包含视频,用户可能想要强制编码器按所有输出比特率插入单色视频轨迹。In this scenario, when the input has no video, you may want to force the encoder to insert a monochrome video track at all the output bitrates. 这可确保对于视频轨迹和音频曲目的数目,输出资产都是同源的。This ensures that your output Assets are all homogenous with respect to number of video tracks and audio tracks. 要实现此目的,需要指定“InsertBlackIfNoVideo”标志。To achieve this, you need to specify the "InsertBlackIfNoVideo" flag.

可使用此部分中所述的任何 MES 预设,并进行以下修改:You can take any of the MES presets documented in this section, and make the following modification:

JSON 预设JSON preset

{
      "KeyFrameInterval": "00:00:02",
      "StretchMode": "AutoSize",
      "Condition": "InsertBlackIfNoVideo",
      "H264Layers": [
      …
      ]
}

XML 预设XML preset

使用 XML 时,请使用 Condition="InsertBlackIfNoVideo" 作为“H264Video” 元素的属性,并使用 Condition="InsertSilenceIfNoAudio" 作为“AACAudio” 的属性。When using XML, use Condition="InsertBlackIfNoVideo" as an attribute to the H264Video element and Condition="InsertSilenceIfNoAudio" as an attribute to AACAudio.

. . .
<Encoding>
  <H264Video Condition="InsertBlackIfNoVideo">
    <KeyFrameInterval>00:00:02</KeyFrameInterval>
    <SceneChangeDetection>true</SceneChangeDetection>
    <StretchMode>AutoSize</StretchMode>
    <H264Layers>
      <H264Layer>
        . . .
      </H264Layer>
    </H264Layers>
    <Chapters />
  </H264Video>
  <AACAudio Condition="InsertSilenceIfNoAudio">
    <Profile>AACLC</Profile>
    <Channels>2</Channels>
    <SamplingRate>48000</SamplingRate>
    <Bitrate>128</Bitrate>
  </AACAudio>
</Encoding>
. . .  

旋转视频Rotate a video

Media Encoder Standard 支持旋转的角度为 0/90/180/270。The Media Encoder Standard supports rotation by angles of 0/90/180/270. 默认行为是“自动”,即尝试在传入的视频文件中检测旋转元数据并对其进行补偿。The default behavior is "Auto", where it tries to detect the rotation metadata in the incoming video file and compensate for it. 将以下“Sources” 元素包含在此部分定义的其中一个预设中:Include the following Sources element to one of the presets defined in this section:

JSON 预设JSON preset

"Sources": [
{
  "Streams": [],
  "Filters": {
    "Rotation": "90"
  }
}
],
"Codecs": [

...

XML 预设XML preset

<Sources>
       <Source>
      <Streams />
      <Filters>
        <Rotation>90</Rotation>
      </Filters>
    </Source>
</Sources>

另请参阅主题,了解有关触发旋转补偿时,编码器如何解释预设中的宽度和高度设置的信息。Also, see this topic for more information on how the encoder interprets the Width and Height settings in the preset, when rotation compensation is triggered.

可使用值“0”向编码器指示忽略输入视频中的旋转元数据(如果有)。You can use the value "0" to indicate to the encoder to ignore rotation metadata, if present, in the input video.

媒体服务学习路径Media Services learning paths

媒体服务 v3(最新版本)Media Services v3 (latest)

查看最新版本的 Azure 媒体服务!Check out the latest version of Azure Media Services!

媒体服务 v2(旧版)Media Services v2 (legacy)

另请参阅See Also

媒体服务编码概述Media Services Encoding Overview