Azure媒体服务视频轮播

时间:2015-07-23 11:11:25

标签: azure media azure-media-services

我正在尝试使用Azure Media Services正常播放视频。在人像模式下拍摄的视频无法正常播放,不会旋转。这是我的代码(取自Azure媒体服务示例)

 Dim filenamewithpath As String = Server.MapPath("." & "\video2.mp4")
        Dim filename As String = Path.GetFileName(filenamewithpath)
        Dim preset As String = "H264 Adaptive Bitrate MP4 Set 720p"
        FileVideo.SaveAs(filenamewithpath)

        ' Create and cache the Media Services credentials in a static class variable.

        Dim assetName = "UploadSingleFile_" + DateTime.UtcNow.ToString()
        Dim asset As IAsset = _context.Assets.Create(assetName, AssetCreationOptions.None)
        Dim assetFile = asset.AssetFiles.Create(filename)
        Dim accessPolicy As IAccessPolicy = _context.AccessPolicies.Create(assetName, TimeSpan.FromDays(30), _
                AccessPermissions.Write Or AccessPermissions.List)
        Dim locator As ILocator = _context.Locators.CreateLocator(LocatorType.Sas, asset, accessPolicy)
        assetFile.Upload(filenamewithpath)

        ' Declare a new job.
        Dim job As IJob = _context.Jobs.Create(preset + " encoding job")
        Dim mediaProcessors = _context.MediaProcessors.Where(Function(p) p.Name.Contains("Media Encoder")).ToList()

        Dim latestMediaProcessor = mediaProcessors.OrderBy(Function(mp) New Version(mp.Version)).LastOrDefault()


        ' Create a task with the encoding details, using a string preset.
        Dim task As ITask = job.Tasks.AddNew(preset + " encoding task", latestMediaProcessor, preset, Microsoft.WindowsAzure.MediaServices.Client.TaskOptions.ProtectedConfiguration)

        ' Specify the input asset to be encoded.
        task.InputAssets.Add(asset)
        ' Add an output asset to contain the results of the job. 
        task.OutputAssets.AddNew("Output asset", AssetCreationOptions.None)
        job.Submit()


        ' Check job execution and wait for job to finish. 
        Dim progressJobTask As Task = job.GetExecutionProgressTask(CancellationToken.None)
        progressJobTask.Wait()

        ' If job state is Error the event handling 
        ' method for job progress should log errors.  Here we check 
        ' for error state and exit if needed.
        If job.State = JobState.[Error] Then
            Throw New Exception(vbLf & "Exiting method due to job error.")
        End If

        Dim MP4Asset As IAsset = job.OutputMediaAssets(0)
        ' BuildSasUrlForMP4File creates a SAS Locator
        ' and builds the SAS Url that can be used to 
        ' progressively download the MP4 file.
        Dim fullSASURL As String = BuildSasUrlForMP4File(MP4Asset)
        Dim streamingURL As String _
            = GetStreamingOriginLocatorURL(MP4Asset)
        fullSASURL = "http://aka.ms/azuremediaplayeriframe?url=" &  fullSASURL _
            & "&autoplay=false"

在此链接中: http://azure.microsoft.com/blog/2014/08/21/advanced-encoding-features-in-azure-media-encoder/

它表明Azure Media Services可以自动旋转:

<Presets Rotation="Auto">
 <Preset
   Version="5.0">

但我无法弄清楚如何在我的代码中加入它。

由于

1 个答案:

答案 0 :(得分:1)

好的,这里至少有一个我能够挖掘的解决方案:

1)获取您尝试使用的预设的XML。就我而言,我使用的是“H264 Adaptive Bitrate MP4 Set 720p”。在上面的代码中,这是在字符串“预设”中。

2)使用整个XML文件代替使用此字符串。

3)将xml Presets标签更改为Presets Rotation =“Auto”

这是获取预设的链接(可能还有其他) https://github.com/AzureMediaServicesSamples/Encoding-Presets/tree/master/VoD/Azure%20Media%20Encoder

创建一个新的XML文件(我把它放在我的网站的根目录-named config1.xml中)。这是我预设的XML:

