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()));
        }
コード例 #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));
        }
コード例 #5
0
        public static async Task <CreationResult> Create(
            string urlOrId,
            double startTime    = 0,
            double endTime      = -1,
            int parentElementId = -1,
            double watchPoint   = 0,
            ViewMode viewMode   = MediaPlayerConst.DefaultViewMode,
            bool shouldDisplay  = true)
        {
            // TODO: Time the verification check
            JObject metadata = await YdlUtils.GetYouTubeVideoMetadata(urlOrId);

            if (metadata == null || string.IsNullOrWhiteSpace((string)metadata["id"]))
            {
                LogTo.Warning($"Failed to recognise {urlOrId} as a YouTube video");
                return(CreationResult.FailUnknown);
            }

            YouTubeMediaElement ytEl;
            string youtubeId    = (string)metadata["id"];
            string title        = (string)metadata["title"];
            string uploader     = (string)metadata["uploader"];
            string date         = (string)metadata["upload_date"];
            string thumbnailUrl = (string)metadata["thumbnail"];

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

            List <ContentBase> contents = new List <ContentBase>();

            string elementHtml = string.Format(CultureInfo.InvariantCulture,
                                               MediaPlayerConst.YouTubeElementFormat,
                                               title,
                                               ytEl.GetJsonB64());

            contents.Add(new TextContent(true, elementHtml));
            if (Config.IncludeYouTubeThumbnail)
            {
                Image img = DownloadThumbnail(thumbnailUrl);
                if (img != null)
                {
                    var imgContent = ContentEx.CreateImageContent(img, string.Format(YTConst.VideoThumbImgRegPath, ytEl.Id));
                    if (imgContent != null)
                    {
                        contents.Add(imgContent);
                    }
                }
            }

            var refs = new References()
                       .WithTitle(title)
                       .WithAuthor(uploader)
                       .WithDate(HumanReadableDate(date))
                       .WithLink(ytEl.Url);

            var priority = MediaPlayerConst.DefaultExtractPriority;

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