Link to home
Start Free TrialLog in
Avatar of garyknight
garyknightFlag for United States of America

asked on

Using Powershell to Base64 Encode an MP3 Audio File

I'm trying to use Powershell to Base64 encode an mp3 audio file (for uploading via API call), but for some reason the resulting Base64 encoded text isn't right. Here is my code:

$recording="c:\myfile.mp3"
$fileContent = Get-Content $recording -Encoding UTF8
$fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
$fileContentEncoded | set-content ("c:\output.b64")

Open in new window


I compare my resulting text from this script with the resulting text from one of the free online base64 encoders (which I know is correct), and I get a different string of text.

Can anyone help me?
Avatar of oBdA
oBdA

"You're reading it wrong." This is a binary file, you need to use Byte as encoding.
Encode and save:
$inFile = "c:\myfile.mp3"
$b64File = "c:\output.b64"
[System.Convert]::ToBase64String((Get-Content -Path $inFile -Encoding Byte)) | Set-Content -Path $b64File -Encoding ASCII

Open in new window

Decode and compare:
$b64File = "c:\output.b64"
$recoveredFile = "c:\myfile.b64.mp3"
[System.Convert]::FromBase64String((Get-Content -Path $b64File -Encoding ASCII -Raw)) | Set-Content -Path $recoveredFile -Encoding Byte -Force
& fc.exe $inFile $recoveredFile

Open in new window

Avatar of garyknight

ASKER

Thanks for your quick reply. I tried to encode using your method and got the following error. I'm using Powershell 6.0.4...not sure if that makes a difference.

Get-Content : Cannot bind parameter 'Encoding'. Cannot convert the "Byte" value of type "System.String" to type "System.Text.Encoding".
At C:\encoder.ps1:7 char:71
+ ... rt]::ToBase64String((Get-Content -Path $inFile -Encoding Byte)) | Set ...
+                                                              ~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetContentCommand
It seems like PowerShell 2.0 is about as tenacious as Windows XP ...
Seriously, PowerShell 2.0 is from 2009. PS 5.1 is even supported on Windows 7 / Server 2008 R2, and PS3.0 is already a lot better than PS 2.0. Consider upgrading.
Windows Management Framework 5.1
https://www.microsoft.com/en-us/download/details.aspx?id=54616
Encode on a legacy system:
$inFile = "c:\myfile.mp3"
$b64File = "c:\output.b64"
[IO.File]::WriteAllText($b64File, ([System.Convert]::ToBase64String(([IO.File]::ReadAllBytes($inFile)))))

Open in new window

Decode on a legacy system:
$b64File = "c:\output.b64"
$recoveredFile = "c:\myfile.b64.mp3"
[IO.File]::WriteAllBytes($recoveredFile, ([System.Convert]::FromBase64String(([IO.File]::ReadAllText($b64File)))))
& fc.exe $inFile $recoveredFile

Open in new window

@oBdA, that might be an issue with PS Core (!), not PS 2.0, as the OP stated they use 6.0.4
Here is what I get from $PSVersionTable:

PS C:\Program Files\PowerShell\6.0.4> $Psversiontable

Name                           Value
----                           -----
PSVersion                      6.0.4
PSEdition                      Core
GitCommitId                    v6.0.4
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks to you both!!
@Qlemo - glad you found that link, as I thought I had read something similar, but couldn't find it.  I did find this one (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/providers/filesystem-provider/get-content-for-filesystem?view=powershell-6) which has incorrect info, and the built-in help also has bad info.
@garyknight, please also select oBdA's first comment as solution, not only as Helpful. He found the basic issue, I have been just correcting the new option as he was not aware of a change there.
@footech, that's very bad, because the obvious location to look for is yours. No one will look into MIcrosoft Powershell Management for Get-Content  :<, and I have just been lucky to get the correct link by searching with the correct keywords.