Detect Motions with Azure Media Analytics


The Azure Media Motion Detector media processor (MP) enables you to efficiently identify sections of interest within an otherwise long and uneventful video. Motion detection can be used on static camera footage to identify sections of the video where motion occurs. It generates a JSON file containing a metadata with timestamps and the bounding region where the event occurred.

Targeted towards security video feeds, this technology is able to categorize motion into relevant events and false positives such as shadows and lighting changes. This allows you to generate security alerts from camera feeds without being spammed with endless irrelevant events, while being able to extract moments of interest from long surveillance videos.

The Azure Media Motion Detector MP is currently in Preview.

This article gives details about Azure Media Motion Detector and shows how to use it with Media Services SDK for .NET

Motion Detector input files

Video files. Currently, the following formats are supported: MP4, MOV, and WMV.

Task configuration (preset)

When creating a task with Azure Media Motion Detector, you must specify a configuration preset.


You can use the following parameters:

Name Options Description Default
sensitivityLevel String:'low', 'medium', 'high' Sets the sensitivity level at which motions are reported. Adjust this to adjust number of false positives. 'medium'
frameSamplingValue Positive integer Sets the frequency at which algorithm runs. 1 equals every frame, 2 means every second frame, and so on. 1
detectLightChange Boolean:'true', 'false' Sets whether light changes are reported in the results 'False'
mergeTimeThreshold Xs-time: Hh:mm:ss
Example: 00:00:03
Specifies the time window between motion events where 2 events are be combined and reported as 1. 00:00:00
detectionZones An array of detection zones:
- Detection Zone is an array of 3 or more points
- Point is an x and y coordinate from 0 to 1.
Describes the list of polygonal detection zones to be used.
Results are reported with the zones as an ID, with the first one being 'id':0
Single zone, which covers the entire frame.

