public ExtractResult ImageExtract(int expectedId, string base64, string subs)
        {
            if (string.IsNullOrWhiteSpace(base64))
            {
                return(new ExtractResult(false, "Failed to create image extract because base 64 data was null or empty"));
            }

            subs = subs ?? string.Empty;
            byte[] bytes = Convert.FromBase64String(base64);

            try
            {
                List <ContentBase> contents = new List <ContentBase>();
                contents.Add(new TextContent(true, subs));

                var    htmlCtrl = ContentEx.GetFirstHtmlCtrl();
                var    refs     = htmlCtrl?.GetReferences();
                string title    = refs?.Title ?? CurrentMediaElement.Id;

                var image      = CreateImage(base64);
                var imgContent = ContentEx.CreateImageContent(image, title);
                if (imgContent != null)
                {
                    contents.Add(imgContent);
                }

                var priority = MediaPlayerState.Instance.Config.DefaultExtractPriority;
                var res      = ContentEx.CreateSMElement(expectedId, priority, contents, refs, false);
                return(new ExtractResult(res == CreationResult.Ok, res.Name()));
            }
            catch (Exception)
            {
                return(new ExtractResult(false, "Failed to create image extract"));
            }
        }
        public ExtractResult GifExtract(int expectedId, string base64, string subs)
        {
            if (string.IsNullOrWhiteSpace(base64))
            {
                return(new ExtractResult(false, "Failed to create image extract because base 64 data was null or empty"));
            }

            byte[] bytes = Convert.FromBase64String(base64);

            try
            {
                var    htmlCtrl = ContentEx.GetFirstHtmlCtrl();
                var    refs     = htmlCtrl?.GetReferences();
                string filename = $"{refs?.Title ?? CurrentMediaElement.Id}.gif";
                string filepath = MediaPlayerFilesystem.ImageDir.CombineFile(filename).FullPath;
                File.WriteAllBytes(filepath, bytes);

                List <ContentBase> contents = new List <ContentBase>();
                subs  = subs ?? string.Empty;
                subs += "\r\n";
                subs += $"<img href='{filepath}'>";
                contents.Add(new TextContent(true, subs));

                refs.Title += ": Gif Extract";
                var priority = MediaPlayerState.Instance.Config.DefaultExtractPriority;
                var res      = ContentEx.CreateSMElement(expectedId, priority, contents, refs, false);
                return(new ExtractResult(res == CreationResult.Ok, res.Name()));
            }
            catch (Exception e)
            {
                return(new ExtractResult(false, $"Failed to create image extract with exception {e}"));
            }
        }
        public async Task <ExtractResult> GifExtractRemote(double start, double end, string subs)
        {
            var streams = await Ydl.GetAVStreamUrlsAsync(CurrentMediaElement.Id);

            if (streams == null)
            {
                return(new ExtractResult(false, "Youtube-dl failed to get av streams"));
            }

            var htmlCtrl = ContentEx.GetFirstHtmlCtrl();
            var refs     = htmlCtrl?.GetReferences();

            var filename = $"{refs?.Title ?? CurrentMediaElement.Id}-{start}-{end}";
            var filepath = MediaPlayerFilesystem.ImageDir.CombineFile(filename).FullPath;

            if (!await Ffmpeg.GifExtract(streams.Video, start, end, filepath))
            {
                return(new ExtractResult(false, "Ffmpeg failed to create gif extract"));
            }

            List <ContentBase> contents = new List <ContentBase>();
            var htmlContent             = subs ?? string.Empty;

            htmlContent += "\r\n";
            htmlContent += $"<img href='{filepath}'>";
            var mainHtml = new TextContent(true, htmlContent);

            contents.Add(mainHtml);

            refs.Title += $": Gif Extract - {start} -> {end}"; // TODO: Convert to human readable
            var priority = MediaPlayerState.Instance.Config.DefaultExtractPriority;
            var res      = ContentEx.CreateSMElement(ExpectedElementId, priority, contents, refs, false);

            return(new ExtractResult(res == CreationResult.Ok, res.Name()));
        }
Ejemplo n.º 4
0
        public CreationResult Create(
            int parentElementId,
            double startTime,
            double endTime,
            double watchPoint,
            ViewMode viewMode,
            bool shouldDisplay)
        {
            var html = ContentEx.GetFirstHtmlCtrl();
            var refs = ReferenceParser.GetReferences(html);

            if (refs == null)
            {
                return(CreationResult.FailUnknown);
            }

            var ytEl = new YouTubeMediaElement
            {
                Id         = this.Id,
                StartTime  = startTime,
                EndTime    = endTime,
                WatchPoint = watchPoint,
                ViewMode   = viewMode,
            };

            refs.Title += $": {startTime} -> {endTime}";
            string elementHtml = string.Format(CultureInfo.InvariantCulture,
                                               MediaPlayerConst.YouTubeElementFormat,
                                               refs.Title,
                                               ytEl.GetJsonB64());

            var contents = new List <ContentBase> {
                new TextContent(true, elementHtml)
            };
            var priority = MediaPlayerConst.DefaultExtractPriority;

            return(ContentEx.CreateSMElement(parentElementId, priority, contents, refs, shouldDisplay));
        }