Exemplo n.º 1
0
        public static async Task <int> GetDurationAsync(Media media, List <IParam> parameters, CancellationToken token)
        {
            int    duration  = 0;
            IParam startTime = parameters.Find(e => e.Key == "-ss");
            IParam endTime   = parameters.Find(e => e.Key == "-to");

            int[]          times      = new int[] { 3600, 60, 1 };
            string         commandStr = $"-i \"{media.Path}\"";
            ProcessCommand process    = new ProcessCommand(commandStr, token);

            await foreach (string result in process.ResultAsync(token))
            {
                log.Info(result);
                if (result != null && result.Contains("Duration"))
                {
                    Match regex = new Regex(@"Duration: (\d+):(\d+):(\d+)").Match(result);
                    duration = regex.Groups.Values.TakeLast(3).Zip(times).Select((f, s) => f.First.Value.ToInt() * f.Second).Sum();
                    log.Info($"duration: {duration}");
                    break;
                }
            }

            duration  = endTime?.Value?.ToSeconds() ?? duration;
            duration -= startTime?.Value?.ToSeconds() ?? 0;

            return(duration);
        }
Exemplo n.º 2
0
        public static async Task ConvertAsync(Media media, List <IParam> parameters, CancellationToken token)
        {
            string         newFile    = NewFile(media, parameters);
            string         paramsStr  = string.Join(" ", parameters.Select(e => $"{e.Key} {e.Value}"));
            string         commandStr = $"-i \"{media.Path}\" {paramsStr} \"{newFile}\"";
            ProcessCommand process    = new ProcessCommand(commandStr, token);
            int            duration   = await GetDurationAsync(media, parameters, token);

            log.Info(duration);
            media.State = State.Converting;
            await foreach (string result in process.ResultAsync(token))
            {
                ProcessResult(media, result, duration);
            }
            media.Eta   = "Finished";
            media.State = State.Finished;
            log.Info($"{media.FileName} finished");
        }