コード例 #1
0
        private Task <GrabResult> GrabAsync(Uri uri, PlaylistDocument doc, CancellationToken cancellationToken,
                                            GrabOptions options)
        {
            // get resources
            IList <IGrabbed> grabs;

            if (doc.Streams.Count > 0)
            {
                grabs = GrabStreams(uri, doc);
            }
            else if (doc.Segments.Count > 0)
            {
                grabs = GrabSegments(uri, doc);
            }
            else
            {
                grabs = null;
            }

            // make result
            var result = new GrabResult(uri, grabs);

            if (options.Flags.HasFlag(GrabOptionFlag.Decrypt) && doc.Key != null)
            {
                UpdateDecryptionMethod(result, doc.Key);
            }
            return(Task.FromResult(result));
        }
コード例 #2
0
        protected void EnqueueTakenImage(PylonGrabResult_t grabResult)
        {
            PylonBuffer <Byte> buffer;  /* Reference to the buffer attached to the grab result. */

            /* Get the buffer from the dictionary. */
            if (!m_buffers.TryGetValue(grabResult.hBuffer, out buffer))
            {
                /* Oops. No buffer available? We should never have reached this point. Since all buffers are
                 * in the dictionary. */
                throw new Exception("Failed to find the buffer associated with the handle returned in grab result.");
            }

            /* Create a new grab result to enqueue to the grabbed buffers list. */
            GrabResult newGrabResultInternal = new GrabResult();

            newGrabResultInternal.Handle = grabResult.hBuffer; /* Add the handle to requeue the buffer in the stream grabber queue. */

            bool isBayer8 = PylonHelper.IsBayer8(grabResult.PixelType);

            if (isBayer8 && m_bayer8Conversion == Bayer8Conversion.Raw)
            {
                newGrabResultInternal.ImageData = new Image(grabResult.SizeX, grabResult.SizeY, buffer.Array, grabResult.PixelType == EPylonPixelType.PixelType_RGBA8packed);
            }
            else if (grabResult.PixelType == EPylonPixelType.PixelType_Mono8 || grabResult.PixelType == EPylonPixelType.PixelType_RGBA8packed)
            {
                newGrabResultInternal.ImageData = new Image(grabResult.SizeX, grabResult.SizeY, buffer.Array, grabResult.PixelType == EPylonPixelType.PixelType_RGBA8packed);
            }
            else /* Conversion is required. */
            {
                /* Create a new format converter if needed. */
                if (!m_hConverter.IsValid)
                {
                    m_convertedBuffers = new Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <byte> >(); /* Create a new dictionary for the converted buffers. */
                    m_hConverter       = Pylon.ImageFormatConverterCreate();                                /* Create the converter. */

                    // We force all non 8-bit Bayer formats to color.
                    // For Bayer 8, user may choose Raw, Mono or Color.
                    bool isBayer    = Pylon.IsBayer(grabResult.PixelType);
                    bool bayerColor = (isBayer && !isBayer8) || (isBayer8 && m_bayer8Conversion == Bayer8Conversion.Color);
                    m_converterOutputFormatIsColor = !Pylon.IsMono(grabResult.PixelType) || bayerColor;
                }
                /* Reference to the buffer attached to the grab result handle. */
                PylonBuffer <Byte> convertedBuffer = null;
                /* Look up if a buffer is already attached to the handle. */
                bool bufferListed = m_convertedBuffers.TryGetValue(grabResult.hBuffer, out convertedBuffer);
                /* Perform the conversion. If the buffer is null a new one is automatically created. */
                Pylon.ImageFormatConverterSetOutputPixelFormat(m_hConverter, m_converterOutputFormatIsColor ? EPylonPixelType.PixelType_BGRA8packed : EPylonPixelType.PixelType_Mono8);
                Pylon.ImageFormatConverterConvert(m_hConverter, ref convertedBuffer, buffer, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, (uint)grabResult.PaddingX, EPylonImageOrientation.ImageOrientation_TopDown);
                if (!bufferListed) /* A new buffer has been created. Add it to the dictionary. */
                {
                    m_convertedBuffers.Add(grabResult.hBuffer, convertedBuffer);
                }
                /* Add the image data. */
                newGrabResultInternal.ImageData = new Image(grabResult.SizeX, grabResult.SizeY, convertedBuffer.Array, m_converterOutputFormatIsColor);
            }
            lock (m_lockObject)                              /* Lock the grab result queue to avoid that two threads modify the same data. */
            {
                m_grabbedBuffers.Add(newGrabResultInternal); /* Add the new grab result to the queue. */
            }
        }
