private Task <byte[]> CreateThumbnailImpl(Title title, int position)
        {
            return(Task.Run(async() =>
            {
                var time = TimeSpan.FromSeconds(position);
                var inputFilePath = TitleEnumerationService.GetUncPath(title.Location);
                var getThumbnailResult = await _mediaToolkitService.ExecuteAsync(new FfTaskGetThumbnail(inputFilePath,
                                                                                                        new GetThumbnailOptions()
                {
                    SeekSpan = time,
                    OutputFormat = OutputFormat.Image2,
                    PixelFormat = PixelFormat.Rgba,
                }));

                var fs = new MemoryStream();
                using (var img = new MagickImage(getThumbnailResult.ThumbnailData))
                {
                    img.Resize(512, 0);
                    await img.WriteAsync(fs);
                }

                fs.Position = 0;
                return fs.ToArray();
            }));
        }
        private async Task SyncDirectory(string path)
        {
            var generes = _fileSystem.Directory.EnumerateDirectories(path);

            _db.Database.Connect();
            foreach (var genreLocation in generes)
            {
                var genre = _db.Query().Select.Table <Genre>()
                            .Where
                            .Column(f => f.Location).Is.EqualsTo(genreLocation)
                            .FirstOrDefault();

                if (genre == null)
                {
                    genre          = new Genre();
                    genre.Location = genreLocation;
                    genre.Name     = _fileSystem.Path.GetFileName(genreLocation);
                    genre          = _db.InsertWithSelect(genre);
                    _logger.LogInformation("Created Genre " + genre.Name);
                }

                var array = _fileSystem.Directory.EnumerateDirectories(genreLocation)
                            .OrderBy(e => _fileSystem.DirectoryInfo.FromDirectoryName(e).LastWriteTime)
                            .ToArray();

                var seasons = new List <Season>();
                for (var i = 0; i < array.Length; i++)
                {
                    var seasonsLocation = array[i];
                    var season          = _db.Query().Select.Table <Season>()
                                          .Where
                                          .Column(f => f.Location).Is.EqualsTo(seasonsLocation)
                                          .FirstOrDefault();
                    if (season == null)
                    {
                        season          = new Season();
                        season.Location = seasonsLocation;
                        season.Name     = _fileSystem.Path.GetFileName(seasonsLocation);
                        season.IdGenre  = genre.GenreId;
                        var lastSeason = _db.Query().Select.Table <Season>()
                                         .Where
                                         .Column(f => f.IdGenre).Is.EqualsTo(genre.GenreId)
                                         .Order.By(f => f.OrderNo).Descending
                                         .LimitBy(1)
                                         .FirstOrDefault();
                        season.OrderNo     = (lastSeason?.OrderNo ?? -1) + 1;
                        seasons.Add(season = _db.InsertWithSelect(season));
                        _logger.LogInformation("\t Created Season " + season.Name);
                    }
                    else
                    {
                        seasons.Add(season);
                    }

                    var titles = _fileSystem.Directory.EnumerateFiles(seasonsLocation, "*.m4v")
                                 .OrderBy(e => _fileSystem.FileInfo.FromFileName(e).LastWriteTime)
                                 .ToArray();
                    var titleEntites = new List <Title>();
                    for (var index = 0; index < titles.Length; index++)
                    {
                        var titleLocation = titles[index];
                        var title         = _db.Query().Select.Table <Title>()
                                            .Where.Column(f => f.Location).Is.EqualsTo(titleLocation)
                                            .FirstOrDefault();
                        if (title == null)
                        {
                            title          = new Title();
                            title.Location = titleLocation;
                            title.Name     = _fileSystem.Path.GetFileNameWithoutExtension(title.Location);
                            title.OrderNo  = index;
                            var lastTitle = _db.Query().Select.Table <Title>()
                                            .Where
                                            .Column(f => f.IdSeason).Is.EqualsTo(season.SeasonId)
                                            .Order.By(f => f.OrderNo).Descending
                                            .LimitBy(1)
                                            .FirstOrDefault();
                            season.OrderNo       = (lastTitle?.OrderNo ?? -1) + 1;
                            title.IdSeason       = season.SeasonId;
                            title.PlaybackLength = 0;

                            try
                            {
                                var metaData = await _mediaToolkitService.ExecuteAsync(new FfTaskGetMetadata(title.Location));

                                var streamData = metaData.Metadata.Streams.FirstOrDefault();
                                if (streamData != null)
                                {
                                    title.PlaybackLength = (int)double.Parse(streamData?.Duration, CultureInfo.InvariantCulture);
                                }
                            }
                            catch (Exception e)
                            {
                                _logger.LogError(e, "Could not obtain Metadata or Thumbnail from title");
                                continue;
                            }

                            titleEntites.Add(_db.InsertWithSelect(title));
                            _logger.LogInformation("\t\t Created Title " + title.Name);
                        }
                        else
                        {
                            titleEntites.Add(title);
                        }
                    }

                    _db.Query().Update.Table <Season>()
                    .Set
                    .Column(f => f.PlaybackLength).Value(titleEntites.Select(f => f.PlaybackLength ?? 0).Sum())
                    .Where
                    .PrimaryKey().Is.EqualsTo(season.SeasonId)
                    .ExecuteNonQuery();
                }


                _db.Query().Update.Table <Genre>()
                .Set
                .Column(f => f.PlaybackLength).Value(seasons.Select(f => f.PlaybackLength ?? 0).Sum())
                .Where
                .PrimaryKey().Is.EqualsTo(genre.GenreId)
                .ExecuteNonQuery();
            }

            _db.Database.CloseConnection();
        }
