Well, it’s Saturday night and I was watching some of the old Powershell tutorials, which I had downloaded from Microsoft Virtual Academy.
THE PROBLEM :
But problem with these downloaded tutorials is that – They have a Default download name, and one can barely understand the Content inside each vedio. see in the below picture.
SOLUTION :
Media players like, iTunes and Windows Media Player displays detailed information for all the media files, but where do they get this information from?
Answer to this is METADATA of that file .
So why not dig this Metadata to get more information about the files and see if we can get something useful, hence the following script.
SCRIPT :
Function Get-MP3MetaData | |
{ | |
[CmdletBinding()] | |
[Alias()] | |
[OutputType([Psobject])] | |
Param | |
( | |
[String] [Parameter(Mandatory=$true, ValueFromPipeline=$true)] $Directory | |
) | |
Begin | |
{ | |
$shell = New-Object -ComObject "Shell.Application" | |
} | |
Process | |
{ | |
Foreach($Dir in $Directory) | |
{ | |
$ObjDir = $shell.NameSpace($Dir) | |
$Files = gci $Dir| ?{$_.Extension -in '.mp3','.mp4'} | |
Foreach($File in $Files) | |
{ | |
$ObjFile = $ObjDir.parsename($File.Name) | |
$MetaData = @{} | |
$MP3 = ($ObjDir.Items()|?{$_.path -like "*.mp3" -or $_.path -like "*.mp4"}) | |
$PropertArray = 0,1,2,12,13,14,15,16,17,18,19,20,21,22,27,28,36,220,223 | |
Foreach($item in $PropertArray) | |
{ | |
If($ObjDir.GetDetailsOf($ObjFile, $item)) #To avoid empty values | |
{ | |
$MetaData[$($ObjDir.GetDetailsOf($MP3,$item))] = $ObjDir.GetDetailsOf($ObjFile, $item) | |
} | |
} | |
New-Object psobject -Property $MetaData |select *, @{n="Directory";e={$Dir}}, @{n="Fullname";e={Join-Path $Dir $File.Name -Resolve}}, @{n="Extension";e={$File.Extension}} | |
} | |
} | |
} | |
End | |
{ | |
} | |
} | |
#ForEach($item in ("D:\Powershell\Tutorials\4_DSC" |Get-MP3MetaData)){ | |
# $NewName = [regex]::Replace($(($item.Title).Split(":")[1].Trim() + $item.extension),"[*(/)\\&#]",{''}) | |
# $Oldname = $item.Fullname | |
# Rename-Item -LiteralPath $item.Fullname -NewName $NewName -Force | |
#} |
HOW IT WORKS :
Just feed the function with the directory path, where all your .MP3 or .MP4 files are located and you’ll get the MetaData associated with each file, like in the below picture.
You can use this MetaData in many different ways, like I used it to Rename the downloaded files to solve my problem in the following animation.
Or, you can move all the songs which you’ve downloaded to a folder Categorized by their respective Album name, this is pretty handy too 😉 using the following script.
ForEach($item in ("C:\Users\Prateek\Downloads\music" |Get-MP3MetaData)){ | |
$Source = $item.Fullname | |
$Album = $item.Album | |
Set-Location C:\Music | |
If(-not (gci | ?{$_.name -eq "$Album" -and $_.PSisContainer})) | |
{ | |
New-Item -Name $Album -ItemType Directory -Force |Out-Null | |
} | |
$destination = "C:\Music\$album" | |
Move-Item -Path $Source -Destination $destination -Force -Verbose | |
} |
and you’ll end up with folder structures with the Album name’s from the MetaData that has all your songs categorized properly within these folders.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ForEach($item in ("C:\Users\Prateek\Downloads\music" |Get-MP3MetaData)){ | |
$Source = $item.Fullname | |
$Album = $item.Album | |
Set-Location C:\Music | |
If(-not (gci | ?{$_.name -eq "$Album" -and $_.PSisContainer})) | |
{ | |
New-Item -Name $Album -ItemType Directory -Force |Out-Null | |
} | |
$destination = "C:\Music\$album" | |
Move-Item -Path $Source -Destination $destination -Force -Verbose | |
} |
Hoping you’ll find it fun!! 🙂 Happy weekends!
Prateek Singh
Follow @SinghPrateik
Shell object is not created
LikeLike
Hi Prasoon, I’ve fixed that. please check now. Thank you.
LikeLike
[…] on October 16, 2016 submitted by /u/Prateeksingh1590 [link] [comments] Leave a […]
LikeLike
Very nice! I’ve written similar scripts in the past for exif data in .jpg files. I would be very grateful if you could abstract SETTING metadata for .mp3 into a function. that’s something I haven’t figured out how to do without using external tools.
LikeLike
Ever considered using taglib-sharp.dll for these tasks?
I’m not really into .COM objects, it’s still a bit too complicated for me, but once I have turned lazy Sunday into PS Sunday with fixing some common errors in my mp3/flac library.
LikeLike
VMware Certified Advanced Professional 6 (Desktop and Mobility Deployment) – The industry-recognized VCAP6-DTM Deploy certification validates that you know how to deploy and optimize VMware Horizon 6 (with View) environments. It demonstrates that you have the understanding and skills vital to leverage best practices to provide a scalable and dependable Business Mobility platform for your organization. Some of the subject areas involve: Configuring and managing Horizon View components, configuring cloud pod archituecture, configuring Group Policy settings related to Horizon View, Configuring and optimizing desktop images for Horizon View & Mirage, Configuring and managing App Volumes AppStacks, Configuring desktop pools, Configuring and deploying ThinApp packaged applications, Configuring VMWare Identity Manager, etc.Szumigalski.com was created by Sebastian to propagate his passion for PowerShell & VMWare. Sebastian is an IT skilled professional employed in Singapore for more than 15 years who’s often searching for innovative technology to sharpen his technical abilities & understanding. Ever since, Sebastian has joined PowerShell User Group & VMWare VMug group, and has also been taking part in all the meetings held in Singapore. This weblog will show you how Sebastian manage to automate his every day undertakings with the help of PowerShell. You can find study courses meant for the VCAP6-DTM exam, which are personally created by Sebastian. Sebastian is licensed with VCAP6-DTM, and is skilled with virtualization & server maintenance from four years experience of automation. The need for VMWare prepared admins and engineers are ever-increasing in today’s tech marketplace. Learn about more about PowerShell & VMWare at Szumigalski.com!
LikeLike
[…] to everyone, I am pretty new to this and I am trying to follow this to move 200 + gigs of music that are on in one folder to move into the Album that is in the […]
LikeLike
@MaciekzKlanu
taglib-sharp.dll/ Set-Id3Tag does not write the the correct values for Genre (ID3V2.TCON) . There may be others.
The write routines in the File object do not have this bug.
LikeLike