コード例 #3
0
        protected virtual void Grab(GrabResult result, JObject vars, GrabOptions options)
        {
            if (options.Flags.HasFlag(GrabOptionFlag.GrabImages))
            {
                var image_url = new Uri(result.OriginalUri, vars.SelectToken("$.image_url").Value <string>());
                result.Resources.Add(new GrabbedImage(GrabbedImageType.Primary, null, image_url));
            }

            result.Title             = vars.SelectToken("$.video_title").Value <string>();
            result.Statistics        = new GrabStatisticInfo();
            result.Statistics.Length = TimeSpan.FromSeconds(vars.SelectToken("$.video_duration").Value <int>());
            var qualities = vars.SelectTokens("$.defaultQuality[*]").Select(t => t.Value <int>()).ToArray();

            foreach (var quality in qualities)
            {
                var key = $"quality_{quality}p";
                var url = vars.SelectToken($"$.{key}")?.Value <string>();
                if (string.IsNullOrEmpty(url))
                {
                    continue;
                }
                var vid = new GrabbedMedia(new Uri(result.OriginalUri, url), null, new MediaFormat("video/mp4", "mp4"), MediaChannels.Both);
                vid.Resolution  = $"{quality}p";
                vid.FormatTitle = $"MP4 {vid.Resolution}";
                result.Resources.Add(vid);
            }
        }
コード例 #4
0
        /// <inheritdoc />
        public override async Task <GrabResult> GrabAsync(Uri uri, CancellationToken cancellationToken, GrabOptions options)
        {
            // grab view id
            var viewId = GetViewId(uri);

            if (viewId == null)
            {
                return(null);
            }
            uri = MakeStandardUri(viewId);

            using var client = HttpHelper.GetClient();
            // download content
            var response = await client.GetAsync(uri, cancellationToken);

            response.EnsureSuccessStatusCode();
            var htmlContent = await response.Content.ReadAsStringAsync();

            // cut the useful part of htmlContent to speed up regex look up
            htmlContent = CutUsefulPart(htmlContent);

            var objListIndex = htmlContent.IndexOf("playerObjList.");

            if (objListIndex < 0)
            {
                throw new GrabParseException("Could not find the video.");
            }
            htmlContent = htmlContent.Insert(objListIndex, "var playerObjList = {};\r\n");

            // grab javascript flashvars
            var flashVarsMatch = FlashVarsFinder.Match(htmlContent);

            if (!flashVarsMatch.Success)
            {
                throw new GrabParseException("Failed to locate flashvars.");
            }
            var qualityVarsMatch = QualityItemsVarFinder.Match(htmlContent);

            if (!qualityVarsMatch.Success)
            {
                throw new GrabParseException("Failed to locate qualityItems.");
            }
            var flashVarsVariableName    = flashVarsMatch.Groups[3].Value;
            var qualityItemsVariableName = qualityVarsMatch.Groups[3].Value;

            var(flashVars, qualityItems) = await ExtractFlashVars(flashVarsMatch.Groups[1].Value, flashVarsVariableName,
                                                                  qualityItemsVariableName);

            // generate result
            var result = new GrabResult(uri);

            Grab(result, flashVars, qualityItems, options);
            return(result);
        }
