Last active
September 14, 2022 14:14
-
-
Save esperecyan/f0d2e4d7b3addcd02f5d73cb6435fa5c to your computer and use it in GitHub Desktop.
『convert-video-to-texture-for-mtoon.ps1.jse』 動画をMToon上で再生できるきゃぱきゃぱ式テクスチャに変換するスクリプトです。 【16:9で38分割したサンプルFBX】https://cdn.discordapp.com/attachments/511077066628661271/997715603345391638/screen.fbx / https://twitter.com/mirabetakasi64/status/1148233404019642368
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#@~^AQAAAA==~IAAAAA==^#~@ function toPSString(str) { return "'" + str.replace(/%/g, '"%"').replace(/'/g, "''") + "'"; } /* -*- mode: powershell;-*- | |
<#*/ var command = 'param($Path, $FrameCount, $VerticalResolution, [int]$TextureSize = 8192)' | |
+ '; $_PSCommandPath = ' + toPSString(WSH.ScriptFullName) | |
+ '; Invoke-Expression (Get-Content ' + toPSString(WSH.ScriptFullName) + ' -Encoding UTF8 -Raw)'; | |
var namePattern = /^-(?!(?:b(?:and|or|xor|not)|sh[lr]|[ic]?(?:eq|ne|gt|ge|lt|le|(?:not)?(?:like|match|contains|in)|replace|split)|join|is(?:not)?|as|and|or|not|f)$)[0-9a-z]+$/i; | |
var args = ''; for (var i = 0; i < WSH.Arguments.Length; i++) { | |
var arg = WSH.Arguments(i); args += ' ' + (namePattern.test(arg) ? arg : toPSString(arg)); } | |
WSH.CreateObject('WScript.Shell').Run('PowerShell -NoExit -Command &{' + command + '}' + args); /*#> | |
<# | |
.SYNOPSIS | |
動画をMToon上で再生できるきゃぱきゃぱ式テクスチャに変換するスクリプトです。 | |
.DESCRIPTION | |
あらかじめパスの通った場所にffmpegをインストールしておく必要があります。 | |
参照: | |
ティーと@きゃぱの者@PPLさんのツイート: ““全部UVスクロールのみでやりました”(大変だった{全ギレ})。例によって特性上、VRMアバターに入れてバーチャルキャストで使えるやつ。1列ミスってるし😡😡😡…” | |
<https://twitter.com/mirabetakasi64/status/1148233404019642368> | |
Licence: MPL-2.0 <https://www.mozilla.org/MPL/2.0/> | |
Version: 0.2.0 | |
Author: 100の人 | |
配布元: <https://gist.github.com/esperecyan> | |
.EXAMPLE | |
convert-video-to-texture-for-mtoon.ps1.jse -Path test.mp4 -FrameCount 100 -VerticalResolution 38 | |
.PARAMETER Path | |
動画ファイルのパス。 | |
.PARAMETER FrameCount | |
出力時のテクスチャを縦に何分割するか。フレーム数。 | |
.PARAMETER VerticalResolution | |
映像を縦に何分割するか。縦のポリゴン数。解像度。 | |
.PARAMETER TextureSize | |
出力時のテクスチャの1辺のピクセル数。省略時は8192。 | |
#> | |
using namespace System.IO | |
using namespace System.Drawing | |
using namespace System.Windows.Forms | |
Set-StrictMode -Version Latest; $ErrorActionPreference = 'Stop'; | |
$PSCommandPath = $_PSCommandPath; $PSScriptRoot = Split-Path $PSCommandPath -Parent | |
Add-Type -AssemblyName @('System.Drawing', 'System.Windows.Forms') | |
$Path = Resolve-Path $Path | |
$ErrorActionPreference = 'SilentlyContinue' | |
$videoInfo = (ffprobe -show_streams -print_format json $Path 2>$null | ConvertFrom-Json).streams[0] | |
$ErrorActionPreference = 'Stop' | |
$temporaryFolderPath = Join-Path ([Path]::GetTempPath()) ([Path]::GetRandomFileName()) | |
New-Item $temporaryFolderPath -ItemType Directory | |
ffmpeg -i $Path -r $($FrameCount / $videoInfo.duration) -f image2 (Join-Path $temporaryFolderPath '%04d.png') | |
$destinationRect = New-Object Rectangle(0, 0, ($TextureSize / $VerticalResolution), ($TextureSize / $FrameCount)) | |
$sourcePixelHeight = $videoInfo.height / $VerticalResolution | |
$sourceRect = New-Object Rectangle(0, 0, $videoInfo.width, 1) | |
$destinationBitmap = New-Object Bitmap($TextureSize, $TextureSize) | |
$graphics = [Graphics]::FromImage($destinationBitmap) | |
foreach ($file in (Get-ChildItem $temporaryFolderPath)) { | |
$image = [Image]::FromFile($file.FullName) | |
$destinationRect.Y = (-1 + $file.Basename) * $destinationRect.Height | |
for ($i = 0; $i -lt $VerticalResolution; $i++) { | |
$destinationRect.X = $destinationRect.Width * $i | |
$sourceRect.Y = $sourcePixelHeight * $i | |
$graphics.DrawImage($image, $destinationRect, $sourceRect, [GraphicsUnit]::Pixel) | |
} | |
$image.Dispose() | |
} | |
Remove-Item $temporaryFolderPath -Recurse | |
$graphics.Dispose() | |
$destinationPath = Join-Path (Split-Path $Path -Parent) ([Path]::GetFileNameWithoutExtension($Path) + '-texture-for-mtoon.png') | |
$destinationBitmap.Save($destinationPath) | |
$destinationBitmap.Dispose() | |
[MessageBox]::Show("$destinationPath へ保存しました。", (Split-Path -Path @($PSCommandPath) -Leaf)) | |
(Get-Process -Id $pid).CloseMainWindow() | Out-Null # */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment