Can Powershell independently extract LYRICS audio metadata from multiple flac files to multiple text files?

36 Views Asked by At

I have some flac audio files that have synced (timed) lyrics metadata stored in the LYRICS tag. Here is an example from a screenshot taken from the program Mp3tag:

FLAC file with LYRICS metadata

Currently I have used two methods to extract the metadata for each flac file to a respective text file with the same name as the flac file.

Method 1: Mp3tag + Powershell

I used a method provided by user ryerman on the Mp3Tag forums, here

In this method, I do as follows:

Mp3tag, Options, Tools, New

Name: Export LYRICS to TXT

Path: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Parameter:

-windowstyle hidden -command "Write-Output ''$regexp($regexp($regexp($replace(%lyrics%,'',''''),^\w+\|\|,),\r\n,'' ''),''\s''$,)'' | Out-File -FilePath ''$replace(%_folderpath%%_filename%,'','''').txt'' -Encoding Unicode"

This is a great method for me for within the Mp3tag program. At first I thought that I could potentially "reverse" the Powershell parameter shown above to regular Powershell code and thus the extraction could be done external to Mp3tag. My hope was that this would allow the extraction to be done fully and independently by Powershell if the parameter code was "reversed" correctly. But I am very new to all of this (Mp3tag and Powershell) and didn't really understand what was happening with this Mp3tag Tool. I wrote into the Mp3tag forum post and ryerman clarified for me that in his Mp3tag tool, "Mp3tag extracts the lyrics which are then processed by Powershell cmdlets." Therefore, his code cannot be "reversed" to become Powershell independent - it requires Mp3tag.

Method 2: ffprobe + Powershell

For this method, I used two lines of code in Windows CMD:

for %%x in (*.flac) do ffprobe "%%x" -show_entries format_tags=lyrics -of compact=p=0:nk=1 -v 0 > "%%~dpnx.txt"
powershell -command "$filePath = '.\*.txt'; Get-ChildItem $filePath -Recurse | ForEach-Object {(Get-Content $_).Replace('\n',\"`r`n\") | Set-Content $_}"

The reason I had to do it in two steps is because for whatever reason the ffprobe command - while it did successfully extract the lyrics metadata - formatted the text files with literal "\n" strings instead of actual newline/carriage returns at the end of each line. The second command (Powershell within CMD) then "fixes" the text files so they have actual carriage returns for each line of lyrics.

I like ffprobe (or maybe even exiftool, which can do similar extractions), but I am trying to see if I could JUST use Powershell. Though they are fine, I would prefer to not use ffprobe or Mp3tag because I am trying to use as few "extra" 3rd party executables/dependencies as possible. Also, in some of my reading, I've also run across discussions of using Powershell with a "library" I believe called LibTag, but I am wondering if this LYRICS metadata extraction can be done JUST by Powershell.

Another user on the Mp3tag forum (LyricsLover) responded to my query and suggested the following Powershell code:

# Run in flac folder
$shell = New-Object -ComObject Shell.Application
$folder = $shell.Namespace((Get-Location).Path)
$folderItems = Get-ChildItem -Filter *.flac | foreach { $folder.ParseName($_) }

# 0 Name, 1 Size, 2 Item type, 13 Contributing artists, 14 Album, 15 Year
# 16 Genre, 20 Authors, 21 Title, 27 Length, 28 Bit rate
$properties = 21

foreach($item in $folderItems)
{
    $fileMetadata = @{}
    foreach($property in $properties)
    {
        $name = $folder.GetDetailsOf($folderItems, $property)
        $value = $folder.GetDetailsOf($item, $property)
        $fileMetadata.Add($name, $value)
    }
    [PSCustomObject]$fileMetadata
}

I got this code working as both a .ps1 script and also converted it to work in Windows CMD:

powershell -command "$shell = New-Object -ComObject Shell.Application;$folder = $shell.Namespace((Get-Location).Path);$folderItems = Get-ChildItem -Filter *.flac | foreach { $folder.ParseName($_)};$properties=21;foreach($item in $folderItems){$fileMetadata = @{};foreach($property in $properties){$name = $folder.GetDetailsOf($folderItems, $property);$value = $folder.GetDetailsOf($item, $property);$fileMetadata.Add($name, $value)}[PSCustomObject]$fileMetadata}"

I played around with entering different metadata "numbers" in this code corresponding to their property.

In the above code (as an example), I have entered number 21, corresponding to the metadata "Title." This returns to the console screen the title of each flac audio file in the working directory.

The biggest issue with this method, as LyricsLover and I discussed, is that the "number" associated with the metadata audio tag "Lyrics" doesn't seem to be known and (so far) hasn't been found. If I knew that number, I could plug it in and that code SHOULD return the text of metadata tag LYRICS, if I understand correctly.

Secondly, with regard to the above code: I would like to "extend" or modify the existing code as follows:

Take the result of the console window (in this case, #21, or the Title of the song) and export this to a separate text file for EACH flac file in the working directory, where each text file is based on the filename of the respective flac file.

If,

(1) I can find the number associated the metadata tag LYRICS and insert it in the code at "$properties = LYRICS #

(2) Modify/Extend the above Powershell code so that it exports the result of the metadata query to a separate text file for EACH flac file in the working directory, where each text file is based on the filename of the respective flac file

Then,

In my understanding this will be a "Powershell only/independent" method for doing what has been described above with Method #1 (Mp3tag + Powershell) or Method #2 (ffprobe + Powershell).

Would someone be able to provide the additional Powershell code to extend the existing Powershell code above to export the results to text files? As I said, I am very new to Powershell and though I have researched it, I haven't been able to figure out how to get this Powershell metadata console output exported to text files.

Further, is it possible to determine the "number" associated with the audio metadata tag LYRICS?

If this number can't be found, then is there ANOTHER Powershell independent method/code/script that could extract the LYRICS metadata for a folder of flac files to individual text files?

0

There are 0 best solutions below