コード例 #5
0
        public override async Task <GrabResult> GrabAsync(Uri uri, CancellationToken cancellationToken, GrabOptions options)
        {
            if (!Supports(uri))
            {
                return(null);
            }

            using var client = HttpHelper.CreateClient(uri);
            var content = await client.GetStringAsync(uri).ConfigureAwait(false);

            var result   = new GrabResult(uri);
            var paramMap = ParsePage(content, result);

            // grab info
            result.Title      = paramMap.GetOrDefault("title")?.ToString();
            result.Statistics = new GrabStatisticInfo
            {
                Length = TimeSpan.FromSeconds(Convert.ToDouble(paramMap.GetOrDefault("duration"))),
            };

            // grab images
            var img = (paramMap.GetOrDefault("image") ?? paramMap.GetOrDefault("ThumbUrl169") ?? paramMap.GetOrDefault("ThumbUrl")) as string;

            if (Uri.TryCreate(img, UriKind.Absolute, out var imgUri))
            {
                result.Resources.Add(new GrabbedImage(GrabbedImageType.Thumbnail, uri, imgUri));
            }
            img = (paramMap.GetOrDefault("ThumbSlideBig") ?? paramMap.GetOrDefault("ThumbSlide")) as string;
            if (Uri.TryCreate(img, UriKind.Absolute, out imgUri))
            {
                result.Resources.Add(new GrabbedImage(GrabbedImageType.Preview, uri, imgUri));
            }

            // grab resources
            var hls = paramMap["VideoHLS"] as string;

            if (string.IsNullOrEmpty(hls))
            {
                throw new GrabParseException("Could not locate the HLS playlist file.");
            }
            var hlsUri        = new Uri(hls);
            var hlsGrabber    = new HlsGrabber();
            var hlsGrabResult = await hlsGrabber.GrabAsync(hlsUri, cancellationToken, options).ConfigureAwait(false);

            foreach (var resource in hlsGrabResult.Resources)
            {
                result.Resources.Add(resource);
            }

            return(result);
        }
コード例 #6
0
        protected void EnqueueTakenImage(PylonGrabResult_t grabResult)
        {
            PylonBuffer <Byte> buffer;  /* Reference to the buffer attached to the grab result. */

            /* Get the buffer from the dictionary. */
            if (!m_buffers.TryGetValue(grabResult.hBuffer, out buffer))
            {
                /* Oops. No buffer available? We should never have reached this point. Since all buffers are
                 * in the dictionary. */
                throw new Exception("Failed to find the buffer associated with the handle returned in grab result.");
            }

            /* Create a new grab result to enqueue to the grabbed buffers list. */
            GrabResult newGrabResultInternal = new GrabResult();

            newGrabResultInternal.Handle = grabResult.hBuffer; /* Add the handle to requeue the buffer in the stream grabber queue. */

            /* If already in output format add the image data. */
            if (grabResult.PixelType == EPylonPixelType.PixelType_Mono8 || grabResult.PixelType == EPylonPixelType.PixelType_RGBA8packed)
            {
                newGrabResultInternal.ImageData = new Image(grabResult.SizeX, grabResult.SizeY, buffer.Array, grabResult.PixelType == EPylonPixelType.PixelType_RGBA8packed);
            }
            else /* Conversion is required. */
            {
                /* Create a new format converter if needed. */
                if (!m_hConverter.IsValid)
                {
                    m_convertedBuffers             = new Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <byte> >(); /* Create a new dictionary for the converted buffers. */
                    m_hConverter                   = Pylon.PixelFormatConverterCreate(m_hDevice, 1);                    /* Create the converter. */
                    m_converterOutputFormatIsColor = !Pylon.IsMono(grabResult.PixelType) || Pylon.IsBayer(grabResult.PixelType);
                }
                /* Reference to the buffer attached to the grab result handle. */
                PylonBuffer <Byte> convertedBuffer = null;
                /* Look up if a buffer is already attached to the handle. */
                bool bufferListed = m_convertedBuffers.TryGetValue(grabResult.hBuffer, out convertedBuffer);
                /* Perform the conversion. If the buffer is null a new one is automatically created. */
                Pylon.PixelFormatConverterConvert(m_hConverter, ref convertedBuffer, buffer);
                if (!bufferListed) /* A new buffer has been created. Add it to the dictionary. */
                {
                    m_convertedBuffers.Add(grabResult.hBuffer, convertedBuffer);
                }
                /* Add the image data. */
                newGrabResultInternal.ImageData = new Image(grabResult.SizeX, grabResult.SizeY, convertedBuffer.Array, m_converterOutputFormatIsColor);
            }
            lock (m_lockObject)                              /* Lock the grab result queue to avoid that two threads modify the same data. */
            {
                m_grabbedBuffers.Add(newGrabResultInternal); /* Add the new grab result to the queue. */
            }
        }
