Introduction
Each video on YouTube offers a range of video quality options (144p, 240p, 360p, etc.). However, beyond the visible options, YouTube also provides a list of available audio qualities and additional hidden video qualities. This article explores how to access all available video and audio formats and play them using MFormats or MPlatform SDK.
To work with YouTube links, our SDKs leverage a third-party utility called yt-dlp.exe. This tool offers extensive functionality, but this article focuses on the most relevant aspects. For a comprehensive overview of yt-dlp.exe, visit its GitHub page.
Note: Since yt-dlp.exe is a third-party tool, it may stop functioning correctly with YouTube at any time. Always keep this in mind when integrating it into your solutions.
Working with yt-dlp.exe and SDK
The utility can be found in the following paths:
MPlatform:
C:\Program Files (x86)\Medialooks\MPlatform SDK\Bin\x64\DecoderLib\yt-dlp.exe
MFormats:
C:\Program Files (x86)\Medialooks\MFormats SDK\Bin\x64\yt-dlp.exe
To retrieve the available video and audio qualities, use the following command in the Command Prompt:
<Path to the utility> -F <Video URL>
Example:
PATH\yt-dlp.exe -F https://www.youtube.com/watch?v=aqz-KE-bpKQ
The output will display IDs for available video and audio formats, which are essential for selecting the desired quality.
Playing YouTube videos in desired quality
Once you have the format IDs, you can play a YouTube video in the selected quality using the following structure: youtube.options='-f <Video_ID>'.
Example: Selecting video (mp4 1920×1080 60 fps, ID = 399):
MPlatform:
m_objFile.FileNameSet("https://www.youtube.com/watch?v=aqz-KE-bpKQ", "youtube.options='-f 399'");
MFormats:
m_objReader.ReaderOpen("https://www.youtube.com/watch?v=aqz-KE-bpKQ", "youtube.options='-f 399'");
Note:
Selecting audio only (e.g., youtube.options='-f 251') will result in an exception.
Selecting video only (e.g., youtube.options='-f 242') will play the video without audio.
Some IDs (e.g., 17, 18, 22) include both video and audio, allowing playback without separate selection.
Automatic selection of best quality
If specific quality selection is not required, use shortcuts to let yt-dlp.exe choose the best options. For example, to play a video at a maximum of 720p with the best available audio:
youtube.options='-f bestvideo[height<=720]+bestaudio'
For more examples, refer to the yt-dlp GitHub page.
Running yt-dlp.exe via code
You can work with yt-dlp.exe programmatically using C#. C# provides tools to work with console applications, allowing you to pass arguments and capture output using the System.Diagnostics.Process class.
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;
process.Start();
// Array of strings to store the utility output:
string [] AppOut = process.StandardOutput.ReadToEnd().Split('\n');
Once you have retrieved the output, the next step is to parse it to extract the required video and audio IDs:
How to update yt-dlp.exe
If you encounter an error when opening a YouTube video, it's likely that yt-dlp.exe needs to be updated. You can update it using one of the following methods:
Method 1: Manually Replace yt-dlp.exe
-
Download the latest version of yt-dlp.exe from its official repository:
GitHub - yt-dlp Releases -
Locate the existing yt-dlp.exe file in your SDK or your application directory:
MPlatform:
C:\Program Files (x86)\Medialooks\MPlatform SDK\Bin\x64\DecoderLib\yt-dlp.exe
MFormats:
C:\Program Files (x86)\Medialooks\MFormats SDK\Bin\x64\yt-dlp.exe
-
Replace the old yt-dlp.exe with the newly downloaded version.
Method 2: Automatic Update Using Command Prompt
Run the following command in Command Prompt to update yt-dlp.exe automatically:
PATH\yt-dlp.exe -U
This command will check for updates and install the latest version.
By following these methods, you can ensure that yt-dlp.exe remains up to date and continues working correctly with YouTube videos.