#
YoutubeDownloader Powershell Module
#
Hopefully something useful 📹
Okay well the title is debatable, but I don't think there is any need for me to explain to you what YouTube is, I am sure you have heard it, used it and maybe even starred in one of your own videos on there. I do think it was a great idea at the start, then obviously google took over it, and well the rest is history. Anyways it is still a great site for obtaining videos on something you are interested in. I have learnt some great things off of YouTube, however sometimes I cannot finish watching the video I am watching or I need to watch it several more times for the information that I am trying to digest to make total sense to me. Well for me it's a whole lot easier if I have a copy of this video but on my local hard disk. That way I can watch it in VLC player which is a far superior media player to any media player on a web interface.
Just because I think VLC is superior it doesn't mean it is, thats just my opinion. Like I think The Realm is one amazing comic you have to read, again my opinion
#
Past solution to problem 🧩
Pretty much what I think many others have resorted to, using your favourite search engine to ask the quesiton, and get back a whole host of hyperlinks to select from. As you are also probably aware the internet is not always the safest place, and I personally only like to visit sites I know are safe. However I always get a lurking suspicion that not all youtube video download sites are completely safe. Although I have used one of these sites in the past, it never occured to me to think about making a Powershell binary module to achieve the task. So I guess that inspired this module to become a reality.
Is there a better way?
Just because you know how to do something one way doesn't make it the only way, or even the best way
#
What is out there already? 👓
As always I had a quick butchers on the Powershell gallery before embarking on this project, and checking to see what was out there on the gallery. A total of 10 results were returned, one of them being the UniversalDashboard player I did a while back. I did see that a few of these modules did mention the ability to download the video or song. However it seemed that the majority of these modules did not allow you to download. For me that was a good enough excuse to add another Powershell module to the list, and make it nice and clear it did the thing I was looking for download youtube video. So YoutubeDownloader was set for action.
Keep Reading
No spoiler alerts but must read solution below
#
Where did you start? 🧗♂️
Pretty much the same place I started for all my other binary Powershell modules I have made. Which is by having a look on https://www.nuget.org/ to see what is out there, and how I can bring that to an easy-to-use Powershell cmdlet. From having a look through the results returned I decided to use https://www.nuget.org/packages/VideoLibrary this as my choice of package to accomplish my mission. I selected this as there was documentation and it seemed easy enough to follow. The one thing I got reminded by doing this project, is that you really have to read the manual don't try and be a typical male and get building with the example, else you might miss the fact that although on nuget it states dotnet 1.3 on the github project page it says dotnet 4.5 so I like to keep things as backwards compatible as possible, because it wasn't all that long ago I had to install Powershell 7 so I could get building Crescendo modules for Powershell. When you get started on building the binary module the first command you have to issue is dotnet new classlib which actually defaults to dotnet 6 so to make this backwards compatible I used a Powershell 5 compatible dotnet version.
#
Next building steps 👨🔧
So once you issue the dotnet new classlib command in visual studio this builds a .csproj file for you and a .cs file. On the nuget site on the package you want to use there is a .NET CLI tab which allows you to copy the code to use the package within your project. So after adding the powershell, and videolibrary package I had a .csproj file which looked like:-
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net4.7.2</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" />
<PackageReference Include="VideoLibrary" Version="3.1.9" />
</ItemGroup>
</Project>
Within the .cs file I now had to make the binary module which will be output as a .dll file once the project can be built and published without any errors. Again my knowledge on C-Sharp is very limited, but I am always up for a challenge, and plus the example looked very simple 😁 this allowed me to create this as the .cs file:-
using System.Management.Automation;
using VideoLibrary;
namespace YoutubeDownload
{
[Cmdlet(VerbsData.Save, "YoutubeVideo")]
public class Program : PSCmdlet
{
[Parameter(HelpMessage = "Enter the full youtube url for the video you want to download, best to enclose this url in speech-marks", Mandatory = true)]
public string? URL { get; set; }
[Parameter(HelpMessage = "Enter the directory to where you want the video output, it will automatically name the video and extension, so just enter directory path to where you want to download it", Mandatory = true)]
public string? Path { get; set; }
protected override void EndProcessing()
{
var youTube = YouTube.Default;
var video = youTube.GetVideo(URL);
File.WriteAllBytes(Path + video.FullName, video.GetBytes());
}
}
}
Trust this is the least code I have ever had to do to get a binary module at the end of it. So the next two steps after having the .csproj and .cs file ready:-
dotnet build
dotnet publish
You can obviously only run the publish command if the build command runs without errors at the end of it. As this means your project has all the required libraries and the code is all good for this project to be published. After running the publish command you get the holy grail the .dll file which will allow you to load this into Powershell as a module. So now I needed to open a new Powershell session outside of VS Code which I was using to build this binary module. I then issued the Import-Module cmdlet and entered the path to my .dll file which was created. I gave the module a quick spin and it all worked as planned. When giving the module a quick spin I mean I first verify the cmdlet is showing in the Get-Command -module YoutubeDownloader then I make sure the cmdlet actually works as planned. As I only had one cmdlet within this module, this allowed very quick testing. Next on the menu is to create the .psd1 file which the majority of Powershell users associate modules with. I did this with the New-ModuleManifest cmdlet and passed all the appropriate parameters. I then like to test the module again to make sure I have all the required .dll files as nested modules and the root module .dll file all work when you move the folder to a new location and load from a new Powershell session using the newly created .psd1 file. If all goes well then the final step is to share your hardwork with the community to allow someone else to benefit from using your module. For me this means to publish it to the Powershell gallery. However before I issue that cmdlet I like to make a quick project site, which is a github repository containing the module and an icon file I like to associate with the module. most importantly making sure the repository includes a README which contains some informaiton on the module and how to use it. By creating this github repository and linking this as the project site, this then reflects on the Powershell gallery. Now we can do the finally bit and publish the module to the Powershell gallery.
#
Mission accomplished 🎰
Publish-Module was next on the agenda, quickly making sure before-hand I had all the information I wanted in the .psd1 file. So many times I have published a module, and forgot to include the project page, or the icon, then you have to edit the version of the module and include the missing information then republish. It's more annoying than time-consuming, so always best to triple check things. After a few minutes you should be able to check your module on the Powershell gallery. Even though this was my 117th module I have now published to the Powershell gallery, it always gives me a sense of great achievement of sharing something you have created. I never did University, in-fact I only did a year at college, and IT wasn't even really a thing at school. I got into IT through an apprenticeship paying me a whole £50 a week. I started at the bottom and been trying to climb that ladder ever since. Self teaching myself Powershell wasn't an over-night thing, it took longer than a month, several months. But you keep learning eventually it makes sense. I did believe that Powershell would explode for sysadmin type people managing lots of machines, but to me from what I seen in many work-places it's just not that way at all, Windows admins are point and click type of breed that is not moving away from a full-on GUI anytime soon.
Undiscovered Country Image comics. This is another amazing comic you need to check out
#
Example 👇
The emphasis behind this was the KISS approach Keep It Simple Stupid I wanted to be able to use this module and get a video I wanted downloaded in the time it would take me to find a valid website to do the same thing online, or even better less. I think I got that result, just check how simple this is to use:-
Save-YoutubeVideo -URL "https://www.youtube.com/watch?v=c9m7ZdSwgkQ" -Path C:\
Thanks for reading