static int Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("MP4ToHLS <asset-id>");
                return -1;
            }

            string accountName = Environment.GetEnvironmentVariable("ACCOUNT_NAME");
            string accountKey = Environment.GetEnvironmentVariable("ACCOUNT_KEY");
            CloudMediaContext cloudMediaContext = new CloudMediaContext(accountName, accountKey);

            string assetId = args[0];
            IAsset asset = cloudMediaContext.FindAssetById(assetId);

            IJob job = cloudMediaContext.Jobs.Create(string.Format("Convert {0} to HLS Asset", asset.Name));

            IMediaProcessor processor = cloudMediaContext.GetMediaProcessor("Windows Azure Media Encoder");

            ITask task = job.Tasks.AddNew("MP4 to HLS",
                processor,
                "H264 Adaptive Bitrate MP4 Set SD 16x9",
                Microsoft.WindowsAzure.MediaServices.Client.TaskOptions.ProtectedConfiguration);

            task.InputAssets.Add(asset);

            task.OutputAssets.AddNew(string.Format("HLS for {0}", asset.Name),
                AssetCreationOptions.None);

            job.Submit();

            Console.WriteLine(job.Id);
            Console.WriteLine("Once job is complete, retrieve StreamingURL and append (format=m3u8-aapl)");
            return 0;
        }
        static int Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.Error.WriteLine("AddFile <AssetId> <filename>, Adds a file to an existing asset");
                return -1;
            }

            string accountName = Environment.GetEnvironmentVariable("ACCOUNT_NAME");
            string accountKey = Environment.GetEnvironmentVariable("ACCOUNT_KEY");
            CloudMediaContext cloudMediaContext = new CloudMediaContext(accountName, accountKey);

            string assetId = args[0];
            IAsset asset = cloudMediaContext.FindAssetById(assetId);

            string fileName = args[1];

            // create the AssetFile with the name as the file being uploaded.
            // This is required by the service
            var assetFile = asset.AssetFiles.Create(System.IO.Path.GetFileName(fileName));

            assetFile.Upload(fileName);

            return 0;
        }
        static int Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("DownloadAsset <asset-id> - downloads all asset files.");
                return -1;
            }

            string accountName = Environment.GetEnvironmentVariable("ACCOUNT_NAME");
            string accountKey = Environment.GetEnvironmentVariable("ACCOUNT_KEY");
            CloudMediaContext cloudMediaContext = new CloudMediaContext(accountName, accountKey);

            string assetId = args[0];
            IAsset asset = cloudMediaContext.FindAssetById(assetId);

            string folder = asset.Id.ToString().Replace(":", "");

            Directory.CreateDirectory(folder);

            foreach (var file in asset.AssetFiles)
            {
                file.Download(string.Format(@"{0}\{1}" ,folder ,file.Name));
            }

            return 0;
        }
        static int Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("MP4ToSmoothStream <asset-id>");
                return -1;
            }

            string accountName = Environment.GetEnvironmentVariable("ACCOUNT_NAME");
            string accountKey = Environment.GetEnvironmentVariable("ACCOUNT_KEY");
            CloudMediaContext cloudMediaContext = new CloudMediaContext(accountName, accountKey);

            string assetId = args[0];
            IAsset asset = cloudMediaContext.FindAssetById(assetId);

            IJob job = cloudMediaContext.Jobs.Create(string.Format("Convert {0} to Smooth Stream", asset.Name));

            IMediaProcessor processor = cloudMediaContext.GetMediaProcessor("Windows Azure Media Encoder");

            ITask task = job.Tasks.AddNew("MP4 to Smooth Stream Conversion",
                processor,
                "H264 Smooth Streaming 720p", // Task Preset. Amend as required.
                Microsoft.WindowsAzure.MediaServices.Client.TaskOptions.ProtectedConfiguration);

            task.InputAssets.Add(asset);

            task.OutputAssets.AddNew(string.Format("Smooth Stream for {0}", asset.Name),
                AssetCreationOptions.None);

            job.Submit();

            Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", job.Id, job.Name, job.State, job.RunningDuration, job.LastModified);

            return 0;
        }
        static int Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("DecryptAsset <asset-id>");
                return -1;
            }

            string accountName = Environment.GetEnvironmentVariable("ACCOUNT_NAME");
            string accountKey = Environment.GetEnvironmentVariable("ACCOUNT_KEY");
            CloudMediaContext cloudMediaContext = new CloudMediaContext(accountName, accountKey);

            string assetId = args[0];
            IAsset asset = cloudMediaContext.FindAssetById(assetId);

            IJob job = cloudMediaContext.Jobs.Create(string.Format("Decrypt {0}", asset.Name));

            IMediaProcessor decryptProcessor = cloudMediaContext.GetMediaProcessor("Storage Decryption");

            ITask decryptTask = job.Tasks.AddNew(string.Format("Decrypt {0}", asset.Name),
                    decryptProcessor,string.Empty,
                    Microsoft.WindowsAzure.MediaServices.Client.TaskOptions.None);

            decryptTask.InputAssets.Add(asset);

            decryptTask.OutputAssets.AddNew(string.Format("{0} decrypted", asset.Name),
                AssetCreationOptions.None);

            job.Submit();

            Console.WriteLine(job.Id);

            return 0;
        }
        /// <summary>
        /// Creates an Origin locator for a streaming asset and creates
        /// an HTML file that wraps a Silverlight Player to play it.
        /// </summary>
        static int Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("MakePlayer <streaming-asset-id>");
                return -1;
            }

            string accountName = Environment.GetEnvironmentVariable("ACCOUNT_NAME");
            string accountKey = Environment.GetEnvironmentVariable("ACCOUNT_KEY");
            CloudMediaContext cloudMediaContext = new CloudMediaContext(accountName, accountKey);

            string assetId = args[0];

            IAsset asset = cloudMediaContext.FindAssetById(assetId);

            var manifest = from f in asset.AssetFiles
                           where f.Name.EndsWith(".ism")
                           select f;

            var manifestFile = manifest.First();

            IAccessPolicy streamingPolicy = cloudMediaContext.AccessPolicies.Create("Streaming policy",
                TimeSpan.FromDays(1),
                AccessPermissions.Read);

            ILocator originLocator = cloudMediaContext.Locators.CreateLocator(LocatorType.OnDemandOrigin,asset,
                streamingPolicy);

            string urlForClientStreaming = originLocator.Path + manifestFile.Name + "/manifest";

            string buffer = File.ReadAllText("..\\SmoothStreamingPlayer\\SmoothStreamingPlayer.html");

            buffer = buffer.Replace("http://streams.smooth.vertigo.com/elephantsdream/Elephants_Dream_1024-h264-st-aac.ism/manifest",
                urlForClientStreaming);

            string filename = string.Format("..\\SmoothStreamingPlayer\\{0}.html", asset.Id.Replace(":", ""));

            File.WriteAllText(filename, buffer);

            Console.WriteLine(filename);

            return 0;
        }
        static int Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("DeleteAsset <asset-id>");
                return -1;
            }

            string accountName = Environment.GetEnvironmentVariable("ACCOUNT_NAME");
            string accountKey = Environment.GetEnvironmentVariable("ACCOUNT_KEY");
            CloudMediaContext cloudMediaContext = new CloudMediaContext(accountName, accountKey);

            string assetId = args[0];
            IAsset asset = cloudMediaContext.FindAssetById(assetId);

            asset.Delete();

            return 0;
        }
        static int Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("Thumbnail <asset-id>");
                return -1;
            }

            string accountName = Environment.GetEnvironmentVariable("ACCOUNT_NAME");
            string accountKey = Environment.GetEnvironmentVariable("ACCOUNT_KEY");
            CloudMediaContext cloudMediaContext = new CloudMediaContext(accountName, accountKey);

            string assetId = args[0];
            IAsset asset = cloudMediaContext.FindAssetById(assetId);

            IJob job = cloudMediaContext.CreateThumbnails(asset);

            Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", job.Id, job.Name, job.State, job.RunningDuration, job.LastModified);

            return 0;
        }
        static int Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("AssetFiles <asset-id>");
                return -1;
            }

            string accountName = Environment.GetEnvironmentVariable("ACCOUNT_NAME");
            string accountKey = Environment.GetEnvironmentVariable("ACCOUNT_KEY");
            CloudMediaContext cloudMediaContext = new CloudMediaContext(accountName, accountKey);

            string assetId = args[0];
            IAsset asset = cloudMediaContext.FindAssetById(assetId);

            foreach (var file in asset.AssetFiles)
            {
                Console.WriteLine("{0}\t{1}\t{2}", file.Id, file.Name, file.IsEncrypted);
            }

            return 0;
        }
        static int Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("ToMP4 <asset-id>");
                return -1;
            }

            string accountName = Environment.GetEnvironmentVariable("ACCOUNT_NAME");
            string accountKey = Environment.GetEnvironmentVariable("ACCOUNT_KEY");
            CloudMediaContext cloudMediaContext = new CloudMediaContext(accountName, accountKey);

            string assetId = args[0];
            IAsset asset = cloudMediaContext.FindAssetById(assetId);

            IJob job = cloudMediaContext.Jobs.Create(string.Format("Convert {0} to MP4", asset.Name));

            IMediaProcessor processor = cloudMediaContext.GetMediaProcessor("Windows Azure Media Encoder");

            ITask task = job.Tasks.AddNew("MP4 Conversion",
                processor,
                //"H.264 256k DSL CBR",
                "H264 Adaptive Bitrate MP4 Set 720p",
                Microsoft.WindowsAzure.MediaServices.Client.TaskOptions.None);

            task.InputAssets.Add(asset);

            task.OutputAssets.AddNew(string.Format("MP4 for {0}", asset.Name),
                AssetCreationOptions.None);

            job.Submit();

            Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", job.Id, job.Name, job.State, job.RunningDuration, job.LastModified);

            return 0;
        }
        static int Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.Error.WriteLine("RunTask <AssetId> <task-preset-file>");
                return -1;
            }

            string accountName = Environment.GetEnvironmentVariable("ACCOUNT_NAME");
            string accountKey = Environment.GetEnvironmentVariable("ACCOUNT_KEY");
            CloudMediaContext cloudMediaContext = new CloudMediaContext(accountName, accountKey);

            string assetId = args[0];
            IAsset asset = cloudMediaContext.FindAssetById(assetId);

            IMediaProcessor processor = null;

                Console.WriteLine("Choose Media processor: ");
                try
                {
                    Dictionary<int, IMediaProcessor> processors = new Dictionary<int, IMediaProcessor>();
                    var procs = cloudMediaContext.MediaProcessors.ToList();
                    int i = 1;
                    foreach (var proc in procs)
                    {
                        Console.WriteLine("{0}. {1} \n      with id: {1}", i, proc.Name, proc.Id);
                        Console.WriteLine();
                        processors.Add(i, proc);
                        i++;
                    }
                    Console.Write("Enter [1..n]:");
                    var key = Console.ReadKey();
                    Console.WriteLine();
                    if (char.IsDigit(key.KeyChar))
                    {
                        int result = key.KeyChar - '0';
                        processor = processors[result];
                    }

                }
                catch
                {
                    Console.WriteLine("Could not get processor(s)!");
                    return -1;
                }

                if (processor == null)
                {
                    Console.WriteLine("Invalid choice");
                    return -1;
                }

            string filename = args[1];
            IJob job = cloudMediaContext.Jobs.Create("Run Task");

            string configuration = File.ReadAllText(Path.GetFullPath(filename));

            ITask task = job.Tasks.AddNew("My Task",
                processor,
                configuration,
               Microsoft.WindowsAzure.MediaServices.Client.TaskOptions.None);

            // Specify the input asset to be encoded.
            task.InputAssets.Add(asset);
            // Add an output asset to contain the results of the job. Since the
            // asset is already protected with PlayReady, we won't encrypt.
            task.OutputAssets.AddNew("Output asset",
                AssetCreationOptions.None);

            // Launch the job.
            job.Submit();

            Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", job.Id, job.Name, job.State, job.RunningDuration, job.LastModified);

            return 0;
        }