コード例 #7
0
        /// <summary>
        /// Updates <see cref="GrabResult"/> according to the information obtained from metadata.
        /// </summary>
        private void UpdateResult(GrabResult result, YouTubeMetadata md)
        {
            var response = md.PlayerResponse;

            result.Title        = response.Title;
            result.CreationDate = response.UploadedAt;
            result.Description  = response.ShortDescription;

            result.Statistics = new GrabStatisticInfo
            {
                Author    = response.Author,
                Length    = response.Length,
                ViewCount = response.ViewCount
            };
        }
コード例 #8
0
        private void UpdateDecryptionMethod(GrabResult result, HlsKey key)
        {
            switch (key.Method)
            {
            case HlsKeyMethod.Aes128:
                var decryptor = new HlsAes128Decryptor(key);
                result.OutputStreamWrapper = decryptor.WrapStreamAsync;
                break;

            case HlsKeyMethod.None:
                break;

            default:
                throw new NotSupportedException($"HLS grab error: Decrypting {key.Method} is not supported.");
            }
        }
コード例 #9
0
        /// <summary>
        /// Generates all possible image URLs of the specified YouTube video.
        /// </summary>
        protected virtual void AppendImagesToResult(GrabResult result, string id, bool useHttps = true)
        {
            // We are gonna iterate through all possible image types and add links to every image
            // into result resources. Notice that since these URIs are not checked by sending HTTP
            // requests, there is a rare possibility for some generated links to be invalid and
            // missing from YouTube servers.

            var imageTypes = Enum.GetValues(typeof(YouTubeImageType));

            foreach (YouTubeImageType imageType in imageTypes)
            {
                var uri = GetYouTubeImageUri(id, imageType, useHttps);
                var img = new GrabbedImage(GrabbedImageType.Primary, null, uri);
                result.Resources.Add(img);
            }
        }
コード例 #10
0
        private void StartDownload(object sender, RoutedEventArgs e)
        {
            if (!CheckAccessibility(SpiderManager.CurrentConfigOfSpider.RequestConfig.URL))
            {
                MessageBox.Show("URL校验未通过");
                return;
            }

            //var config = Spider.CurrentConfigOfSpider.GrabConfigs.First(c => c.GetGrabType() == EGrabType.File) as GrabConfigOfFile;
            //var outPutPath = Path.Combine(config.DirectoryPath, config.FileName ?? Utilities.GrabNamingHelper.GetNameForFile(config.FileNameTag));
            //File.WriteAllText(outPutPath, "123");



            foreach (var grabConfig in SpiderManager.CurrentConfigOfSpider.GrabConfigs)
            {
                if (grabConfig.OnGrabFinish == null)
                {
                    grabConfig.OnGrabFinish += (url, isSuccess, message) =>
                    {
                        this.Dispatcher.Invoke(() =>
                        {
                            var item = new GrabResult(isSuccess)
                            {
                                GrabType = grabConfig.GetGrabType().ToString(),
                                URL      = url,
                                Message  = message
                            };
                            ListDownload.Items.Add(item);
                            ListDownload.SelectedIndex = ListDownload.Items.Count - 1;
                            ListDownload.UpdateLayout();
                            ListDownload.ScrollIntoView(ListDownload.SelectedItem);//TODO失效
                        });
                        if (isSuccess)
                        {
                            SaveConfig();
                        }
                        ;
                    };
                }
                Task.Factory.StartNew(() =>
                {
                    grabConfig.StartGrabbing(SpiderManager.CurrentConfigOfSpider.RequestConfig);
                });
            }
        }
コード例 #11
0
        private static void Save(GrabResult result)
        {
            var m = result.GetTMeshData();

            StringBuilder sb = new StringBuilder();

            sb.Append("g ").Append(Name).Append("\n");

            foreach (var v in m.vertices)
            {
                sb.Append(string.Format("v {0} {1} {2}\n", v.position.x, v.position.y, v.position.z));
            }
            sb.Append("\n");

            foreach (var vn in m.vertices)
            {
                sb.Append(string.Format("vn {0} {1} {2}\n", vn.normal.x, vn.normal.y, vn.normal.z));
            }
            sb.Append("\n");

            foreach (var uv in m.vertices)
            {
                sb.Append(string.Format("vt {0} {1}\n", uv.texCoord.x, uv.texCoord.y));
            }
            for (int material = 0; material < m.SubMeshCount; material++)
            {
                sb.Append("\n");

                var triangles = m.GetTriangles(material);
                for (int i = 0; i < triangles.Count; i += 1)
                {
                    sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n",
                                            triangles[i].vertexId[0] + 1, triangles[i].vertexId[1] + 1, triangles[i].vertexId[2] + 1));
                }
            }

            string path = "_Export/Techs";

            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            System.IO.File.WriteAllText(path + "/" + Name + ".obj", sb.ToString());
            GUIConfig.log = "Exported " + Name + ".obj to " + path + " using the GrabIt API";
        }
コード例 #12
0
        /// <summary>
        /// Given the specified <paramref name="metaData"/>, generates proper <see cref="GrabResult"/>.
        /// </summary>
        protected virtual GrabResult GrabUsingConfiguration(Configuration configuration, Uri originalUri)
        {
            var grabList = new List <IGrabbed>();

            // Ensure we have at least one video format available
            if (string.IsNullOrWhiteSpace(configuration?.Request?.Files?.Progressive?.FirstOrDefault().Url))
            {
                return(null);
            }

            if (configuration.Video?.Thumbs != null)
            {
                foreach (var keyValuePair in configuration.Video.Thumbs)
                {
                    var imageType = GrabbedImageType.Thumbnail;
                    if (keyValuePair.Key == "base")
                    {
                        imageType = GrabbedImageType.Primary;
                    }

                    grabList.Add(new GrabbedImage(imageType, null, new Uri(keyValuePair.Value)));
                }
            }

            foreach (var progressive in configuration.Request.Files.Progressive)
            {
                var format = new MediaFormat(progressive.Mime, MimeHelper.ExtractMimeExtension(progressive.Mime));
                var vid    = new GrabbedMedia(new Uri(progressive.Url), null, format, MediaChannels.Both)
                {
                    Resolution = progressive.Quality
                };
                grabList.Add(vid);
            }

            var result = new GrabResult(originalUri, grabList)
            {
                Title = configuration.Video?.Title
            };

            return(result);
        }
コード例 #13
0
        /// <inheritdoc />
        public sealed override async Task <GrabResult> GrabAsync(Uri uri, CancellationToken cancellationToken, GrabOptions options)
        {
            // get YouTube ID
            var id = GetYouTubeId(uri);

            if (string.IsNullOrEmpty(id))
            {
                return(null);
            }

            // generate original link
            var originalUri = GetYouTubeStandardUri(id);

            // prepare result
            var result = new GrabResult(originalUri);

            // grab using the internal grab method
            await GrabAsync(result, id, cancellationToken, options);

            return(result);
        }
コード例 #14
0
        /// <summary>
        /// Given the specified <paramref name="metaData"/>, generates proper <see cref="GrabResult"/>.
        /// </summary>
        protected virtual GrabResult GrabUsingMetadata(IDictionary <string, string> metaData)
        {
            var grabList = new List <IGrabbed>();

            // extract metadata
            var title            = metaData.GetOrDefault("og:title");
            var image            = metaData.GetOrDefault("og:image");
            var description      = metaData.GetOrDefault("og:description");
            var originalUri      = new Uri(metaData.GetOrDefault("og:url"));
            var type             = metaData.GetOrDefault("og:type");
            var video            = metaData.GetOrDefault("og:video");
            var video_secure_url = metaData.GetOrDefault("og:video:secure_url");
            var video_type       = metaData.GetOrDefault("og:video:type");
            var video_width      = int.Parse(metaData.GetOrDefault("og:video:width", "0"));
            var video_height     = int.Parse(metaData.GetOrDefault("og:video:height", "0"));

            // grab image
            if (!string.IsNullOrEmpty(image))
            {
                grabList.Add(new GrabbedImage(GrabbedImageType.Primary, null, new Uri(image)));
            }

            // grab video
            if (!string.IsNullOrEmpty(video))
            {
                var format = new MediaFormat(video_type, MimeHelper.ExtractMimeExtension(video_type));
                var vid    = new GrabbedMedia(new Uri(video), null, format, MediaChannels.Both);
                grabList.Add(vid);
            }

            // make result
            var result = new GrabResult(originalUri, grabList)
            {
                Title       = title,
                Description = description,
            };

            return(result);
        }
