Skip to content

Instantly share code, notes, and snippets.

@fergalmoran
Created May 29, 2017 19:56
Show Gist options
  • Save fergalmoran/6a06190335bfe0ec36bca44e2f0e85b0 to your computer and use it in GitHub Desktop.
Save fergalmoran/6a06190335bfe0ec36bca44e2f0e85b0 to your computer and use it in GitHub Desktop.
Youtube DL
using System;
using System.Dynamic;
using Newtonsoft.Json;
using NYoutubeDL;
using static NYoutubeDL.Helpers.Enums;
namespace NetCoreYTDl {
class Program {
const string YT_URL = "https://www.youtube.com/watch?v=rzfmZC3kg3M";
static void Main(string[] args) {
try {
Console.WriteLine($"Downloading: {YT_URL}");
Console.WriteLine("Downloading audio");
DownloadAudio(YT_URL);
Console.WriteLine("Getting information");
DownloadInfo(YT_URL);
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
Console.WriteLine("Finished");
}
private static void DownloadAudio(string YT_URL) {
var youtubeDl = new YoutubeDL();
youtubeDl.Options.FilesystemOptions.Output = $"/tmp/{System.Guid.NewGuid().ToString()}.m4a";
youtubeDl.Options.PostProcessingOptions.ExtractAudio = true;
youtubeDl.Options.PostProcessingOptions.AudioFormat = AudioFormat.aac;
youtubeDl.VideoUrl = YT_URL;
youtubeDl.StandardOutputEvent += (s, e) =>
Console.WriteLine(e);
var result = youtubeDl.PrepareDownload();
Console.WriteLine(result);
youtubeDl.Download().WaitForExit();
}
private static void DownloadInfo(string YT_URL) {
var youtubeDl = new YoutubeDL();
youtubeDl.Options.VerbositySimulationOptions.DumpJson = true;
youtubeDl.VideoUrl = YT_URL;
youtubeDl.StandardOutputEvent += (s, e) =>
ProcessOutput(e);
var result = youtubeDl.PrepareDownload();
Console.WriteLine(result);
youtubeDl.Download().WaitForExit();
}
public static void ProcessOutput(string output) {
dynamic result = JsonConvert.DeserializeObject<ExpandoObject>(output);
Console.WriteLine($"Description: {result.description}");
Console.WriteLine($"Uploader: {result.uploader}");
Console.WriteLine($"Thumbnail: {result.thumbnail}");
Console.WriteLine($"Duration: {result.duration}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment