Skip to content

02 Getting Started

javidsho edited this page May 23, 2023 · 22 revisions

Package Installation

The SharpGrabber package defines abstractions only. The actual grabbers have their own packages and should be installed separately.

Install-Package SharpGrabber -Version 2.1.1

It's an optional package to work with media files. Using this package, you can easily concatenate video segments, or mux audio and video channels. It uses ffmpeg shared libraries underneath.

Install-Package SharpGrabber.Converter -Version 1.1

This package adds the capability to parse M3U8 playlist files - including master playlists - and download video segments. With the help of the SharpGrabber.Converter package, segments may be joined together. This package also supports AES-128 decryption.

Install-Package SharpGrabber.Hls -Version 1.3

Adds support to download high-quality videos from YouTube, even if they are served as separate video and audio files only. The high-quality output is possible thanks to the SharpGrabber.Converter library.

Install-Package SharpGrabber.YouTube -Version 1.5
Install-Package SharpGrabber.Vimeo -Version 1.0
Install-Package SharpGrabber.Odysee -Version 1.0.1

Warning: This grabber is not guaranteed to work. It works only for clients that Instagram allows anonymous access to public content.

Install-Package SharpGrabber.Instagram -Version 0.1

Adds support for PornHub, xnxx, and xvideos.

Install-Package SharpGrabber.Adult -Version 1.0.2

Building a Grabber

First things first. To start grabbing stuff, the first thing you'll need is to build a grabber - the sole purpose of GrabberBuilder. It provides a fluent way to build a multi-grabber.

Example

var grabber = GrabberBuilder.New()
			    .UseDefaultServices()
			    .AddYouTube()
			    .AddVimeo()
			    .AddHls()
			    .Build();

Note: You'll need to install the corresponding grabber packages to be able to add them to the a grabber builder e.g. SharpGrabber.YouTube.

Grabbing Resources

Now you should provide the grabber with a URL to scrape. Then you can access the collection of all grabbed resources from grabResult.Resources.

Example

var grabResult = await grabber.GrabAsync(new Uri("https://www.youtube.com/watch?v=LTseTg48568"));

Note: In the example above the simplest form of GrabAsync is used. You can provide CancellationToken, IProgress, and GrabOptions to other overloads. GrabOptions allows you to avoid grabbing unnecessary information and therefore avoid useless process cycles and potential network usage.

Selecting Resources

The collection of grabbed resources returned by GrabAsync usually contains various types of grabbed resources, such as images and video. Naturally, they need to be filtered in most situations. You may use the extension methods named Resource and Resources for convenience.

Example

Console.WriteLine("Grabbed {0}", grabResult.Title);
var info = grabResult.Resource<GrabbedInfo>();
var images = grabResult.Resources<GrabbedImage>().ToArray();
var mediaFiles = grabResult.Resources<GrabbedMedia>().ToArray();
Console.WriteLine("Found {0} images and {1} media files", images.Length, mediaFiles.Length);
var sortedMediaFiles = mediaFiles.OrderByResolutionDescending().ThenByBitRateDescending().ToArray();
var bestVideo = mediaFiles.GetHighestQualityVideo();
var bestAudio = mediaFiles.GetHighestQualityAudio();
Console.WriteLine("Best video = {0}, Best audio = {1}", bestVideo, bestAudio);