RayT
asked on
Modifying Audio Metadata Using PowerShell
Is there an easy way to modify audio file (mp3, wav, etc.) using PowerShell? I only want to modify the Title and Album.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As others have said, it's not the easiest path using PowerShell to edit this. Using TagLib-Sharp isn't terrible, but you do have to have some experience to put it all together and modify it for your own purposes/situation.
Here's some sample code:
You would have to download the taglib-sharp DLL. You can get it from https://www.nuget.org/packages/TagLibSharp/ Just download the nuget package, and then extract the files from that (renaming the package with a .zip extension might make it easier for you).
The script has to reference the correct path to the DLL. The DLL file downloaded from the internet has to be unblocked (right-click > Properies > Unblock > OK) for PowerShell to use it properly.
You would also have to develop the criteria by which you're going to supply the title and album info. The sample above just has static strings, so if you ran it every file processed would get the same info.
Are you aware of limitations with .WAV files and metadata? Don't expect it to work/be supported the same as .MP3 files.
Here's some sample code:
$sourceFolder = "C:\Music"
$TagLibDLL = "c:\temp\taglib-sharp.dll"
Add-Type -Path (Resolve-Path $TagLibDLL)
Get-ChildItem -LiteralPath $sourceFolder -File -Filter *.mp3 | Select-Object -ExpandProperty Fullname | ForEach-Object `
{
$file = $_
# Change select metadata and save the file
[TagLib.File]::Create($file) | ForEach-Object {
$_.Tag.Title = "whatever" <# need some criteria #>
$_.Tag.Album = "whatever" <# need some criteria #>
$_.Save()
$_.Dispose()
}
}
You would have to download the taglib-sharp DLL. You can get it from https://www.nuget.org/packages/TagLibSharp/ Just download the nuget package, and then extract the files from that (renaming the package with a .zip extension might make it easier for you).
The script has to reference the correct path to the DLL. The DLL file downloaded from the internet has to be unblocked (right-click > Properies > Unblock > OK) for PowerShell to use it properly.
You would also have to develop the criteria by which you're going to supply the title and album info. The sample above just has static strings, so if you ran it every file processed would get the same info.
Are you aware of limitations with .WAV files and metadata? Don't expect it to work/be supported the same as .MP3 files.
ASKER
Thanks! Looks like taglib-sharp.dll is thing to use.
ASKER
I would have said footech gave the best response. I tried the code and it did not work. I ended creating my own solution
[System.Reflection.Assembly]::LoadFile("C:\temp\taglib-sharp.dll" )
foreach ($Project in $Projects) {
$Source = $SourceStudioProjects + $Project
$Files = @(Get-ChildItem -Path $Source -Recurse)
foreach ($iFile in $Files) {
$mediaFile=[TagLib.File]::Create($iFile.fullname)
$mediaFile.Tag.Album = $iFile.Directory.Name
$mediaFile.Tag.Title = $iFile.Name -creplace $iFile.Extension,""
$mediaFile.Save()
}
}
The only problem I have is the $mediaFile.Tag.Album is not being set. Don't know why. Still researching the problem.
[System.Reflection.Assembly]::LoadFile("C:\temp\taglib-sharp.dll" )
foreach ($Project in $Projects) {
$Source = $SourceStudioProjects + $Project
$Files = @(Get-ChildItem -Path $Source -Recurse)
foreach ($iFile in $Files) {
$mediaFile=[TagLib.File]::Create($iFile.fullname)
$mediaFile.Tag.Album = $iFile.Directory.Name
$mediaFile.Tag.Title = $iFile.Name -creplace $iFile.Extension,""
$mediaFile.Save()
}
}
The only problem I have is the $mediaFile.Tag.Album is not being set. Don't know why. Still researching the problem.
I cannot detect any flaw with footech's code (beyond the changes required to set the album), and I would prefer a mixed style.
Add-Type -Path 'c:\temp\taglib-sharp.dll'
foreach ($Project in $Projects) {
Get-ChildItem -LiteralPath ($SourceStudioProjects + $Project) -File -Recurse |
% {
$file = $_
$media = [TagLib.File]::Create($file.FullName)
$media.Tag.Title = $file.BaseName
$media.Tag.Album = $file.Directory.Name
$media.Save()
$media.Dispose()
}
}
ASKER
I'll try it again and get back with you.
Forgot to say that setting the album tag worked (for mp3 files).
ASKER
The following code is still malfunctioning. The Title is updated, but the Album is not. Why? How do I fix?
Add-Type -Path 'C:\Users\zzz\Desktop\Music Dev\Post Session Actions\taglib-sharp.dll'
foreach ($Project in $Projects) {
$Source = $SourceStudioProjects + $Project
$Files = @(Get-ChildItem -Path $Source -Recurse)
foreach ($file in $Files) {
$media =[TagLib.File]::Create($file.fullname)
$media.Tag.Title = $file.BaseName
# the following line fails, $file.Directory.Name contains the correct value
$media.Tag.Album = $file.Directory.Name
$media.Save()
$media.Dispose()
}
}
Add-Type -Path 'C:\Users\zzz\Desktop\Music Dev\Post Session Actions\taglib-sharp.dll'
foreach ($Project in $Projects) {
$Source = $SourceStudioProjects + $Project
$Files = @(Get-ChildItem -Path $Source -Recurse)
foreach ($file in $Files) {
$media =[TagLib.File]::Create($file.fullname)
$media.Tag.Title = $file.BaseName
# the following line fails, $file.Directory.Name contains the correct value
$media.Tag.Album = $file.Directory.Name
$media.Save()
$media.Dispose()
}
}
ASKER
Maybe that's the problem. How can this be changed to work for mp3, wav and flac files?
For what it's worth, I ran the code I posted and it worked fine. I tested with .MP3. I don't have any .FLAC files to test with, and .WAV files don't work the same.
I see nothing in the modifications that would cause anything to fail. What type of file are you seeing the Album tag not set, and how are you viewing that info? Are you getting an error message?
I see nothing in the modifications that would cause anything to fail. What type of file are you seeing the Album tag not set, and how are you viewing that info? Are you getting an error message?
ASKER
No error message. The application ran but did not update the Album. Only the Title was updated.
What type of file are you seeing the Album tag not set, and how are you viewing that info?
If you want to go down the Powershell line then use something like the TagLib library Qlemo mentioned as described here:
https://solvedbypowershell.blogspot.com/2013/02/powershell-id3-tag-editing-via-taglib.html