Ejemplo n.º 1
0
        public ProgressTask(ProgressTaskFactory progressTaskFactory, object progressBarLock, IConsole konsoleWindow,
                            int maxProgress, int textWidth)
        {
            this.progressBarLock = progressBarLock;
            ProgressTaskFactory  = progressTaskFactory;

            progressBarCurrent = 0;
            progressBarText    = "";

            InitProgressBar(konsoleWindow, maxProgress, textWidth);
        }
        /// <summary>
        /// Transcode's all videos of a site
        /// </summary>
        /// <param name="configuration">Json configuration of IDBrowserService as IDBrowserConfiguration class</param>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <param name="siteName">Site name to transcode</param>
        /// <param name="videoSize">Video size to transcode. (e.g. Hd480, Hd720, Hd1080)</param>
        /// <param name="taskCount">FFmpeg task count (Default 2)</param>
        /// <param name="logLevel">Serilog log level</param>
        public static void TranscodeAllVideos(IDBrowserConfiguration configuration, CancellationToken cancellationToken,
                                              string siteName, string videoSize, Serilog.Events.LogEventLevel logLevel, int taskCount)
        {
            LoggingLevelSwitch loggingLevelSwitch = new LoggingLevelSwitch
            {
                MinimumLevel = logLevel
            };

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.ControlledBy(loggingLevelSwitch)
                         .WriteTo.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log", "ConsoleFunctions.log"))
                         .CreateLogger();

            SiteSettings siteSettings = configuration.Sites[siteName];

            IDImagerDB db = new IDImagerDB(GetIDImagerDBOptionsBuilder <IDImagerDB>(siteSettings.ConnectionStrings.DBType,
                                                                                    siteSettings.ConnectionStrings.IDImager).Options);

            var query = db.idCatalogItem
                        .Include(x => x.idFilePath)
                        .Where(x => configuration.VideoFileExtensions.Contains(x.idFileType));

            ProgressTaskFactory progressTaskFactory = new ProgressTaskFactory(taskCount, 100, 60, Log.Logger);

            Task windowChangeListenerTask = new Task(() =>
            {
                int width  = Console.WindowWidth;
                int height = Console.WindowHeight;

                while (!cancellationToken.IsCancellationRequested)
                {
                    if (width != Console.WindowWidth || height != Console.WindowHeight)
                    {
                        width  = Console.WindowWidth;
                        height = Console.WindowHeight;

                        progressTaskFactory.RedrawConsoleWindows(100, 60);
                    }

                    cancellationToken.WaitHandle.WaitOne(100);
                }
            }, cancellationToken);

            windowChangeListenerTask.Start();

            if (!IsWindows)
            {
                // Log output scrolling not supported at the moment because Console.MoveBufferArea is currently not supported under Linux and MacOs.
                // But we can write a single infoline.
                progressTaskFactory.WriteLog("Log output in console not supported under Linux at the moment. Please take a look at the ConsoleFunctions.log in Logs directory.",
                                             true, LogEventLevel.Information);
            }

            List <TranscodeVideoBatchInfo> listTranscodeVideoBatch = new List <TranscodeVideoBatchInfo>();

            foreach (idCatalogItem catalogItem in query)
            {
                catalogItem.GetHeightAndWidth(out int originalVideoWidth, out int originalVideoHeight);

                string strTranscodeFilePath = StaticFunctions.GetTranscodeFilePath(catalogItem.GUID,
                                                                                   siteSettings.ServiceSettings.TranscodeDirectory, videoSize);
                FileInfo transcodeFileInfo = new FileInfo(strTranscodeFilePath);

                if (!transcodeFileInfo.Exists)
                {
                    string strOriginalFilePath = StaticFunctions.GetImageFilePath(catalogItem, siteSettings.ServiceSettings.FilePathReplace);

                    TranscodeVideoBatchInfo transcodeVideoBatchInfo = new TranscodeVideoBatchInfo(catalogItem.GUID, originalVideoWidth, originalVideoHeight,
                                                                                                  strOriginalFilePath, strTranscodeFilePath);

                    listTranscodeVideoBatch.Add(transcodeVideoBatchInfo);
                }
            }

            db.Dispose();

            int intTotalCount = listTranscodeVideoBatch.Count();
            int intCounter    = 1;
            TranscodeVideoBatchInfo lastTranscodeVideoBatch = listTranscodeVideoBatch.LastOrDefault();

            foreach (TranscodeVideoBatchInfo transcodeVideoBatchInfo in listTranscodeVideoBatch)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                string strTranscodeFilePath = StaticFunctions.GetTranscodeFilePath(transcodeVideoBatchInfo.GUID,
                                                                                   siteSettings.ServiceSettings.TranscodeDirectory, videoSize);

                StaticFunctions.GetTranscodeVideoSize(videoSize, transcodeVideoBatchInfo.VideoWidth, transcodeVideoBatchInfo.VideoHeight,
                                                      out VideoSize targetVideoSize, out int targetVideoWidth, out int targetVideoHeight);

                var conversionOptions = StaticFunctions.GetConversionOptions(targetVideoSize, transcodeVideoBatchInfo.VideoWidth,
                                                                             transcodeVideoBatchInfo.VideoHeight);

                progressTaskFactory.WriteLog(string.Format("Transcoding file {0} of {1} with guid {2} and path \"{3}\" from resolution {4}x{5} to {6}x{7} ",
                                                           intCounter, intTotalCount, transcodeVideoBatchInfo.GUID, transcodeVideoBatchInfo.VideoFileInfo.FullName, transcodeVideoBatchInfo.VideoWidth,
                                                           transcodeVideoBatchInfo.VideoHeight, targetVideoWidth, targetVideoHeight),
                                             IsWindows, LogEventLevel.Information);

                ProgressTask progressTask = progressTaskFactory.GetIdleProgressTask();

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                if (progressTask == null)
                {
                    progressTask = progressTaskFactory
                                   .WaitForAnyTask()
                                   .Result;
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                string progressBarText = string.Format("{0} ({1} of {2})", transcodeVideoBatchInfo.TranscodeFileInfo.Name, intCounter, intTotalCount);

                progressTask.Task = new Task(() => {
                    TranscodeVideoProgressTask(transcodeVideoBatchInfo, videoSize, cancellationToken, progressTask, progressBarText);
                });

                progressTask.Task.Start();

                // If we are on the last item we have to wait for all tasks to complete
                if (transcodeVideoBatchInfo == lastTranscodeVideoBatch)
                {
                    progressTaskFactory.WaitForAllTasks();
                }

                intCounter++;
            }

            Log.CloseAndFlush();
        }