Pages Menu
TwitterRssFacebook

Posted by on Jan 13, 2014 in Development

Streaming MP4 video files in Azure Storage containers (Blob Storage)

Streaming MP4 video files in Azure Storage containers (Blob Storage)

We recently had some problems streaming MP4 video files in a Microsoft Azure storage blob. Hopefully these pointers will help if you’re also struggling:

Get Some Tools

You’ll need some tools as you’ll need to examine and change the HTTP headers of your files, which you can’t do in the Azure Portal. From using them, I can recommend either the Azure Storage Explorer, or the CloudBerry Explorer. Both are free, and both let you download and upload files, as well as set headers. I’ll be using CloudBerry Explorer for the screenshots.

For investigation and diagnostics, you’ll also need Fiddler. If you don’t know how to use Fiddler, it’s definately worth investing some time. It’s an excellent way to start to understand how HTTP traffic works.

Check your Blob Type

Make sure your videos are BlockBlobs. Check the header x-ms-blob-type. There are BlockBlobs and PageBlobs … but for streaming video you want BlockBlobs.

Check your Content Type

Make sure you have a sensible value in the Content-Type header. By default your videos may well have the type application/octet-stream. You should change this to be correct for your media. For MP4 files, use video/mp4.

Check your Storage Version

My what? Yep, this was news to me too. The Azure Blob Storage system is big. Because it has some pretty specific and unusual requirements, Microsoft don’t run it over IIS. Instead, it’s a purpose-built system, designed to deliver content in the most efficient way possible. It’s also in a state of evolution, with new features being added all the time. When it was created, your account was assigned a specific version number, and it will run at that version number until it’s changed. This enables Microsoft to make changes to the infrastructure without breaking everyone’s code each time they change how a feature works.

However, there have been some improvements to how Azure handles streaming of MP4 file – and you will want those changes if you want to get your MP4 files streaming. So, you need to change your account version. You can’t do this in the Azure portal, but here’s how you can programatically:

To get this to work, you’ll also need the Azure.Storage nuget package, which you can install by running  Install-Package WindowsAzure.Storage from the Package Manager console in Visual Studio.

In my sample, I’m updating the account to the “2013-08-15” release, one of the more recent ones. However, for the streaming video improvements, you can use any version number newer than “2012-02-12”.

My account was on version “2009-09-19” – you can also tell quickly by looking at the response headers, looking for the x-ms-version header.

There’s more on Azure Versioning on MSDN: Versioning for the Blob, Queue, and Table Services in Windows Azure.

Check your Videos

If you’ve got this far (and I got this far) then it might be time to examine your videos, to make sure they’re in the best possible shape to support streaming downloads. It seems to make a difference how you choose to play them as to what’s needed and what’s not. For instance, Google Chrome consistently streamed my MP4s without me needing to make any changes, but Windows Media Player (and anything that relied on it, such as embedded video streams) refused to.

I don’t know that much about video file formats, but I did find this blog post by Renaun Erickson which describes a problem with H.264 files where the index for the file (which allows streaming) is placed at the end of the file, meaning you can’t start streaming until you’ve downloaded the whole video! He’s created a small tool, the QTIndexSwapper, which moves this index to the beginning of the file, which you can download from that page as well (requires Adobe Air).

Doing this on my videos made an instant difference for Media Player – which now started streaming the videos correctly.

Caching In

If you’re serving up your videos from a Storage Blob, have you considered a CDN? You’ll be serving your customers from a CDN node that’s best for them (UK guys get served from the UK node, US guys from the US node etc) which will give better performance. Also, if your video content doesn’t change much then you can cache the videos on the CDN. This is good for you because otherwise you’d be paying twice: once to transfer the data to your customers, and once to transfer it from your Storage Blob to the CDN. Fix the cache at a year, and you’ll only pay one lot of transfer charges!

There is no way to clear the Azure CDN Cache!

However… make sure you get everything right BEFORE you set the cache age to its maximum possible 1 year setting. If you’ve forgotten to do something, or you need to change one of your headers, these changes won’t get picked up by the CDN until the cached copy expires. You could be waiting a very long time for your videos to start streaming! Instead, set it to a low value (like a day) whilst you verify everything is OK, then look at increasing it. There is no way to clear the Azure CDN Cache! (I didn’t do this, and now need to rename all my videos…)

You can change the cache expiry by setting the Cache-Control header. For instance, to set the header to 24 hours I use public, max-age=86400:

videoCacheHeader

 

You can check your handiwork in Fiddler, by requesting the video (either using Media Player and watching what Fiddler does, or by using the Composer tab to request it directly). The first time you’ll see the Cache-Control header in the response. In subsequent requests (assuming you’re using the CDN URL) you’ll also start to see the age of the cached copy:

videoCacheResponse

 

Beware of your local cache! When you’re trying to diagnose problems with caches and remote content stores, it really gets in the way! You can clear the cache in Fiddler by clicking the Clear Cache button on the toolbar. I also hold down Ctrl and click it again to delete cookies (just a habit). If you start to see 304 Not Modified response headers – this is your local cache kicking in.

Finally…

I’d not seen this before, but I was recommended the Player Framework to use as the embedded video player. This is a Microsoft Open Source project which has a number of impressive-looking features, including adaptive streaming. I shall certainly be investigating this for the next project we have that involves embedded video: it looks like it takes a lot of the hassle out of serving up video content.

Massive thank you to Vito, who’s a Azure Technical Evangelist and who helped me with a lot of this stuff. 🙂

Written by Tom Morgan

Tom is a Microsoft Teams Platform developer and Microsoft MVP who has been blogging for over a decade. Find out more.
Buy the book: Building and Developing Apps & Bots for Microsoft Teams. Now available to purchase online with free updates.

7 Comments

  1. This helped me quite a lot, thanks.

  2. Great Post.
    Was of huge help!

  3. Great post thanks. A tip for anyone who wants to make changes to video’s in the CDN. You shouldn’t need to rename your videos, you can just add a version to the URL using a query string parameter. So each time you want to change the video you can just append the version in the url. eg https://blob/myvideo.mp4?v=1 then https://blob/myvideo.mp4?v=2.

  4. This is great info, thanks. It might have been even greater if, for the benefit of those starting at square one, you included an example of HTML5 tags for playing the video. I realize there are a lot of ways people are doing this, but…just for sake of completeness.

  5. CORRECT VERSION NOW IS “2015-04-05”;

  6. Is there anyway to do this using a Private Storage Container on Azure? If not what is the best practise around this?

  7. you saved my life thanks still working solution today the 24th November 2017
    I just had to change the version all the rest was already done by the upload on azure storage (mime type, block, and other stuff)

Trackbacks/Pingbacks

  1. Monday, January 13, 2014 on #WindowsAzure | Alexandre Brisebois - […] shared 5 times • thoughtstuff.co.uk Streaming MP4 video in Azure Storage containers (Blob Storage) | thoughtstuff | Tom Morgan…

Post a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.