I am trying to use Out-String
to save the output of ffprobe
as a string in PowerShell.
(ffprobe
can output the metadata of a media file.)
The output contains non-Latin characters, but when I use Out-String
, the characters are not displayed correctly and are replaced with question marks.
For example, the output of the ffprobe $filename
command when run alone is:
Metadata:
encoder : Lavc59.20.100 libopus
ALBUM : 钗头凤
ARTIST : 等什么君
comment : 酷我音乐
TITLE : 钗头凤
But when I use ffprobe $filename 2>&1 | Out-String
, the output is displayed as:
Metadata:
encoder : Lavc59.20.100 libopus
ALBUM : 閽楀ご鍑?
ARTIST : 绛変粈涔堝悰
comment : 閰锋垜闊充箰
TITLE : 閽楀ご鍑?
I suspect that the issue is with the encoding used by PowerShell when converting the ffprobe output to a string. No matter with or without Out-String
, the issue has appeared.
Similar problem can be found when running scripts that output non-Latin characters. For example, when I run
Write-Output 'Non-latin characters like Cyrillic (Д, Ц); CJK characters (本, 화, が).'
the output is fine. However, when I save the script to a file (test.ps1) and run it using the .\test.ps1
command, the output is displayed with encoding errors.
Non-latin characters like Cyrillic (袛, 笑); CJK characters (鏈?, 頇?, 銇?).
My $OutputEncoding
variable is set to [System.Text.Encoding]::UTF8
BodyName : utf-8
EncodingName : Unicode (UTF-8)
HeaderName : utf-8
WebName : utf-8
WindowsCodePage : 1200
IsBrowserDisplay : True
IsBrowserSave : True
IsMailNewsDisplay : True
IsMailNewsSave : True
IsSingleByte : False
EncoderFallback : System.Text.EncoderReplacementFallback
DecoderFallback : System.Text.DecoderReplacementFallback
IsReadOnly : True
CodePage : 65001
Can anyone suggest a solution to fix this encoding issue?
Any help would be appreciated. Thank you.