Ask your technical questions on forums or here :
ASP.NET or MVC | C# | Windows Phone
Microsoft Technology Journals by Abhimanyu K Vatsa
HOME ABOUT RAZOR BOOK MVC ASP.NET JQUERY C# VIDEOS EBOOK ARCHIVE

31 Aug 2016

Azure Media Service Encoding with custom presets

In this post you will learn how to use custom preset for Azure Media Service Encoding. But before that let's look at a case study or issue which I faced.

Introduction

When I uploaded a 55.5 MB mp4 file and encoded with "H264AdaptiveBitrateMP4Set720p" encoder preset, I received following output files:



















Look into green rectangular highlighted video files in the image, this looks good according to input file size. But if you look at red rectangular highlighted video files, these are *improved* files for adaptive streaming, which looks useless if you compare with my example "a dark line on my face in video can't be removed by system automatically...make sense". Here I'm trying to understand Azure Media Services encoding permutations but increasing file size 2-3 times larger than input file is never a acceptable deal.

Why I should pay more for bandwidth and storage on these large files, how I convince my clients?

On this issue I thought to understand this from community so I posted this here, but I'm still not convinced with answer.

Note: What are exact reasons encoder increases the file size, if you come to know please share with me too?

Is there any way I can define not to create such files when scheduling encoding?

Yes, we can use custom preset file and send it with encoding request to Azure Media Service. Let's see how it works in the code:

//// XML Preset
string name = "UploadedVideo-" + Guid.NewGuid().ToString();
IJob job = context.Jobs.Create(name);
IMediaProcessor processor = GetLatestMediaProcessorByName("Media Encoder Standard", context);
string configuration = System.IO.File.ReadAllText(HttpContext.Server.MapPath("~/MediaServicesCustomPreset.xml"));
ITask task = job.Tasks.AddNew(name + "- encoding task", processor, configuration, TaskOptions.None);
task.InputAssets.Add(inputAsset);
task.OutputAssets.AddNew(name + "-Adaptive-Bitrate-MP4", AssetCreationOptions.None);
job.Submit();
IAsset encodedOutputAsset = job.OutputMediaAssets[0];
string smoothStreamingUri = PublishAssetGetURLs(encodedOutputAsset, fileName);
string assetDetails = "MediaServiceFileName:" + encodedOutputAsset.Name + ", MediaServiceContainerUri:" + encodedOutputAsset.Uri + ", AssetId:" + encodedOutputAsset.Id;

Check code in repository here.

Notice blue highlighted code, that's what I added to send custom preset file with encoding request. I will be using xml preset file, you can use json file too. Let's look at the xml file now.

<?xml version="1.0" encoding="utf-16"?>
<Preset xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="1.0" xmlns="http://www.windowsazure.com/media/encoding/Preset/2014/03">
  <Encoding>
    <H264Video>
      <KeyFrameInterval>00:00:02</KeyFrameInterval>
      <H264Layers>
        <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>
      <Chapters />
    </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>

Check code in repository here.

If we do not pass custom preset file to Azure Media Service, then they use their default preset file which has higher bitrates and they don't have better logic to check input file bitrate and based on this decide new bitrates, this is what I understood.

So, I removed higher bitrate encoding from my preset file and this does the work. This has one disadvantage is that, any superb video quality will be loosed because preset is hardcoded for every video file. But, at-least now I have opportunity to write this bitrate dynamically based on video being uploaded through our service. And then send this new bitrate to Azure Media Services. Check the issue log here.

Hope this helps.

2 Jul 2016

Microsoft MVP: Honored to have received the Microsoft MVP award for the 5th year

MVP Again J 1st July 6:30 PM IST has a very special meaning to me, this day starts with full of expectations and fast heartbeats which lasts until the moment of receiving email from Microsoft. Earlier we had a trick to know renewal status even before official confirmation, but this is that hole is closed ;)

So, once again Microsoft awarded me with the MVP (Most Valuable Professional) award for the 5th time in a row in the Visual Studio and Development Technologies (earlier it was ASP.NET/IIS) category.

I'm honored to be the part of Microsoft MVP Program, this is one of the most prestigious award to me.

Here is the body of the mail that I received:



























I'd like to thanks to my family, all friends, Microsoft Indian MVP Group, Biplab Paul (India MVP Program Lead), Gandharv Rawat and my blog readers and followers.

A very-very special thanks to 👩 who is supporting me every day.

Thanks.

17 Jun 2016

Caching in MVC with Donut Caching - excluding caching login

The main purpose of using caching is to dramatically improve the performance of your application. This is nothing but output caching that means whatever you see is cached, and exact similar things is display to everyone.

I recommend you to read Output Caching in MVC post before you read here, because you should be very careful when using any caching mechanism. Or, if you already know output caching, keep reading here only.

Biggest problem you face

If you display user login status on page which you want to cache, then you need to be careful. With output cache attribute [OutputCache(....)] caches everything on page and it will not exclude caching of some portion like login status.

In the situation, the best caching library you should use is Donut Caching (aka Donut Output Caching). Let’s understand its uses.

Using Donut Caching

