Hey, when I try to download a restricted video in my country just like this:
"https://www.youtube.com/watch?v=iN8PKcNGcuI"
I get an exception saying "YoutubeExplode.Exceptions.VideoUnavailableException" and the program stops working.
even that I wrote it in (try-catch) how to fix it?
Here is a part of my code(it stops at the last line)
c#
try
{
//string Path = Directory.GetCurrentDirectory();
//GrantAccess(Path);
var url = GetYoutubeUrl();
if (url != "")
{
var id = YoutubeClient.ParseVideoId(url); // "bnsUkE8i0tU"
var client = new YoutubeClient();
var streamInfoSet = await client.GetVideoMediaStreamInfosAsync(id);
I've also had a few of these which kinda sucks... Can't find a way around it.
As far as I know, you can't download videos that are blocked :/ A VPN could possibly work but I haven't been bothered enough to try
I know, But at least there should be a way to prevent an exception and crash!
An exception can't escape try/catch, unless you're debugging in VS
In my app, i have it encapsulated inside a try/catch and it doesn't escape and logs fine so must be another problem your side?
how does your catch look like?
I can't reproduce this at all. This is possible unless you rethrow exception in catch, or you have try/finally without catch.
I can reproduce this with:
#! "netcoreapp2.0"
#r "nuget: YoutubeExplode, *"
using YoutubeExplode;
try
{
var url = "https://www.youtube.com/watch?v=iN8PKcNGcuI";
if (url != "")
{
var id = YoutubeClient.ParseVideoId(url); // "bnsUkE8i0tU"
var client = new YoutubeClient();
var streamInfoSet = await client.GetVideoMediaStreamInfosAsync(id);
}
}finally{
}
and
#! "netcoreapp2.0"
#r "nuget: YoutubeExplode, *"
using YoutubeExplode;
try
{
var url = "https://www.youtube.com/watch?v=iN8PKcNGcuI";
if (url != "")
{
var id = YoutubeClient.ParseVideoId(url); // "bnsUkE8i0tU"
var client = new YoutubeClient();
var streamInfoSet = await client.GetVideoMediaStreamInfosAsync(id);
}
}catch{
throw;
}
but with:
#! "netcoreapp2.0"
#r "nuget: YoutubeExplode, *"
using YoutubeExplode;
try
{
var url = "https://www.youtube.com/watch?v=iN8PKcNGcuI";
if (url != "")
{
var id = YoutubeClient.ParseVideoId(url); // "bnsUkE8i0tU"
var client = new YoutubeClient();
var streamInfoSet = await client.GetVideoMediaStreamInfosAsync(id);
}
}catch{
Console.WriteLine("Catch");
}
I get output catch and there are no unhandled exceptions thrown.
Notice:
I tried to run my program normally (Not using visual studio) and it just crashed!
Here is my code... (I don't know why it looks so messy, I used the 'code' tab)
private async void LoadMusic()
{
try
{
//string Path = Directory.GetCurrentDirectory();
//GrantAccess(Path);
var url = GetYoutubeUrl();
if (url != "")
{
var id = YoutubeClient.ParseVideoId(url); // "bnsUkE8i0tU"
var client = new YoutubeClient();
var streamInfoSet = await client.GetVideoMediaStreamInfosAsync(id);
var streamInfo = streamInfoSet.Audio.WithHighestBitrate();
var ext = streamInfo.Container.GetFileExtension();
await client.DownloadMediaStreamAsync(streamInfo, Directory.GetCurrentDirectory() + "\\" + id + $".{ext}");
//File.WriteAllBytes(Path + video.FullName.Replace('-', '_'), video.GetBytes()
var inputFile = new MediaFile { Filename = Directory.GetCurrentDirectory() + "\\" + id + $".{ext}" };
var outputFile = new MediaFile { Filename = Directory.GetCurrentDirectory() + "\\" + id + $".{"wav"}" };
using (var engine = new Engine())
{
engine.GetMetadata(inputFile);
engine.Convert(inputFile, outputFile);
}
//axWindowsMediaPlayer1.URL = Directory.GetCurrentDirectory() + "\\" + id + $".{ext}";
player.SoundLocation = Directory.GetCurrentDirectory() + "\\" + id + $".{"wav"}";
player.Load();
if (!StopPlaying) player.Play();
}
}
catch (InvalidCastException e) { MessageBox.Show(e.Message); }
}
Can you format your code properly? Use three backticks.
I did, hope it's more clear now
@SlowLogicBoy yep mine is fine too. my try/catch is same as yours - pic
@AhmadEgb it might be crashing because you're only catching one Exception type which is InvalidCastException.
Either add a catch for YoutubeExplode.Exceptions.VideoUnavailableException or add a generic catch as shown in my picture above
Well you are only catching InvalidCastException
Most helpful comment
how does your catch look like?
I can't reproduce this at all. This is possible unless you rethrow exception in catch, or you have try/finally without catch.
I can reproduce this with:
and
but with:
I get output catch and there are no unhandled exceptions thrown.