static startAndEndOutput GetStartAndEndFrames(List <thumb> input, string fileName, TraceWriter log)
        {
            startAndEndOutput output = new startAndEndOutput();

            ////Wait for download to complete
            //log.Info("Waiting for Download to Complete");
            //do
            //{
            //    //wait
            //} while (!downloadComplete);
            //log.Info("Download Complete");
            //output.startTime = frameRefine((input.OrderBy(a => a.start).First().start), fileName, true, log);
            //output.endTime = frameRefine((input.OrderBy(a => a.end).Last().end), fileName, false, log);
            output.startTime = input.OrderBy(a => a.start).First().start;
            output.endTime   = input.OrderBy(a => a.end).Last().end;

            output.fileName = fileName;

            return(output);
        }
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "GetBrandPosition/{videoID}")] HttpRequestMessage req,
                                                     string videoId,
                                                     Binder binder,
                                                     TraceWriter log,
                                                     ExecutionContext context)
        {
            try
            {
                log.Info("Brand position request recieved.");
                log.Info(string.Format("videoID: {0}", videoId));

                GetSettings(context);
                log.Info("Retrieved Settings");


                var o = await req.Content.ReadAsAsync <ExtractProcessData>();

                //set the TLS level used
                System.Net.ServicePointManager.SecurityProtocol = System.Net.ServicePointManager.SecurityProtocol | System.Net.SecurityProtocolType.Tls12;

                // create the http client
                var handler = new HttpClientHandler();
                handler.AllowAutoRedirect = false;
                var client = new HttpClient(handler);
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);

                // obtain video access token used in subsequent calls
                log.Info($"{apiUrl}/auth/{location}/Accounts/{accountId}/Videos/{videoId}/AccessToken");
                var videoAccessTokenRequestResult = await client.GetAsync($"{apiUrl}/auth/{location}/Accounts/{accountId}/Videos/{videoId}/AccessToken");

                var videoAccessToken = await videoAccessTokenRequestResult.Content.ReadAsStringAsync();

                videoAccessToken = videoAccessToken.Replace("\"", "");

                client.DefaultRequestHeaders.Remove("Ocp-Apim-Subscription-Key");

                // Get video details
                var videoRequestResult = await client.GetAsync($"{apiUrl}/{location}/Accounts/{accountId}/Videos/{videoId}/Index?accessToken={videoAccessToken}");

                var videoResult = await videoRequestResult.Content.ReadAsStringAsync();


                dynamic videoDetails = JsonConvert.DeserializeObject(videoResult);

                // Get video download URL
                var videoDownloadURLResult = await client.GetAsync($"{apiUrl}/{location}/Accounts/{accountId}/Videos/{videoId}/SourceFile/DownloadUrl?accessToken={videoAccessToken}");

                var videoDL = await videoDownloadURLResult.Content.ReadAsStringAsync();

                string videoDowloadURL = JsonConvert.DeserializeObject(videoDL).ToString();

                //log.Info("Download Starting");
                //DownloadFile(videoDowloadURL, videoDetails.name.ToString());

                //Iterate through the results to extract the key frames and thumbnail images to build a list of the thumbs to be analyzed
                List <thumb> thumbs = new List <thumb>();
                foreach (var item in videoDetails.videos)
                {
                    foreach (var shot in item.insights.shots)
                    {
                        foreach (var keyFrame in shot.keyFrames)
                        {
                            foreach (var instance in keyFrame.instances)
                            {
                                thumbs.Add(new thumb
                                {
                                    thumbId = instance.thumbnailId,
                                    image   = GetThumb(instance.thumbnailId.ToString(), videoId, videoAccessToken),
                                    start   = (DateTime)instance.start,
                                    end     = (DateTime)instance.end
                                });
                            }
                        }
                    }
                }

                log.Info("Pulled Thumbs");

                //Invoke the matchThumbs method to run thumbs against custom vision
                List <thumb> predictions = matchThumbs(thumbs, log);
                log.Info("Made Predictions");

                //Write results
                foreach (thumb prediction in predictions)
                {
                    log.Info(message: $"Thumb: {prediction.thumbId} - {prediction.match} - {prediction.probability} Start: {prediction.start.ToString("HH:mm:ss.ffff")}  End: {prediction.end.ToString("HH:mm:ss.ffff")}");
                }

                startAndEndOutput startAndEndFrames = GetStartAndEndFrames(predictions, videoDetails.name.ToString(), log);

                string responseMsg = string.Format("StartFrame: {0}", startAndEndFrames.startTime) + "  "
                                     + string.Format("EndFrame: {0}", startAndEndFrames.endTime);
                log.Info(responseMsg);

                //WriteReadData("tempresults", startAndEndFrames.fileName+"_brandpos.json", JsonConvert.SerializeObject(startAndEndFrames));
                o.startTime = startAndEndFrames.startTime.ToString("HH:mm:ss.ffff");
                o.endTime   = startAndEndFrames.endTime.ToString("HH:mm:ss.ffff");


                //Write output to HTTP as well
                return(videoDetails != null
                ? (ActionResult) new OkObjectResult(JsonConvert.SerializeObject(o))
                : new BadRequestObjectResult("Please pass a video Id on the query string"));
            }
            catch (Exception ex)
            {
                log.Info(ex.Message);
                return(new BadRequestObjectResult(ex.Message));
            }
        }