The best way to add donut caching to your MVC project is to use the NuGet package. From within Visual Studio, select Tools | Library Package Manager and then choose either Package Manager Console or Manage NuGet Packages. Via the console, just type install-package MvcDonutCaching and hit return. From the GUI, just search for MvcDonutCaching and click the install button.

Excluding from being cached

The package adds several overloads to the built-in Html.Action HTML helper. The extra parameter in each overload is named excludeFromParentCache. Set this to true for any action that should not be cached, or should have a different cache duration from the rest of the page.

@Html.Action("Login", "Account", true)

Here Login is a method inside Account controller, you should define this method like:

public class AccountController : Controller
{
    [ChildActionOnly]
    public ActionResult Login()
    {
        return PartialView("_LoginPartial");
    }
}

Cache rest of the page

The package also include a DonutOutputCacheAttribute to be used in place of the built-in OutputCacheAttribute. This attribute is typically placed on every controller action that needs be be cached.

You can either specify a fixed duration:

[DonutOutputCache(Duration = "300")]
public ActionResult Index()
{
    return View();
}

Or, use a cache profile:

[DonutOutputCache(CacheProfile = "TenMins")]
public ActionResult Index()
{
    return View();
}

If you are using cache profiles, be sure to configure the profiles in the web.config. Add the following within the system.web element:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="TenMins" duration="400" varyByParam="*" />
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

You can also configure the output cache to use a custom provider:

<caching>
  <outputCache defaultProvider="DistributedCacheProvider">
    <providers>
      <add name="DistributedCacheProvider" type="DevTrends.Example.DistributedCacheProvider" />
    </providers>
  </outputCache>
</caching>

Note, that a custom provider is not detailed here but you can write one fairly easily by subclassing System.Web.Caching.OutputCacheProvider. A number of implementations are also available on the web.

Hope this helps.

26 Apr 2016

Credential parameter can only be used with Organization ID credentials - Azure Deployment or Continuous Deployment Error

I faced this error while Azure Web App Deployment starts in after Continuous Integration. Here's the detailed information you see in the error:

-Credential parameter can only be used with Organization ID credentials. For more information, please refer to http://go.microsoft.com/fwlink/?linkid=331007&clcid=0x409 for more information about the difference between an organizational account and a Microsoft account. There was an error with the Azure credentials used for deployment.

 Error Screenshot:


If you face this issue, here's quick way:

Step 1

Edit existing build definition and select "Azure Web App Deployment" and then click on "Manage".


Now you will be redirect on new tab.

Step 2

On new tab click on "Update service configuration" and this will open a dialog box where you need to select "Certificate Based" option and then fill up the details. You can click on "Publish settings file" to download, this file will have everything you need to fill here.


Now click on "ok" button to save changes and then fire another build and this will work now.

Hope this helps.

18 Apr 2016

Azure media services transcoding JobState.Error handling

In case you are facing error in scheduled transcoding task like the one which I can see on my dashboard (image give below):


You can use below code snippet to get more details about the error:


Here is the output after using above code snippet:



More details here: https://gist.github.com/itorian/4ac5fa723b3d5267258450a2c1a00294

Hope this helps.

13 Apr 2016

Comparing two List to find match in both list, 1st list only, 2nd list only that is Insersect, Minus and Minus Except

In this code snippet post you will learn how to compare two List<T> using lambda expressions. Below code is simple and self-explanatory if you read:

Output:

10 Mar 2016

Moving SQL Azure to local SQL or Azure SQL to VM SQL

There are many requirements like moving database between:

1. SQL Azure to local development SQL Server
2. SQL Azure to Virtual Machine SQL Server
3. Development SQL Server to Production SQL Server
4. Production SQL Server to Development SQL Server

Rename database data files and logical files in SQL Server

When I rename database using SQL Server Management Studio I always change its data files (mdf, ldf) name as well as logical file name of the resource. And this is very common need for SQL Server guy.

30 Jan 2016

Presenting session on "Building BOT on Telegram using C#" in C# Corner's Delhi Chapter

I will be attending C# Corner Delhi Chapter on 7th Feb again. I have attended this event couple of times before. This time I will be presenting Building BOT on Telegram using C#.

Why should you attend my session?

When I want to attend any session, I try to reason myself about why should I attend any particular session. Well, if you are wondering the same for me, here are my reasons for you.

  • We will have lots of fun learning something completely new
  • This session will encourage you to think out of the box
  • Only OOPS concept is required to attend this session
  • This session will be about BOT
  • This session will be about AI features
  • You will be able to write your BOT to teach, play, search, broadcast, remind, connect, integrate with other services

Date and time

Date: 7 February 2016
Time: 12:30 PM - 01:30 AM (event with other cool sessions starts at 09:30 AM)
Price: Free of cost
Where: H-217, First Floor, Sector 63, Noida, India

You should come and join other speaker's session as well starting at 09:30 AM. Read more details here http://www.c-sharpcorner.com/Events/221/delhi-chapter-meet-learn-net-C-Sharp-6-0-new-features.aspx

Hope to meet you again.


Update (08/02/2016)

Thanks everyone for joining my session. It was great talking to you and hope to see you again. As I promised, here's the source code of Telegram BOT which you can use and extend http://1drv.ms/1mlm5rE. You can implement awesome applications using this open source services from Telegram.

Some of the event pictures:

During my session


Group picture