Ejemplo n.º 1
0
        protected override async Task <bool> UploadItem(PendingUpload upload)
        {
            var videoFileName     = DB.SharedSettings.GetMatchVideoPath(upload.DataName);
            var videoFileTempName = Path.ChangeExtension(DB.SharedSettings.GetMatchVideoPath(upload.DataName), ".temp.mp4");

            bool success = false;

            if (File.Exists(videoFileName))
            {
                return(true);
            }

            if (File.Exists(videoFileTempName))
            {
                File.Delete(videoFileTempName);
            }

            try
            {
                var matchData = await DB.GetData <MatchData>(upload.DataName);

                var conversion = await FFmpeg.Conversions.FromSnippet.Concatenate(
                    videoFileTempName,
                    matchData.Rounds.Select( roundName => DB.SharedSettings.GetRoundVideoPath(roundName)).ToArray()
                    );

                Console.WriteLine($"Starting conversion of {matchData.Name} with {matchData.Rounds.Count} rounds");

                var stopwatch = new System.Diagnostics.Stopwatch();

                stopwatch.Start();


                await conversion.Start();

                stopwatch.Stop();

                Console.WriteLine($"Conversion of {matchData.Name} took {stopwatch.Elapsed.Humanize()}");

                success = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                success = false;
            }

            if (success)
            {
                Console.WriteLine("Applying changes and removing leftover videos...");

                File.Move(videoFileTempName, videoFileName);

                // delete all the video files of the rounds, then set their type as mergedvideolink
                //and set their respective timespan times of when they appear in the videos

                var matchData = await DB.GetData <MatchData>(upload.DataName);

                matchData.VideoType      = VideoType.MergedVideoLink;
                matchData.VideoStartTime = TimeSpan.Zero;
                matchData.VideoEndTime   = matchData.GetDuration();
                await DB.SaveData(matchData);

                TimeSpan currentTime = TimeSpan.Zero;

                foreach (var roundName in matchData.Rounds)
                {
                    var roundVideoFile = DB.SharedSettings.GetRoundVideoPath(roundName);

                    if (File.Exists(roundVideoFile))
                    {
                        File.Delete(roundVideoFile);
                    }

                    var roundData = await DB.GetData <RoundData>(roundName);

                    roundData.VideoType      = VideoType.MergedVideoLink;
                    roundData.VideoStartTime = currentTime;
                    roundData.VideoEndTime   = currentTime + roundData.GetDuration();

                    await DB.SaveData(roundData);

                    currentTime += roundData.GetDuration();
                }

                Console.WriteLine("Done applying changes.");
            }
            else
            {
                File.Delete(videoFileTempName);
            }

            return(success);
        }