Each video on YouTube has a list of available video qualities (144p, 240p, 360p and e.t.c.). Moreover, we don't see it, but YouTube can also provide us a list of available audio qualities and an additional list of video qualities, which are hidden. This article describes, how to get an access each available video and audio and play it via MFormats or MPlatform SDK.
For working with YouTube links, our SDKs use a third part utility - "yt-dlp.exe". The utility itself has an enormous functionality, but this article covers only the most essential. To get acquainted with all the functionality of this utility, visit the GitHub -> https://github.com/ytdl-org/youtube-dl/blob/master/README.md
Before we begin, it's needed to draw your attention one more time to the fact that "yt-dlp.exe" - is a third part utility. This means that at any time this utility may stop working correctly with YouTube without our knowledge. Please, always pay your attention on this fact and be careful, when you integrate "yt-dlp.exe" in your solutions.
Working with yt-dlp.exe
First of all, we will learn how to work with the utility in manual mode for a better understanding. The "yt-dlp.exe" utility can be found in the following paths (x64 and x86 folders contain the same versions of "yt-dlp.exe"):
- "C:\Program Files (x86)\Medialooks\MPlatform SDK\Bin\x64\DecoderLib\yt-dlp.exe"
- "C:\Program Files (x86)\Medialooks\MPlatform SDK\Bin\x86\DecoderLib\yt-dlp.exe"
- "C:\Program Files (x86)\Medialooks\MFormats SDK\Bin\x64\yt-dlp.exe"
- "C:\Program Files (x86)\Medialooks\MFormats SDK\Bin\x86\yt-dlp.exe"
When we know the utility location, let's open Command Prompt and try to get the lists of available video and audio qualities of this famous video https://www.youtube.com/watch?v=aqz-KE-bpKQ. To do that, it's needed to write the command in the following way: <Path to the utility> -F <Video URL>.
"C:\Program Files (x86)\Medialooks\MFormats SDK\Bin\x64\yt-dlp.exe" -F https://www.youtube.com/watch?v=aqz-KE-bpKQ
If you have done everything correctly, you see the following output. The most interesting information for us - is IDs of available videos and audios. Knowing them, we can select desirable video and audio qualities for playing.
Now, when we know the IDs of audios and videos, we can play the video from YouTube in desirable video and audio qualities. To do that via MFileClass or MFReaderClass instances, it's needed to define arguments of .FileNameSet() and .ReaderOpen() methods for the MFileClass or MFReaderClass instances respectively.
- The first argument is the URL of the video -> https://www.youtube.com/watch?v=aqz-KE-bpKQ
- The second argument has the following structure -> youtube.options='-f <Video_ID>+<Audio_ID>'
For example, if we want to open the YouTube video with the following qualities (video: webm 426x240 ID = 242, audio: webm 128kbit opus ID = 251), it will look like that:
m_objFile.FileNameSet("https://www.youtube.com/watch?v=aqz-KE-bpKQ", "youtube.options='-f 242+251'");
m_objReader.ReaderOpen("https://www.youtube.com/watch?v=aqz-KE-bpKQ", "youtube.options='-f 242+251'");
Please, draw your attention to the features, that you can't select audio only (for example: youtube.options='-f 251'). In this case you will receive just an exception, but you still can select video only (for example: youtube.options='-f 242'). Choosing the video only, you won't have an audio track during playback. There are also might be several options in the list, which contains both the audio and video (IDs: 17, 18 and 22 in our case). If you select one of these IDs, you will get both the audio and video at once.
There might be cases when you just don't need to select specific audio and video qualities. For such an occasion you can use a useful shortcut, which selects suitable qualities for you, according to the written conditions. For example, if you need to play the video in a resolution which isn't higher than 360p and in the best available audio, you can put this string as the second argument:
More useful shortcuts and their detailed descriptions of the "yt-dlp.exe" utility can be found on its GitHub page -> https://github.com/ytdl-org/youtube-dl/blob/master/README.md
Working with yt-dlp.exe through code
There are many scenarios where you can't afford to run "yt-dlp.exe" separately through the command prompt, getting the link lists and manually construct arguments for .FileNameSet() or .ReaderOpen() methods for the MFileClass or MFReaderClass instances. This paragraph covers one of the possible approach how to interact with the utility via C# code.
Fortunately for us, C# has good tools to work with third party console applications which allow us to pass start arguments and capture application output. "System.Diagnostics.Process" class is needed for our goals. Let's declare, initialize and fill fields of an instance of the class to work with "yt-dlp.exe".
string URL = @"https://www.youtube.com/watch?v=aqz-KE-bpKQ";
Process process = new Process();
process.StartInfo.FileName = @"yt-dlp.exe"; // Path to yt-dlp.exe
process.StartInfo.Arguments = @"-F " + URL; // URL string of the YouTube video
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
// Array of strings to store the utility output:
string  AppOut = process.StandardOutput.ReadToEnd().Split('\n');
After the above block of code has been run, AppOut array of strings will look something like that:
Now we have collected all the necessary data to choose any of available video and audio quality of the video. To play the video in the desirable video and audio quality, it's needed to parse the gotten utility output somehow to get needed IDs and compose the arguments for .FileNameSet() or .ReaderOpen() methods for the MFileClass or MFReaderClass instances.
Cause of the fact, that parsing and composing logics aren't related to the SDKs themselves, we haven't described how to implement them in this article. It's not a small block of code which doesn't take much space both in your code and in this article. Moreover, for different scenarios, the different parsers are needed. So, instead of it, we have attached a sample which can play videos from YouTube in the desirable video and audio quality. You are welcome to explore it (see the attachments).