152 lines
5.5 KiB
C#
152 lines
5.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using DotNetTools.SharpGrabber.Grabbed;
|
|
using DotNetTools.SharpGrabber;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using DotNetTools.SharpGrabber.Converter;
|
|
|
|
namespace WebAPI
|
|
{
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
CreateHostBuilder(args).Build().Run();
|
|
}
|
|
|
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
|
Host.CreateDefaultBuilder(args)
|
|
.ConfigureWebHostDefaults(webBuilder =>
|
|
{
|
|
webBuilder.UseStartup<Startup>();
|
|
});
|
|
}
|
|
//public static class Program
|
|
//{
|
|
// private static readonly HttpClient Client = new HttpClient();
|
|
// private static readonly HashSet<string> TempFiles = new HashSet<string>();
|
|
|
|
// public static async Task Main(string[] args)
|
|
// {
|
|
// var grabber = GrabberBuilder.New()
|
|
// .UseDefaultServices()
|
|
// .AddYouTube()
|
|
// .Build();
|
|
|
|
// Console.WriteLine("Enter FFMPEG path:");
|
|
// var ffmpegPath = Console.ReadLine().Trim();
|
|
// FFmpeg.AutoGen.ffmpeg.RootPath = ffmpegPath;
|
|
|
|
// Console.WriteLine("Enter a YouTube URL:");
|
|
// var url = Console.ReadLine();
|
|
// if (url == null)
|
|
// return;
|
|
|
|
// var result = await grabber.GrabAsync(new Uri(url, UriKind.Absolute));
|
|
|
|
// var audioStream = ChooseMonoMedia(result, MediaChannels.Audio);
|
|
// //var videoStream = ChooseMonoMedia(result, MediaChannels.Video);
|
|
// if (audioStream == null)
|
|
// throw new InvalidOperationException("No audio stream detected.");
|
|
// //if (videoStream == null)
|
|
// // throw new InvalidOperationException("No video stream detected.");
|
|
|
|
// try
|
|
// {
|
|
// var audioPath = await DownloadMedia(audioStream, result);
|
|
// //var videoPath = await DownloadMedia(videoStream, result);
|
|
// GenerateOutputFile(audioPath);
|
|
// }
|
|
// finally
|
|
// {
|
|
// foreach (var tempFile in TempFiles)
|
|
// {
|
|
// try
|
|
// {
|
|
// File.Delete(tempFile);
|
|
// }
|
|
// catch (Exception)
|
|
// {
|
|
// // ignored
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// private static void GenerateOutputFile(string audioPath)
|
|
// {
|
|
// Console.WriteLine("Output Path:");
|
|
// var outputPath = Console.ReadLine();
|
|
// if (string.IsNullOrWhiteSpace(outputPath))
|
|
// throw new Exception("No output path is specified.");
|
|
// var merger = new MediaMerger(outputPath);
|
|
// merger.AddStreamSource(audioPath, MediaStreamType.Audio);
|
|
// //merger.AddStreamSource(videoPath, MediaStreamType.Video);
|
|
// merger.OutputMimeType = "audio/mp3";//videoStream.Format.Mime;
|
|
// merger.OutputShortName = "mp3";//videoStream.Format.Extension;
|
|
|
|
// merger.Build();
|
|
// Console.WriteLine($"Output file successfully created.");
|
|
// }
|
|
|
|
// private static GrabbedMedia ChooseMonoMedia(GrabResult result, MediaChannels channel)
|
|
// {
|
|
// var resources = result.Resources<GrabbedMedia>()
|
|
// .Where(m => m.Channels == channel)
|
|
// .ToList();
|
|
|
|
// if (resources.Count == 0)
|
|
// return null;
|
|
|
|
// for (var i = 0; i < resources.Count; i++)
|
|
// {
|
|
// var resource = resources[i];
|
|
// Console.WriteLine($"{i}. {resource.Title ?? resource.FormatTitle ?? resource.Resolution}");
|
|
// }
|
|
|
|
// while (true)
|
|
// {
|
|
// Console.Write($"Choose the {channel} file: ");
|
|
// var choiceStr = Console.ReadLine();
|
|
// if (!int.TryParse(choiceStr, out var choice))
|
|
// {
|
|
// Console.WriteLine("Number expected.");
|
|
// continue;
|
|
// }
|
|
|
|
// if (choice < 0 || choice >= resources.Count)
|
|
// {
|
|
// Console.WriteLine("Invalid number.");
|
|
// continue;
|
|
// }
|
|
|
|
// return resources[choice];
|
|
// }
|
|
// }
|
|
|
|
// private static async Task<string> DownloadMedia(GrabbedMedia media, IGrabResult grabResult)
|
|
// {
|
|
// Console.WriteLine("Downloading {0}...", media.Title ?? media.FormatTitle ?? media.Resolution);
|
|
// Console.WriteLine(media.ResourceUri);
|
|
// Client.Timeout=TimeSpan.FromSeconds(200);
|
|
// using var response = await Client.GetAsync(media.ResourceUri);
|
|
// response.EnsureSuccessStatusCode();
|
|
// using var downloadStream = await response.Content.ReadAsStreamAsync();
|
|
// using var resourceStream = await grabResult.WrapStreamAsync(downloadStream);
|
|
// var path = Path.GetTempFileName();
|
|
|
|
// using var fileStream = new FileStream(path, FileMode.Create);
|
|
// TempFiles.Add(path);
|
|
// await resourceStream.CopyToAsync(fileStream);
|
|
// return path;
|
|
// }
|
|
//}
|
|
}
|