0

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.

ZR Han
  • 238
  • 1
  • 3
  • 10
  • It is a Font issue when display character in PS. Need to change the Font in PS.Right click on the PowerShell icon at the top left of the PowerShell screen. Click Properties. A window appears, showing options for font size, family, colors and more. – jdweng Apr 21 '23 at 09:35
  • @jdweng Thank you for response. That doesn't work. As I said it works fine when I just type `Write-Output 'ДЦ本화が'` in the console, which means that the font can display the characters properly. – ZR Han Apr 21 '23 at 09:41
  • Maybe it is a Big/Little Endian issue? Maybe following will help : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_character_encoding?view=powershell-7.3 It may be Standard Output is using a different console from PS. – jdweng Apr 21 '23 at 09:58
  • @jdweng I set `$PSDefaultParameterValues['*:Encoding'] = 'utf8'` and `[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding` and both not working – ZR Han Apr 21 '23 at 10:20
  • 1
    Does this answer your question?: [Using UTF-8 Encoding (CHCP 65001) in Command Prompt / Windows Powershell (Windows 10)](https://stackoverflow.com/a/57134096/1701026) – iRon Apr 21 '23 at 12:03

0 Answers0