Assistance/tutorial 在 PowerShell 中使用 Shazam API
Assistance/tutorial with Shazam API in PowerShell
我正在尝试使用 PowerShell 与 Shazam API 交互,但他们的文档主要在 PHP 中。我在 Python 的另一个论坛上找到了这个片段,据说可以工作:
from pydub import AudioSegment
import base64
import requests
import json
file_path="./test.raw"
url = "https://rapidapi.p.rapidapi.com/songs/detect"
encode_string = base64.b64encode(open(file_path, "rb").read())
payload=encode_string
print(type(payload))
headers = {
'content-type': "text/plain",
'x-rapidapi-key': "<<<your key>>>",
'x-rapidapi-host': "shazam.p.rapidapi.com"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(json.dumps(json.loads(response.text)))
我发现的文档中唯一与 PowerShell 相关的是这个小片段,但它似乎不是很有用:
$headers=@{}
$headers.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
$headers.Add("X-RapidAPI-Host", "shazam-core.p.rapidapi.com")
$headers.Add("X-RapidAPI-Key", "<<<your key>>>")
$response = Invoke-WebRequest -Uri 'https://shazam-core.p.rapidapi.com/v1/tracks/recognize' -Method POST -Headers $headers -ContentType 'multipart/form-data; boundary=---011000010111000001101001' -Body '-----011000010111000001101001
Content-Disposition: form-data; name="file"
-----011000010111000001101001--
'
我想按照其他人在 Python 中所做的方式进行操作,但我只需要帮助将其转换为 PowerShell。我已经找到了一种将音频文件转换为 base 64 并将其缩短为 500kb 的方法,类似于他们的做法:
$Bytes = [System.IO.File]::ReadAllBytes("file.mp3") | select -First 500000
$EncodedText = [Convert]::ToBase64String($Bytes)
我只需要知道如何使用 Invoke-WebRequest 格式化请求。
对于任何感兴趣的人,我计划制作一个纯粹使用 Shazam 的歌曲标记器,它可能最终会在某个时候获得一个 GUI,但我还没有决定那么远,一旦我能摆脱这个障碍其他一切都应该很顺利。
编辑:
获得帮助后,这就是我最终从音频文件到 shazam api 输出的方式:
$Track = "Thru the 7th with My Woadies.mp3"
$RAWTemp = $PSScriptRoot + "\Temp.pcm"
$SampleRateExpression = "(($PSScriptRoot\ffmpeg\bin\ffprobe.exe -loglevel 0 -print_format json -show_format -show_streams '$Track' | ConvertFrom-Json).streams).sample_rate"
$SampleRate = Invoke-expression $SampleRateExpression
$ConvertToRawExpression = "$PSScriptRoot\ffmpeg\bin\ffmpeg.exe -nostats -loglevel 0 -y -i '$Track' -acodec pcm_s16le -f s16le -ac 1 -ar $SampleRate '$RAWTemp'"
Invoke-expression $ConvertToRawExpression
$RawBytes = [System.IO.File]::ReadAllBytes($RAWTemp) | select -First 780000
$EncodedTracKSnippet = [Convert]::ToBase64String($RawBytes)
Remove-Item $RAWTemp
$url = 'https://shazam.p.rapidapi.com/songs/v2/detect'
$headers = @{
'X-RapidAPI-Host'='shazam.p.rapidapi.com';
'X-RapidAPI-Key'='<<<your key>>>'
}
$r = Invoke-WebRequest -Uri $url -UseBasicParsing -Method Post -Body $EncodedTrackSnippet -Headers $headers -ContentType 'text/plain'
$TrackBase = ($r.Content | ConvertFrom-Json).track
$ShazamTrackMetaData = New-Object -TypeName psobject
if (($TrackBase.title) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Title -Value $TrackBase.title; }
if (($TrackBase.subtitle) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Artist -Value $TrackBase.subtitle; }
if ((((($TrackBase).sections).metadata | Where-Object title -eq "Label").text) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Label -Value ((($TrackBase).sections).metadata | Where-Object title -eq "Label").text; }
if ((((($TrackBase).sections).metadata | Where-Object title -eq "Album").text) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Album -Value ((($TrackBase).sections).metadata | Where-Object title -eq "Album").text; }
if ((((($TrackBase).sections).metadata | Where-Object title -eq "Released").text) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Year -Value ((($TrackBase).sections).metadata | Where-Object title -eq "Released").text; }
if ((($TrackBase.genres).primary) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Genre -Value ($TrackBase.genres).primary; }
if (((($TrackBase).sections).text) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Lyrics -Value ((($TrackBase).sections).text); }
if ((($TrackBase.images).coverart) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name CoverArt -Value ($TrackBase.images).coverart; }
$ShazamTrackMetaData | Format-list
输出:
Title : Runnin' Thru The 7th With My Woadies
Artist : $UicideBoy$ & Pouya
Label : G59 Records
Album : $Outh $Ide $Uicide - EP
Year : 2015
Genre : Hip-Hop/Rap
Lyrics : {$null, View from a balcony, Pool lit up blue, Lawn chairs next to the ice chest...}
CoverArt : https://is3-ssl.mzstatic.com/image/thumb/Music115/v4/aa/87/11/aa871103-dba3-4797-4ccd-38e0a811bfe6/195497822997.jpg/400x400cc.jpg
他们的 API Documentation 中确实有 powershell 示例,单击左侧的歌曲 > Detectv2,然后从右侧的下拉列表中单击 select powershell
$Bytes = [System.IO.File]::ReadAllBytes("C:\Users\User\Downloads\clinteastwood_portion_mono.raw") #| select -First 500000
$EncodedText = [Convert]::ToBase64String($Bytes)
$url = 'https://shazam.p.rapidapi.com/songs/v2/detect'
$headers = @{
'X-RapidAPI-Host'='shazam.p.rapidapi.com';
'X-RapidAPI-Key'='<<<your key>>'
}
$r = Invoke-WebRequest -Uri $url -UseBasicParsing -Method Post -Body $EncodedText -Headers $headers -ContentType 'text/plain'
我正在尝试使用 PowerShell 与 Shazam API 交互,但他们的文档主要在 PHP 中。我在 Python 的另一个论坛上找到了这个片段,据说可以工作:
from pydub import AudioSegment
import base64
import requests
import json
file_path="./test.raw"
url = "https://rapidapi.p.rapidapi.com/songs/detect"
encode_string = base64.b64encode(open(file_path, "rb").read())
payload=encode_string
print(type(payload))
headers = {
'content-type': "text/plain",
'x-rapidapi-key': "<<<your key>>>",
'x-rapidapi-host': "shazam.p.rapidapi.com"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(json.dumps(json.loads(response.text)))
我发现的文档中唯一与 PowerShell 相关的是这个小片段,但它似乎不是很有用:
$headers=@{}
$headers.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
$headers.Add("X-RapidAPI-Host", "shazam-core.p.rapidapi.com")
$headers.Add("X-RapidAPI-Key", "<<<your key>>>")
$response = Invoke-WebRequest -Uri 'https://shazam-core.p.rapidapi.com/v1/tracks/recognize' -Method POST -Headers $headers -ContentType 'multipart/form-data; boundary=---011000010111000001101001' -Body '-----011000010111000001101001
Content-Disposition: form-data; name="file"
-----011000010111000001101001--
'
我想按照其他人在 Python 中所做的方式进行操作,但我只需要帮助将其转换为 PowerShell。我已经找到了一种将音频文件转换为 base 64 并将其缩短为 500kb 的方法,类似于他们的做法:
$Bytes = [System.IO.File]::ReadAllBytes("file.mp3") | select -First 500000
$EncodedText = [Convert]::ToBase64String($Bytes)
我只需要知道如何使用 Invoke-WebRequest 格式化请求。
对于任何感兴趣的人,我计划制作一个纯粹使用 Shazam 的歌曲标记器,它可能最终会在某个时候获得一个 GUI,但我还没有决定那么远,一旦我能摆脱这个障碍其他一切都应该很顺利。
编辑:
获得帮助后,这就是我最终从音频文件到 shazam api 输出的方式:
$Track = "Thru the 7th with My Woadies.mp3"
$RAWTemp = $PSScriptRoot + "\Temp.pcm"
$SampleRateExpression = "(($PSScriptRoot\ffmpeg\bin\ffprobe.exe -loglevel 0 -print_format json -show_format -show_streams '$Track' | ConvertFrom-Json).streams).sample_rate"
$SampleRate = Invoke-expression $SampleRateExpression
$ConvertToRawExpression = "$PSScriptRoot\ffmpeg\bin\ffmpeg.exe -nostats -loglevel 0 -y -i '$Track' -acodec pcm_s16le -f s16le -ac 1 -ar $SampleRate '$RAWTemp'"
Invoke-expression $ConvertToRawExpression
$RawBytes = [System.IO.File]::ReadAllBytes($RAWTemp) | select -First 780000
$EncodedTracKSnippet = [Convert]::ToBase64String($RawBytes)
Remove-Item $RAWTemp
$url = 'https://shazam.p.rapidapi.com/songs/v2/detect'
$headers = @{
'X-RapidAPI-Host'='shazam.p.rapidapi.com';
'X-RapidAPI-Key'='<<<your key>>>'
}
$r = Invoke-WebRequest -Uri $url -UseBasicParsing -Method Post -Body $EncodedTrackSnippet -Headers $headers -ContentType 'text/plain'
$TrackBase = ($r.Content | ConvertFrom-Json).track
$ShazamTrackMetaData = New-Object -TypeName psobject
if (($TrackBase.title) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Title -Value $TrackBase.title; }
if (($TrackBase.subtitle) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Artist -Value $TrackBase.subtitle; }
if ((((($TrackBase).sections).metadata | Where-Object title -eq "Label").text) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Label -Value ((($TrackBase).sections).metadata | Where-Object title -eq "Label").text; }
if ((((($TrackBase).sections).metadata | Where-Object title -eq "Album").text) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Album -Value ((($TrackBase).sections).metadata | Where-Object title -eq "Album").text; }
if ((((($TrackBase).sections).metadata | Where-Object title -eq "Released").text) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Year -Value ((($TrackBase).sections).metadata | Where-Object title -eq "Released").text; }
if ((($TrackBase.genres).primary) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Genre -Value ($TrackBase.genres).primary; }
if (((($TrackBase).sections).text) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name Lyrics -Value ((($TrackBase).sections).text); }
if ((($TrackBase.images).coverart) -ne $null) { $ShazamTrackMetaData | Add-Member -MemberType NoteProperty -Name CoverArt -Value ($TrackBase.images).coverart; }
$ShazamTrackMetaData | Format-list
输出:
Title : Runnin' Thru The 7th With My Woadies
Artist : $UicideBoy$ & Pouya
Label : G59 Records
Album : $Outh $Ide $Uicide - EP
Year : 2015
Genre : Hip-Hop/Rap
Lyrics : {$null, View from a balcony, Pool lit up blue, Lawn chairs next to the ice chest...}
CoverArt : https://is3-ssl.mzstatic.com/image/thumb/Music115/v4/aa/87/11/aa871103-dba3-4797-4ccd-38e0a811bfe6/195497822997.jpg/400x400cc.jpg
他们的 API Documentation 中确实有 powershell 示例,单击左侧的歌曲 > Detectv2,然后从右侧的下拉列表中单击 select powershell
$Bytes = [System.IO.File]::ReadAllBytes("C:\Users\User\Downloads\clinteastwood_portion_mono.raw") #| select -First 500000
$EncodedText = [Convert]::ToBase64String($Bytes)
$url = 'https://shazam.p.rapidapi.com/songs/v2/detect'
$headers = @{
'X-RapidAPI-Host'='shazam.p.rapidapi.com';
'X-RapidAPI-Key'='<<<your key>>'
}
$r = Invoke-WebRequest -Uri $url -UseBasicParsing -Method Post -Body $EncodedText -Headers $headers -ContentType 'text/plain'