예제 #1
0
        public PhotoContent CreateTimelapse(int[] photoContentIds, PhotoTimelapseOptions options)
        {
            var photos     = _aquariumDao.GetPhotoContentByIds(photoContentIds);
            var inputFiles = "timelapse/test_%d.jpg";
            var fileName   = "ffmpeg/ffmpeg.exe";
            var output     = $"test.{options.FileType}";

            List <string> fileCleanup = new List <string>();

            try
            {
                if (File.Exists(output))
                {
                    File.Delete(output);
                }

                //Verify directory exists
                var dir = Path.GetDirectoryName(inputFiles);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                //Load all requested images
                var i = 0;
                photos.ForEach(c =>
                {
                    var filePath = inputFiles.Replace("%d", i.ToString());
                    File.WriteAllBytes(filePath, _azureService.GetFileFromStorageContainer(c.Filepath).Result);
                    fileCleanup.Add(filePath);
                    i++;
                });


                //Launch batch process
                var arguments = $"-f image2 -framerate {options.Framerate} -start_number 1 -i {inputFiles} -s {options.Width}x{options.Height} {output}";
                var p         = new Process();
                p.StartInfo.CreateNoWindow         = true;
                p.StartInfo.UseShellExecute        = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError  = true;
                p.StartInfo.FileName  = fileName;
                p.StartInfo.Arguments = arguments;
                p.Start();

                var stdOutput = p.StandardOutput.ReadToEnd();
                var errOutput = p.StandardError.ReadToEnd();
                p.WaitForExit();

                //Verify file exists
                if (!File.Exists(output))
                {
                    throw new Exception($"Command Process Failed with the following output:\n" +
                                        $"Standard Output: {stdOutput}\n" +
                                        $"Error Output: {errOutput}"
                                        );
                }
                else
                {
                    fileCleanup.Add(output);
                }

                //Store the file in azure
                var path    = $"{_config["Photos:Path"]}/timelapse-" + DateTime.UtcNow.Ticks + $".{options.FileType}";
                var content = _aquariumDao.CreatePhotoReference();
                content.Date     = DateTime.UtcNow;
                content.Filepath = path;
                content.Exists   = true;
                _azureService.UploadFileToStorageContainer(File.ReadAllBytes(output), path).Wait();
                if (_azureService.ExistsInStorageContainer(path))
                {
                    _aquariumDao.UpdatePhotoReference(content);
                }

                return(content);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                fileCleanup.ForEach(file =>
                {
                    File.Delete(file);
                });
            }
        }