<Presets Rotation="Auto">
 <Preset
   Version="5.0">
   <Job />
   <MediaFile
     DeinterlaceMode="AutoPixelAdaptive"
     ResizeQuality="Super"
     AudioGainLevel="1"
     VideoResizeMode="Stretch">
     <Metadata
       MergeCollection="True">
       <Item
         Name="WM/EncodedBy"
         Value="Azure Media Encoder 4 - H264 Adaptive Bitrate MP4 Set 720p, 07/30/2014 " />
     </Metadata>    
     <OutputFormat>
       <MP4OutputFormat
         StreamCompatibility="Standard">
         <VideoProfile>
           <MainH264VideoProfile
             BFrameCount="3"
             EntropyMode="Cabac"
             RDOptimizationMode="Speed"
             HadamardTransform="False"
             SubBlockMotionSearchMode="Speed"
             MultiReferenceMotionSearchMode="Balanced"
             ReferenceBFrames="False"
             AdaptiveBFrames="False"
             SceneChangeDetector="False"
             FastIntraDecisions="False"
             FastInterDecisions="False"
             SubPixelMode="Quarter"
             SliceCount="0"
             KeyFrameDistance="00:00:02"
             InLoopFilter="True"
             MEPartitionLevel="EightByEight"
             ReferenceFrames="4"
             SearchRange="64"
             AutoFit="True"
             Force16Pixels="False"
             FrameRate="0"
             SeparateFilesPerStream="True"
             SmoothStreaming="False"
             NumberOfEncoderThreads="0">
             <Streams
               AutoSize="False"
               FreezeSort="False">
               <StreamInfo
                 Size="1280, 720">
                 <Bitrate>
                   <ConstantBitrate
                     Bitrate="3400"
                     IsTwoPass="False"
                     BufferWindow="00:00:05" />
                 </Bitrate>
               </StreamInfo>
               <StreamInfo
                 Size="960, 540">
                 <Bitrate>
                   <ConstantBitrate
                     Bitrate="2250"
                     IsTwoPass="False"
                     BufferWindow="00:00:05" />
                 </Bitrate>
               </StreamInfo>
               <StreamInfo
                 Size="960, 540">
                 <Bitrate>
                   <ConstantBitrate
                     Bitrate="1500"
                     IsTwoPass="False"
                     BufferWindow="00:00:05" />
                 </Bitrate>
               </StreamInfo>
               <StreamInfo
                 Size="640, 360">
                 <Bitrate>
                   <ConstantBitrate
                     Bitrate="1000"
                     IsTwoPass="False"
                     BufferWindow="00:00:05" />
                 </Bitrate>
               </StreamInfo>
               <StreamInfo
                 Size="640, 360">
                 <Bitrate>
                   <ConstantBitrate
                     Bitrate="650"
                     IsTwoPass="False"
                     BufferWindow="00:00:05" />
                 </Bitrate>
               </StreamInfo>              
               <StreamInfo
                 Size="320, 180">
                 <Bitrate>
                   <ConstantBitrate
                     Bitrate="400"
                     IsTwoPass="False"
                     BufferWindow="00:00:05" />
                 </Bitrate>
               </StreamInfo>                          
             </Streams>
           </MainH264VideoProfile>
         </VideoProfile>
         <AudioProfile>
           <AacAudioProfile
             Codec="AAC"
             Channels="2"
             BitsPerSample="16"
             SamplesPerSecond="44100">
             <Bitrate>
               <ConstantBitrate
                 Bitrate="96"
                 IsTwoPass="False"
                 BufferWindow="00:00:00" />
             </Bitrate>
           </AacAudioProfile>
         </AudioProfile>
       </MP4OutputFormat>
     </OutputFormat>
   </MediaFile>
 </Preset>
  <Preset
  Version="5.0">
    <Job />
    <MediaFile
      AudioGainLevel="1">
      <Metadata
        MergeCollection="True">
        <Item
          Name="WM/EncodedBy"
          Value="Azure Media Encoder 3 - H264 Adaptive Bitrate MP4 Set 720p, 07/30/2014 " />
      </Metadata>
      <OutputFormat>
        <MP4OutputFormat
          StreamCompatibility="Standard">
          <AudioProfile>
            <AacAudioProfile
              Codec="AAC"
              Channels="2"
              BitsPerSample="16"
              SamplesPerSecond="44100">
              <Bitrate>
                <ConstantBitrate
                  Bitrate="96"
                  IsTwoPass="False"
                  BufferWindow="00:00:00" />
              </Bitrate>
            </AacAudioProfile>
          </AudioProfile>
        </MP4OutputFormat>
      </OutputFormat>
    </MediaFile>
  </Preset>
  <Preset
   Version="5.0">
   <Job />
   <MediaFile
     AudioGainLevel="1">
     <Metadata
       MergeCollection="True">
       <Item
         Name="WM/EncodedBy"
         Value="Azure Media Encoder 3 - H264 Adaptive Bitrate MP4 Set 720p, 07/30/2014 " />
     </Metadata>    
     <OutputFormat>
       <MP4OutputFormat
         StreamCompatibility="Standard">
         <AudioProfile>
           <AacAudioProfile
             Codec="AAC"
             Channels="2"
             BitsPerSample="16"
             SamplesPerSecond="44100">
             <Bitrate>
               <ConstantBitrate
                 Bitrate="56"
                 IsTwoPass="False"
                 BufferWindow="00:00:00" />
             </Bitrate>
           </AacAudioProfile>
         </AudioProfile>
       </MP4OutputFormat>
     </OutputFormat>
   </MediaFile>
 </Preset>
</Presets>

以下是我的代码中的更改:

 Dim configuration1 As String = File.ReadAllText(Server.MapPath("config1.xml"))
 Dim task As ITask = job.Tasks.AddNew(preset + " encoding task", _
               latestMediaProcessor, configuration1, _
              TaskOptions.None)

请注意,您可以删除不再需要的预设字段。