Ejemplo n.º 3
0
        private async ValueTask <(string, string, List <List <Predict> >)> ManipulateVideo(string file)
        {
            string pathLow                   = null;
            string pathOverlay               = null;
            string pathOverlayTemp           = null;
            List <List <Predict> > predicted = null;

            try
            {
                var absolutePathToFile = Path.Combine(_env.WebRootPath, file);

                var fileName = file.Split('\\')[1];
                var fileExt  = fileName.Split('.');

                pathLow = Path.Combine(default_Path, $"{Rename()}.{fileExt[1]}");
                var absolutePathNew = Path.Combine(_env.WebRootPath, pathLow);

                pathOverlayTemp = Path.Combine(default_Path, $"{Rename()}.{fileExt[1]}");
                var absolutePathNewOverlayTemp = Path.Combine(_env.WebRootPath, pathOverlayTemp);

                pathOverlay = Path.Combine(default_Path, $"{Rename()}.{fileExt[1]}");
                var absolutePathNewOverlay = Path.Combine(_env.WebRootPath, pathOverlay);

                var pathToThumbnail    = $"{Rename()}.jpg";
                var outputForThumbnail = Path.Combine(_env.WebRootPath, default_Path, pathToThumbnail);

                var saveThumbnailTask = new FfTaskSaveThumbnail(
                    absolutePathToFile,
                    outputForThumbnail,
                    TimeSpan.FromSeconds(10)
                    );
                await _media.ExecuteAsync(saveThumbnailTask);

                //MachineLearning _ml = new MachineLearning();
                //predicted = _ml.Pipe(outputForThumbnail, _env);


                var getVideoPortionTask = new FfTaskGetVideoPortion(
                    absolutePathToFile,
                    absolutePathNew,
                    TimeSpan.FromSeconds(10)
                    );

                var logoPath     = Path.Combine(default_Path, "afro_logo.png");
                var logoFullPath = Path.Combine(_env.WebRootPath, logoPath);

                var reduceQualityTask = new FfTaskReduceVideo(
                    absolutePathToFile,
                    absolutePathNewOverlayTemp
                    );

                var addWaterMarkTask = new FfTaskGetOverlay(
                    absolutePathNewOverlayTemp,
                    absolutePathNewOverlay,
                    logoFullPath
                    );
                await _media.ExecuteAsync(getVideoPortionTask);

                await _media.ExecuteAsync(reduceQualityTask);

                await _media.ExecuteAsync(addWaterMarkTask);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            finally
            {
                Delete(pathOverlayTemp);
            }

            return(pathOverlay, pathLow, predicted);
        }