示例#1
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));
        }
示例#2
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));
        }