dotnettools/SharpGrabber

How to use MediaDownloader in C# console project (.NET 5.0)

zydjohnHotmail opened this issue · 1 comments

Hello:
I found the nuget version of SharpGrabber.HLS. I want to see if I can use this new nuget package to download some M3U8 HLS and save to my local hard disk.
I created one C# console project, target .NET 5.0.
And add the following NUGET packages:
PM> Install-Package SharpGrabber.Hls -Version 1.0.0
PM> Install-Package SharpGrabber -Version 2.0.0

The following is my C# code:

using DotNetTools.SharpGrabber;
using DotNetTools.SharpGrabber.Exceptions;
using DotNetTools.SharpGrabber.Grabbed;
using DotNetTools.SharpGrabber.Hls;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

namespace SharpGrabberDownloader
{
	class Program
	{
		public const string M3U8_Link1 = 
			@"http://playertest.longtailvideo.com/adaptive/oceans_aes/oceans_aes.m3u8";
		public const string Local_MP4_File = 
			@"C:\Videos\oceans_aes.mp4";

		private static readonly HttpClient _http = new HttpClient();
		private static async Task Download(Uri uri)
		{
			var downloader = new MediaDownloader(grabbed, path, CurrentGrab);
			await downloader.DownloadAsync();
		}

		static void Main(string[] args)
		{
			var uri = new Uri(M3U8_Link1);
			await Download(uri);
			Console.WriteLine("Done!");
		}
	}
}

But I have some issues:

  1. From solution explorer, I can see the yellow warning mark besides the SharpGrabber.Hls nuget package, it could mean that SharpGrabber.Hls is not compatible with .NET 5.0, right?
  2. I got compiler error:
    The type or namespace name 'MediaDownloader' could not be found (are you missing a using directive or an assembly reference?)
    It seems the MediaDownloader is NOT a public accessible type.
    How I can solve this?
    Finally, what I want to do is just download the M3U8_Link1, and save as a MP4 file, saved at
    Local_MP4_File, but I need download the HLS asynchronously, as I have many other similar HLS to download, some of them are in real-time, as some sports games videos on going.
    But I also need the downloader can stop and combine all downloaded parts to make one MP4 file after the download finishes or the HLS stops sending more data.
    Please advise,
    Thanks,

Hello there,

  1. First of all, you don't need to add both SharpGrabber and SharpGrabber.Hls packages, adding the latter will automatically cause in addition of the former, because it depends on it.
  2. SharpGrabber is a .NET Standard library. It has the most compatibility with .NET and it's also true about .NET 5. Just to be sure, I created a new .NET 5 project and showed me no warnings. Try to read what exactly the warning is about.
  3. MediaDownloader is a class written and used in SharpGrabber.Desktop only, which is not available as a package. Also, it's not what you want. You'd actually want to use StreamDownloader. It's really simple I'm sure you can write it yourself, and I recommend you to do so. However, if you want to use that exact same class anyway, you can copy the code from here: https://github.com/dotnettools/SharpGrabber/blob/master/src/SharpGrabber.Desktop/Downloading/StreamDownloader.cs

I suggest you to write your own code instead of using the StreamDownloader class.
The downloading part is simple, just download the segments asynchronously and save them on the disk. As for conversion, you can install the SharpGrabber.Converter NuGet package and make your life easier. Alternatively, you can run ffmpeg.exe and make it do the conversion for you.

At last, if you want to download a live stream, you need to do more coding and experimenting, because I've tested this part of the library only with non-live streams.