コード例 #15
0
        private IDictionary <string, object> ParsePage(string content, GrabResult result)
        {
            var varNameMatch = VideoVarNameRegex.Match(content);

            if (!varNameMatch.Success)
            {
                throw new GrabParseException("Failed to locate the video variable in the script.");
            }
            var varName = varNameMatch.Groups[1].Value;

            var videoParamRegex   = new Regex(string.Format(VideoParamRegexString, varName), RegexOptions.IgnoreCase | RegexOptions.Multiline);
            var videoParamMatches = videoParamRegex.Matches(content);

            var paramMap = new Dictionary <string, object>();

            foreach (Match paramMatch in videoParamMatches)
            {
                var name        = paramMatch.Groups[2].Value;
                var stringValue = paramMatch.Groups[3].Value.Replace('\'', '"');

                if (JsonHelper.TryParseJson(stringValue, out var value))
                {
                    paramMap.Add(name, value);
                }
            }

            var doc = new HtmlDocument();

            doc.LoadHtml(content);
            var metaTags = doc.DocumentNode.SelectNodes("//meta[starts-with(@property,'og:')]");

            foreach (var tag in metaTags)
            {
                var name = tag.GetAttributeValue("property", null).Replace("og:", string.Empty);
                var val  = tag.GetAttributeValue("content", null);
                paramMap.Add(name, val);
            }
            return(paramMap);
        }
コード例 #16
0
        /// <inheritdoc />
        protected override async Task GrabAsync(GrabResult result, string id, CancellationToken cancellationToken, GrabOptions options)
        {
            // extract base.js script
            var embedPageData = await DownloadEmbedPage(id);

            // download metadata
            var metaData = await DownloadMetadata(id, cancellationToken);

            // update result according to the metadata
            UpdateResult(result, metaData);

            // are there any encrypted streams?
            result.IsSecure = metaData.AllStreams.Any(stream => !string.IsNullOrEmpty(stream.Signature));

            // should we decipher the signature?
            if (result.IsSecure && options.Flags.HasFlag(GrabOptionFlag.Decipher))
            {
                await Decipher(embedPageData, metaData, cancellationToken);
            }

            // append images to the result
            if (options.Flags.HasFlag(GrabOptionFlag.GrabImages))
            {
                AppendImagesToResult(result, id);
            }

            // append muxed streams to result
            foreach (var stream in metaData.MuxedStreams)
            {
                AppendStreamToResult(result, stream);
            }

            // append adaptive streams to result
            foreach (var stream in metaData.AdaptiveStreams)
            {
                AppendStreamToResult(result, stream);
            }
        }
コード例 #17
0
        /// <summary>
        /// Appends the specified <paramref name="stream"/> to the specified <paramref name="result"/>.
        /// </summary>
        protected virtual void AppendStreamToResult(GrabResult result, YouTubeStreamInfo stream)
        {
            MediaChannels channels;

            // get iTag info
            var itagInfo = stream.iTag == null ? null : YouTubeTags.For(stream.iTag.Value);

            // extract extension from mime
            var extension = stream.Extension ?? stream.Mime?.Split('/')?.Last();

            // decide according to stream type - adaptive, or muxed
            if (stream is YouTubeMuxedStream muxedStream)
            {
                // Muxed stream
                channels = MediaChannels.Both;
            }
            else if (stream is YouTubeAdaptiveStream adaptiveStream)
            {
                // Adaptive stream
                var hasVideo = itagInfo?.HasVideo ?? stream.Mime.StartsWith("video");
                channels = hasVideo ? MediaChannels.Video : MediaChannels.Audio;
            }
            else
            {
                throw new NotSupportedException($"YouTube stream of type {stream.GetType()} is not implemented in {nameof(YouTubeGrabber)}.{nameof(AppendStreamToResult)}.");
            }

            var format  = new MediaFormat(stream.Mime, extension);
            var grabbed = new GrabbedMedia(new Uri(stream.Url), null, format, channels);

            result.Resources.Add(grabbed);

            // update grabbed media iTag info
            if (itagInfo != null)
            {
                UpdateStreamITagInfo(grabbed, itagInfo.Value);
            }
        }