JSON example

      "version": "1.0",
      "options": {
        "sensitivityLevel": "medium",
        "frameSamplingValue": 1,
        "detectLightChange": "False",
        "detectionZones": [
            {"x": 0, "y": 0},
            {"x": 0.5, "y": 0},
            {"x": 0, "y": 1}
            {"x": 0.3, "y": 0.3},
            {"x": 0.55, "y": 0.3},
            {"x": 0.8, "y": 0.3},
            {"x": 0.8, "y": 0.55},
            {"x": 0.8, "y": 0.8},
            {"x": 0.55, "y": 0.8},
            {"x": 0.3, "y": 0.8},
            {"x": 0.3, "y": 0.55}

Motion Detector output files

A motion detection job returns a JSON file in the output asset, which describes the motion alerts, and their categories, within the video. The file contains information about the time and duration of motion detected in the video.

The Motion Detector API provides indicators once there are objects in motion in a fixed background video (for example, a surveillance video). The Motion Detector is trained to reduce false alarms, such as lighting and shadow changes. Current limitations of the algorithms include night vision videos, semi-transparent objects, and small objects.

Elements of the output JSON file


In the latest release, the Output JSON format has changed and may represent a breaking change for some customers.

The following table describes elements of the output JSON file.

Element Description
Version This refers to the version of the Video API. The current version is 2.
Timescale "Ticks" per second of the video.
Offset The time offset for timestamps in "ticks." In version 1.0 of Video APIs, this will always be 0. In future scenarios we support, this value may change.
Framerate Frames per second of the video.
Width, Height Refers to the width and height of the video in pixels.
Start The start timestamp in "ticks".
Duration The length of the event, in "ticks".
Interval The interval of each entry in the event, in "ticks".
Events Each event fragment contains the motion detected within that time duration.
Type In the current version, this is always ‘2’ for generic motion. This label gives Video APIs the flexibility to categorize motion in future versions.
RegionID As explained above, this will always be 0 in this version. This label gives Video API the flexibility to find motion in various regions in future versions.
Regions Refers to the area in your video where you care about motion.

-"id" represents the region area – in this version there is only one, ID 0.
-"type" represents the shape of the region you care about for motion. Currently, "rectangle" and "polygon" are supported.
If you specified "rectangle", the region has dimensions in X, Y, Width, and Height. The X and Y coordinates represent the upper left-hand XY coordinates of the region in a normalized scale of 0.0 to 1.0. The width and height represent the size of the region in a normalized scale of 0.0 to 1.0. In the current version, X, Y, Width, and Height are always fixed at 0, 0 and 1, 1.
If you specified "polygon", the region has dimensions in points.
Fragments The metadata is chunked up into different segments called fragments. Each fragment contains a start, duration, interval number, and event(s). A fragment with no events means that no motion was detected during that start time and duration.
Brackets [] Each bracket represents one interval in the event. Empty brackets for that interval means that no motion was detected.
locations This new entry under events lists the location where the motion occurred. This is more specific than the detection zones.

The following JSON example shows the output:

      "version": 2,
      "timescale": 23976,
      "offset": 0,
      "framerate": 24,
      "width": 1280,
      "height": 720,
      "regions": [
          "id": 0,
          "type": "polygon",
          "points": [{'x': 0, 'y': 0},
            {'x': 0.5, 'y': 0},
            {'x': 0, 'y': 1}]
      "fragments": [
          "start": 0,
          "duration": 226765
          "start": 226765,
          "duration": 47952,
          "interval": 999,
          "events": [
                "type": 2,
                "typeName": "motion",
                "locations": [
                    "x": 0.004184,
                    "y": 0.007463,
                    "width": 0.991667,
                    "height": 0.985185
                "regionId": 0


  • The supported input video formats include MP4, MOV, and WMV.
  • Motion Detection is optimized for stationary background videos. The algorithm focuses on reducing false alarms, such as lighting changes, and shadows.
  • Some motion may not be detected due to technical challenges; for example, night vision videos, semi-transparent objects, and small objects.

.NET sample code

The following program shows how to:

  1. Create an asset and upload a media file into the asset.
  2. Create a job with a video motion detection task based on a configuration file that contains the following json preset:

            "Version": "1.0",
            "Options": {
                "SensitivityLevel": "medium",
                "FrameSamplingValue": 1,
                "DetectLightChange": "False",
                "DetectionZones": [
                    {"x": 0, "y": 0},
                    {"x": 0.5, "y": 0},
                    {"x": 0, "y": 1}
                    {"x": 0.3, "y": 0.3},
                    {"x": 0.55, "y": 0.3},
                    {"x": 0.8, "y": 0.3},
                    {"x": 0.8, "y": 0.55},
                    {"x": 0.8, "y": 0.8},
                    {"x": 0.55, "y": 0.8},
                    {"x": 0.3, "y": 0.8},
                    {"x": 0.3, "y": 0.55}
  3. Download the output JSON files.

Create and configure a Visual Studio project

Set up your development environment and populate the app.config file with connection information, as described in Media Services development with .NET.


using System;
using System.Configuration;
using System.IO;
using System.Linq;
using Microsoft.WindowsAzure.MediaServices.Client;
using System.Threading;
using System.Threading.Tasks;

namespace VideoMotionDetection
    class Program
        // Read values from the App.config file.
        private static readonly string _AADTenantDomain =
        private static readonly string _RESTAPIEndpoint =
        private static readonly string _AMSClientId =
        private static readonly string _AMSClientSecret =

        // Field for service context.
        private static CloudMediaContext _context = null;

        static void Main(string[] args)
            AzureAdTokenCredentials tokenCredentials =
                new AzureAdTokenCredentials(_AADTenantDomain,
                    new AzureAdClientSymmetricKey(_AMSClientId, _AMSClientSecret),

            var tokenProvider = new AzureAdTokenProvider(tokenCredentials);

            _context = new CloudMediaContext(new Uri(_RESTAPIEndpoint), tokenProvider);

            // Run the VideoMotionDetection job.
            var asset = RunVideoMotionDetectionJob(@"C:\supportFiles\VideoMotionDetection\BigBuckBunny.mp4",

            // Download the job output asset.
            DownloadAsset(asset, @"C:\supportFiles\VideoMotionDetection\Output");

        static IAsset RunVideoMotionDetectionJob(string inputMediaFilePath, string configurationFile)
            // Create an asset and upload the input media file to storage.
            IAsset asset = CreateAssetAndUploadSingleFile(inputMediaFilePath,
                "My Video Motion Detection Input Asset",

            // Declare a new job.
            IJob job = _context.Jobs.Create("My Video Motion Detection Job");

            // Get a reference to Azure Media Motion Detector.
            string MediaProcessorName = "Azure Media Motion Detector";

            var processor = GetLatestMediaProcessorByName(MediaProcessorName);

            // Read configuration from the specified file.
            string configuration = File.ReadAllText(configurationFile);

            // Create a task with the encoding details, using a string preset.
            ITask task = job.Tasks.AddNew("My Video Motion Detection Task",

            // Specify the input asset.

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

            // Use the following event handler to check job progress.  
            job.StateChanged += new EventHandler<JobStateChangedEventArgs>(StateChanged);

            // Launch the job.

            // Check job execution and wait for job to finish.
            Task progressJobTask = job.GetExecutionProgressTask(CancellationToken.None);


            // 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)
                ErrorDetail error = job.Tasks.First().ErrorDetails.First();
                Console.WriteLine(string.Format("Error: {0}. {1}",
                return null;

            return job.OutputMediaAssets[0];

        static IAsset CreateAssetAndUploadSingleFile(string filePath, string assetName, AssetCreationOptions options)
            IAsset asset = _context.Assets.Create(assetName, options);

            var assetFile = asset.AssetFiles.Create(Path.GetFileName(filePath));

            return asset;

        static void DownloadAsset(IAsset asset, string outputDirectory)
            foreach (IAssetFile file in asset.AssetFiles)
                file.Download(Path.Combine(outputDirectory, file.Name));

        static IMediaProcessor GetLatestMediaProcessorByName(string mediaProcessorName)
            var processor = _context.MediaProcessors
                .Where(p => p.Name == mediaProcessorName)
                .OrderBy(p => new Version(p.Version))

            if (processor == null)
                throw new ArgumentException(string.Format("Unknown media processor",

            return processor;

        static private void StateChanged(object sender, JobStateChangedEventArgs e)
            Console.WriteLine("Job state changed event:");
            Console.WriteLine("  Previous state: " + e.PreviousState);
            Console.WriteLine("  Current state: " + e.CurrentState);

            switch (e.CurrentState)
                case JobState.Finished:
                    Console.WriteLine("Job is finished.");
                case JobState.Canceling:
                case JobState.Queued:
                case JobState.Scheduled:
                case JobState.Processing:
                    Console.WriteLine("Please wait...\n");
                case JobState.Canceled:
                case JobState.Error:
                    // Cast sender as a job.
                    IJob job = (IJob)sender;
                    // Display or log error details as needed.
                    // LogJobStop(job.Id);

Media Services learning paths

Read about the Azure Media Services learning paths:

Provide feedback

Use the User Voice forum to provide feedback and make suggestions on how to improve Azure Media Services. You also can go directly to one of the following categories:

Azure Media Services Motion Detector blog

Azure Media Services Analytics Overview

Azure Media Analytics demos