Youtubeexplode: An example on how to stream a video to the mediaplayer?

Created on 22 Feb 2018  路  8Comments  路  Source: Tyrrrz/YoutubeExplode

I want to play a youtube video in a mediaplayer without the need of downloading the whole video (stream)

question

All 8 comments

When you say media player, do you mean a video rendering control like WPF's media player?

yes, WPF mediaelement

You can probably just feed it the URL

Example using Vlc.DotNet.Core.
However I only play audio here.
I used RoslynPad to run this.

#r "$NuGet\AngleSharp\0.9.9\lib\net45\AngleSharp.dll"
#r "$NuGet\Newtonsoft.Json\10.0.3\lib\net45\Newtonsoft.Json.dll"
#r "$NuGet\YoutubeExplode\4.1.1\lib\net45\YoutubeExplode.dll"
#r "$NuGet\Vlc.DotNet.Core\2.2.1\lib\net45\Vlc.DotNet.Core.dll"
#r "$NuGet\Vlc.DotNet.Core.Interops\2.2.1\lib\net45\Vlc.DotNet.Core.Interops.dll"

using Vlc.DotNet.Core;
using Vlc.DotNet.Core.Interops.Signatures;
using YoutubeExplode;
using YoutubeExplode.Models.MediaStreams;

public const string VlcPath = @"C:\Program Files (x86)\VideoLAN\VLC";

var yt = new YoutubeClient();
var video = await yt.GetVideoMediaStreamInfosAsync("ETrhmFC_1TA");
var audio = video.Audio.WithHighestBitrate();

using(var client = new VlcMediaPlayer(new DirectoryInfo(VlcPath)))
{

    client.SetMedia(new Uri(audio.Url));
    client.Play();
    while (client.State != MediaStates.Ended)
    {
        Thread.Sleep(1000);
    }
}

Edit:
There are Wpf and WinForms controls for video in Vlc.DotNet.Wpf and Vlc.DotNet.Forms, they both have VlcControl to show video.

Of course this has it's downsides that machine has to have vlc installed and you have to choose x86 or x64 to use. Here is info for more details.

@SlowLogicBoy You can also use WithHighestBitrate extension method ;)

If you pass video to Client it opens new window and shows you the video:

#r "$NuGet\AngleSharp\0.9.9\lib\net45\AngleSharp.dll"
#r "$NuGet\Newtonsoft.Json\10.0.3\lib\net45\Newtonsoft.Json.dll"
#r "$NuGet\YoutubeExplode\4.1.1\lib\net45\YoutubeExplode.dll"
#r "$NuGet\Vlc.DotNet.Core\2.2.1\lib\net45\Vlc.DotNet.Core.dll"
#r "$NuGet\Vlc.DotNet.Core.Interops\2.2.1\lib\net45\Vlc.DotNet.Core.Interops.dll"

using Vlc.DotNet.Core;
using Vlc.DotNet.Core.Interops.Signatures;
using YoutubeExplode;
using YoutubeExplode.Models.MediaStreams;

public const string VlcPath = @"C:\Program Files (x86)\VideoLAN\VLC";

var yt = new YoutubeClient();
var video = await yt.GetVideoMediaStreamInfosAsync("ETrhmFC_1TA");
var muxed = video.Muxed.WithHighestVideoQuality();

using(var client = new VlcMediaPlayer(new DirectoryInfo(VlcPath)))
{
    client.SetMedia(new Uri(muxed.Url));
    client.Play();
    while (client.State != MediaStates.Ended)
    {
        Thread.Sleep(1000);
    }
}

@jvitoroc did your question get answered?

@SlowLogicBoy @Tyrrrz That's not what I'm looking for, maybe I'll stay with Electron.js.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ac-lap picture ac-lap  路  7Comments

Luke1998x picture Luke1998x  路  5Comments

vsjtechnologies picture vsjtechnologies  路  7Comments

nk34player picture nk34player  路  4Comments

Tyrrrz picture Tyrrrz  路  4Comments