コード例 #18
0
        /// <inheritdoc />
        public override async Task <GrabResult> GrabAsync(Uri uri, CancellationToken cancellationToken, GrabOptions options)
        {
            // grab view id
            var viewId = GetViewId(uri);

            if (viewId == null)
            {
                return(null);
            }
            uri = MakeStandardUri(viewId);

            using (var client = HttpHelper.CreateClient())
            {
                // download content
                var response = await client.GetAsync(uri, cancellationToken);

                response.EnsureSuccessStatusCode();
                var htmlContent = await response.Content.ReadAsStringAsync();

                // cut the useful part of htmlContent to speed up regex look up
                htmlContent = CutUsefulPart(htmlContent);

                // grab javascript flashvars
                var match = FlashVarsFinder.Match(htmlContent);
                if (!match.Success)
                {
                    throw new GrabParseException("Failed to locate flashvars.");
                }
                var variableName = match.Groups[3].Value;
                var flashVars    = await ExtractFlashVars(match.Groups[1].Value, variableName);

                // generate result
                var result = new GrabResult(uri);
                Grab(result, flashVars, options);
                return(result);
            }
        }
コード例 #19
0
        protected void EnqueueTakenImage(PylonGrabResult_t grabResult)
        {
            PylonBuffer<Byte> buffer;  /* Reference to the buffer attached to the grab result. */

            /* Get the buffer from the dictionary. */
            if (!m_buffers.TryGetValue(grabResult.hBuffer, out buffer))
            {
                /* Oops. No buffer available? We should never have reached this point. Since all buffers are
                   in the dictionary. */
                throw new Exception("Failed to find the buffer associated with the handle returned in grab result.");
            }

            /* Create a new grab result to enqueue to the grabbed buffers list. */
            GrabResult newGrabResultInternal = new GrabResult();
            newGrabResultInternal.Handle = grabResult.hBuffer; /* Add the handle to requeue the buffer in the stream grabber queue. */

            /* If already in output format add the image data. */
            if (grabResult.PixelType == EPylonPixelType.PixelType_Mono8 || grabResult.PixelType == EPylonPixelType.PixelType_RGBA8packed)
            {
                newGrabResultInternal.ImageData = new Image(grabResult.SizeX, grabResult.SizeY, buffer.Array, grabResult.PixelType == EPylonPixelType.PixelType_RGBA8packed);
            }
            else /* Conversion is required. */
            {
                /* Create a new format converter if needed. */
                if (!m_hConverter.IsValid)
                {
                    m_convertedBuffers = new Dictionary<PYLON_STREAMBUFFER_HANDLE,PylonBuffer<byte>>(); /* Create a new dictionary for the converted buffers. */
                    m_hConverter = Pylon.PixelFormatConverterCreate(m_hDevice, 1); /* Create the converter. */
                    m_converterOutputFormatIsColor = !Pylon.IsMono(grabResult.PixelType) || Pylon.IsBayer(grabResult.PixelType);
                }
                /* Reference to the buffer attached to the grab result handle. */
                PylonBuffer<Byte> convertedBuffer = null;
                /* Look up if a buffer is already attached to the handle. */
                bool bufferListed = m_convertedBuffers.TryGetValue(grabResult.hBuffer, out convertedBuffer);
                /* Perform the conversion. If the buffer is null a new one is automatically created. */
                Pylon.PixelFormatConverterConvert(m_hConverter, ref convertedBuffer, buffer);
                if (!bufferListed) /* A new buffer has been created. Add it to the dictionary. */
                {
                    m_convertedBuffers.Add(grabResult.hBuffer, convertedBuffer);
                }
                /* Add the image data. */
                newGrabResultInternal.ImageData = new Image(grabResult.SizeX, grabResult.SizeY, convertedBuffer.Array, m_converterOutputFormatIsColor);
            }
            lock (m_lockObject) /* Lock the grab result queue to avoid that two threads modify the same data. */
            {
                m_grabbedBuffers.Add(newGrabResultInternal); /* Add the new grab result to the queue. */
            }
        }
