Reputation: 53
I am trying to get my videos to play properly using Azure Media Services. Videos that are taken in Portrait mode do not play properly, they are not rotated. here is my codes (taken from the Azure Media Services Sample)
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"
In this link: http://azure.microsoft.com/blog/2014/08/21/advanced-encoding-features-in-azure-media-encoder/
It shows that Azure Media Services can rotate automatically with:
<Presets Rotation="Auto">
<Preset
Version="5.0">
But I can't figure out how to incorporate this in my code.
Thanks
Upvotes: 1
Views: 606
Reputation: 53
OK, here is at least one solution that I have been able to dig up:
1) Obtain the XML for the preset you are trying to use. In my case, I was using the present for "H264 Adaptive Bitrate MP4 Set 720p". In my code above, this was in the string "preset".
2) Instead of using this string, use the entire XML file for the preset.
3) Change the xml Presets tag to Presets Rotation="Auto"
Here is link to get the presets (there may be others) https://github.com/AzureMediaServicesSamples/Encoding-Presets/tree/master/VoD/Azure%20Media%20Encoder
Create a new XML file with (I put mine in the root directory of my website -named config1.xml). Here is the XML for my preset:
<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>
Here are the changes in my code from above:
Dim configuration1 As String = File.ReadAllText(Server.MapPath("config1.xml"))
Dim task As ITask = job.Tasks.AddNew(preset + " encoding task", _
latestMediaProcessor, configuration1, _
TaskOptions.None)
Note that you can remove the preset field as it is no longer needed.
Upvotes: 1