- Windowsには、問題ステップ記録ツール(psr.exe)という、簡易的にスクリーンキャプチャしてくれる便利ツールが標準搭載されている(いつから標準搭載されているのかは知らない)。
- しかし、出力がZIP化されたMHTMLファイルで、キャプチャ画像はBASE64で符号化されていて、ZIPを解凍しても画像が取り出せない。
- とりあえず解凍してできたMHTMLファイルから画像(JPEG)を抽出したい。
- ので、雑に作った。
使い方
PS C:\demo\psr> tree /a /f
フォルダー パスの一覧: ボリューム TI31063700D
ボリューム シリアル番号は 7884-7485 です
C:.
| mhtml2jpeg.ps1
|
\---psr
Recording_20160924_2126.mht
PS C:\demo\psr> .\mhtml2jpeg.ps1 .\psr\Recording_20160924_2126.mht .\psr
PS C:\demo\psr>
PS C:\demo\psr> ls .\psr -name
Recording_20160924_2126.mht
screenshot0001.JPEG
screenshot0002.JPEG
screenshot0003.JPEG
コード
.ps1
Param ([string]$Path, [string]$Destination)
$Path = Resolve-Path $Path
$Destination = Resolve-Path $Destination
$nameRE = [regex]"^Content-Location: (?<Name>screenshot\d{4}.JPEG)$"
$nextRE = [regex]"^--=_NextPart"
$imagePart = $false
$base64 = ""
switch -regex -file $Path
{
'^$' {}
$nameRE
{
$name = $nameRE.Match($_).Groups['Name'].Value
$file = Join-Path $Destination $name
$imagePart = $true
}
$nextRE
{
if ($imagePart)
{
$byte = [Convert]::FromBase64String($base64)
[IO.File]::WriteAllBytes($file, $byte)
$imagePart = $false
$base64 = ""
}
}
default
{
if ($imagePart)
{
$base64 += $_
}
}
}