コード例 #20
0
 public StreamDownloader(GrabbedStreamViewModel viewModel, string targetPath, GrabResult grabResult)
 {
     _grabResult = grabResult;
     _viewModel  = viewModel;
     _targetPath = targetPath;
 }
コード例 #21
0
        protected virtual void Grab(GrabResult result, JObject flashVars, JArray qualityItemVars, GrabOptions options)
        {
            var grabbed = new Dictionary <int, GrabbedMedia>();

            if (options.Flags.HasFlag(GrabOptionFlag.GrabImages))
            {
                var image_url = new Uri(result.OriginalUri, flashVars.SelectToken("$.image_url").Value <string>());
                result.Resources.Add(new GrabbedImage(GrabbedImageType.Primary, null, image_url));
            }

            result.Title      = flashVars.SelectToken("$.video_title").Value <string>();
            result.Statistics = new GrabStatisticInfo
            {
                Length = TimeSpan.FromSeconds(flashVars.SelectToken("$.video_duration").Value <int>())
            };

            if (qualityItemVars != null && qualityItemVars.Count > 0)
            {
                foreach (var quality in qualityItemVars)
                {
                    var url = quality.Value <string>("url");
                    if (string.IsNullOrEmpty(url))
                    {
                        continue;
                    }
                    var vid = new GrabbedMedia(new Uri(result.OriginalUri, url), result.OriginalUri, DefaultMediaFormat, MediaChannels.Both);
                    vid.Resolution = quality.Value <string>("text");
                    var qint = StringHelper.ForceParseInt(vid.Resolution);
                    grabbed.Add(qint, vid);
                }
            }

            var mediaDefinitions = flashVars.SelectToken("$.mediaDefinitions");

            foreach (var def in mediaDefinitions)
            {
                var format       = def.Value <string>("format");
                var url          = def.Value <string>("videoUrl");
                var isQualityArr = def["quality"] is JArray;
                var qualityArr   = isQualityArr ? def["quality"].Values <int>().ToArray() : null;
                var quality      = isQualityArr ? 0 : StringHelper.ForceParseInt(def.Value <string>("quality"));
                if (grabbed.ContainsKey(quality) || string.IsNullOrEmpty(url))
                {
                    continue;
                }
                if (isQualityArr && qualityArr.Length == 0)
                {
                    continue;
                }
                var uri          = new Uri(result.OriginalUri, url);
                var resol        = isQualityArr ? null : $"{quality}p";
                var playlistType = isQualityArr ? HlsPlaylistType.Master : HlsPlaylistType.Stream;

                switch (format.ToLowerInvariant())
                {
                case "mp4":
                    var m = new GrabbedMedia(uri, result.OriginalUri, DefaultMediaFormat, MediaChannels.Both)
                    {
                        Resolution  = resol,
                        FormatTitle = $"MP4 {resol}",
                    };
                    grabbed.Add(quality, m);
                    break;

                case "hls":
                    var sr = new GrabbedStreamReference(uri, result.OriginalUri)
                    {
                        Resolution   = resol,
                        PlaylistType = playlistType,
                    };
                    result.Resources.Add(sr);
                    break;

                default:
                    continue;
                }
            }

            foreach (var g in grabbed.OrderByDescending(m => m.Key))
            {
                result.Resources.Add(g.Value);
            }
        }
コード例 #22
0
 /// <summary>
 /// This method gets called internally by <see cref="GrabAsync(Uri, CancellationToken, GrabOptions)"/> after necessary initializations.
 /// </summary>
 protected abstract Task GrabAsync(GrabResult result, string id, CancellationToken cancellationToken, GrabOptions options);
コード例 #23
0
 public MediaDownloader(GrabbedMediaViewModel grabbedViewModel, string targetPath, GrabResult grabResult)
 {
     _grabResult = grabResult;
     _viewModel  = grabbedViewModel;
     _targetPath = targetPath;
 }