Created
October 1, 2022 08:05
-
-
Save yujinlin0224/d1da1566ec23492642965415022a039d to your computer and use it in GitHub Desktop.
waifu2x-caffe-video.ps1 (2017/06/04)
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
# Define some arguments and these default values form "waIfu2x-caffe-cui.exe" that may be used. | |
param ( | |
[String] $Mode = "noise_scale", | |
[Decimal] $ScaleRatio = 2.0, | |
[Int] $ScaleWidth = 0, | |
[Int] $ScaleHeight = 0, | |
[Int] $NoiseLevel = 0, | |
[String] $Process = "cpu", | |
[Int] $CropSize = 128, | |
[Int] $GPU = 0, | |
[Int] $TTA = 0, | |
[String] $ModelDir = "models/upconv_7_anime_style_art_rgb" | |
) | |
# -------------------- Configurations -------------------- | |
# Define path of applications that will be used. | |
$FFmpegPath = "C:\Users\User\Documents\Software\Video Tools\ffmpeg\bin\ffmpeg.exe" | |
$FFprobePath = "C:\Users\User\Documents\Software\Video Tools\ffmpeg\bin\ffprobe.exe" | |
$WaIfu2xPath = "C:\Users\User\Documents\Software\waIfu2x-caffe\waIfu2x-caffe-cui.exe" | |
# Define name of folders and file types that use to input files. | |
$InputFileTypes = ("*.mp4", "*.avi") | |
$InputFolder = ".\input\" | |
# Define name of folders and files that use to output files and save temp file. | |
# Can use "<FileName>" , "<Time>", and "<Arguments>" tag. | |
$OutputFolder = ".\output\<FileName>.<Arguments>\" | |
$TempFolder = ($env:temp -Replace "\\$") + "\waIfu2x-caffe_ffmpeg\" | |
$TempSourceFolder = $TempFolder + "source\<FileName>.<Arguments>\" | |
$TempConvertFolder = $TempFolder + "convert\<FileName>.<Arguments>\" | |
$TempSourceAudioFile = $TempSourceFolder + "audio.m4a" | |
$TempSourceImageFiles = $TempSourceFolder + "video_flame%08d.png" | |
$TempConvertImageFiles = $TempConvertFolder + "video_flame%08d.png" | |
# -------------------- Configurations -------------------- | |
# Define "RunProgram" function to run program. | |
Function RunProgram($ExecPath, $ExecArgs) { | |
$ProcessStartInfo = New-Object System.Diagnostics.ProcessStartInfo | |
$ProcessStartInfo.RedirectStandardOutput = $True | |
$ProcessStartInfo.UseShellExecute = $False | |
$ProcessStartInfo.FileName = $ExecPath | |
$ProcessStartInfo.Arguments = $ExecArgs | |
$Program = New-Object System.Diagnostics.Process | |
$Program.StartInfo = $ProcessStartInfo | |
$Program.Start() | Out-Null | |
Write-Output $Program | |
} | |
# Define "RunProgram" function to forrmat string. | |
Function ForrmatString($String, $FileName, $Time, $Arguments) { | |
Return (($String -Replace "<FileName>", $FileName) -Replace | |
"<Time>", $Time) -Replace "<Arguments>", $Arguments | |
} | |
# Get all files at input folder to process them. | |
Get-ChildItem -Path $InputFolder -Include $InputFileTypes -Recurse | ForEach-Object { | |
Write-Host "Start processing file {$_.FullName}" -ForegroundColor Cyan | |
# Use "FFprobe" to get infomation of the film. | |
Write-Host "Getting video infomation of the file . . . " -ForegroundColor Yellow | |
$ExecArgs = """{$_.FullName}"" -show_entries ""format"" -show_streams -select_streams ""v"" -print_format ""json""" | |
Write-Host "$ ""$FFprobePath"" $ExecArgs" -ForegroundColor Green | |
$Program = RunProgram $FFprobePath $ExecArgs | |
$Program.WaitForExit() | |
$FFprobeOutputJson = $Program.StandardOutput.ReadToEnd() | ConvertFrom-Json | |
$FPSStrings = $FFprobeOutputJson.streams[0].avg_frame_rate -Split "/" | |
$FPS = $FPSStrings[0].ToDecimal($Null) / $FPSStrings[1].ToDecimal($Null) | |
$InputWidth = $FFprobeOutputJson.streams[0].width.ToDecimal($Null) | |
$InputHeight = $FFprobeOutputJson.streams[0].height.ToDecimal($Null) | |
$InputBitRate = $FFprobeOutputJson.streams[0].bit_rate.ToDecimal($Null) | |
If ($FFprobeOutputJson.streams[0].max_bit_rate -Eq $Null) { | |
$InputMaxBitRate = $InputBitRate * 2 | |
} Else { | |
$InputMaxBitRate = $FFprobeOutputJson.streams[0].bit_rate.ToDecimal($Null) | |
} | |
# Set and check arguments of ""waIfu2x-caffe-cui.exe"", | |
# set width, hetght, and bit rate of the film when outputting, and | |
# create temp and output folder. | |
Write-Host "Checking arguments of ""waIfu2x-caffe-cui.exe"" . . . " -ForegroundColor Yellow | |
$WaIfu2xArgs = "-m $Mode " | |
$ArgsString = "m($Mode)" | |
If ((($Mode -Split "_")[0] -Eq "scale") -Or (($Mode -Split "_")[1] -Eq "scale")) { | |
If (($ScaleRatio -Gt 0) -And ($ScaleWidth -Le 0) -And ($ScaleHeight -Le 0)) { | |
$WaIfu2xArgs += "-s $ScaleRatio " | |
$ArgsString += "s($ScaleRatio)" | |
$OutputWidth = [Math]::Round($InputWidth * $ScaleRatio) | |
$OutputHeight = [Math]::Round($InputHeight * $ScaleRatio) | |
$OutputBitRate = [Math]::Round($InputBitRate * [Math]::Pow($ScaleRatio, 2)) | |
} ElseIf (($ScaleWidth -Gt 0) -And ($ScaleHeight -Le 0)) { | |
$WaIfu2xArgs += "-w $ScaleWidth " | |
$ArgsString += "w($ScaleWidth)" | |
$OutputWidth = $ScaleWidth | |
$OutputHeight = [Math]::Round($InputHeight * ($InputWidth / $ScaleWidth)) | |
$OutputBitRate = [Math]::Round($InputBitRate * [Math]::Pow($InputWidth / $ScaleWidth, 2)) | |
} ElseIf (($ScaleWidth -Le 0) -And ($ScaleHeight -Gt 0)) { | |
$WaIfu2xArgs += "-h $ScaleHeight " | |
$ArgsString += "h($ScaleHeight)" | |
$OutputWidth = [Math]::Round($OutputWidth * ($InputHeight / $ScaleHeight)) | |
$OutputHeight = $ScaleHeight | |
$OutputBitRate = [Math]::Round($InputBitRate * [Math]::Pow($InputHeight / $ScaleHeight, 2)) | |
} ElseIf (($ScaleWidth -Gt 0) -And ($ScaleHeight -Gt 0)) { | |
$WaIfu2xArgs += "-w $ScaleWidth -h $ScaleHeight " | |
$ArgsString += "w($ScaleWidth)h($ScaleHeight)" | |
$OutputWidth = $ScaleWidth | |
$OutputHeight = $ScaleHeight | |
$OutputBitRate = [Math]::Round($InputBitRate * [Math]::Pow($InputHeight / $ScaleHeight, 2)) | |
} Else { | |
Write-Host """ScaleRatio"", ""ScaleWidth"", " -NoNewline -ForegroundColor Red | |
Write-Host "and / or ""ScaleHeight"" have wrong arguments." -ForegroundColor Red | |
Exit | |
} | |
} | |
$OutputMaxBitRate = $OutputBitRate * ($InputMaxBitRate / $InputBitRate) | |
If (($Mode -Split "_")[0] -Eq "noise") { | |
$WaIfu2xArgs += "-n $NoiseLevel " | |
$ArgsString += "n($NoiseLevel)" | |
} | |
$WaIfu2xArgs += "-p $Process -c $CropSize --gpu $GPU -t $TTA --model_dir $ModelDir " | |
$WaIfu2xArgs = $WaIfu2xArgs -Replace " $" | |
$ArgsString += ("p($Process)c($CropSize)gpu($GPU)t($TTA)model($ModelDir)" -Replace "models\/") -Replace "models\\" | |
$Time = Get-Date -format yyyy-MM-dd-HH-mm-ss-fffffff | |
$OutputFolderName = ForrmatString $OutputFolder $_.Name $Time $ArgsString | |
$TempFolderName = ForrmatString $TempFolder $_.Name $Time $ArgsString | |
$TempSourceFolderName = ForrmatString $TempSourceFolder $_.Name $Time $ArgsString | |
$TempConvertFolderName = ForrmatString $TempConvertFolder $_.Name $Time $ArgsString | |
$TempSourceAudioFileName = ForrmatString $TempSourceAudioFile $_.Name $Time $ArgsString | |
$TempSourceImageFilesName = ForrmatString $TempSourceImageFiles $_.Name $Time $ArgsString | |
$TempConvertImageFilesName = ForrmatString $TempConvertImageFiles $_.Name $Time $ArgsString | |
New-Item $OutputFolderName -Type Directory -Force | Out-Null | |
New-Item $TempSourceFolderName -Type Directory -Force | Out-Null | |
New-Item $TempConvertFolderName -Type Directory -Force | Out-Null | |
# Use "FFmpeg" to spilt video to pictures and audio of the film. | |
Write-Host "Spilting video to pictures and audio form the file . . . " -ForegroundColor Yellow | |
$ExecArgs = "-y -i ""{$_.FullName}"" -vn -acodec ""copy"" ""$TempSourceAudioFileName""" | |
Write-Host "$ ""$FFmpegPath"" $ExecArgs" -ForegroundColor Green | |
$Program = RunProgram $FFmpegPath $ExecArgs | |
$Program.WaitForExit() | |
$ExecArgs = "-y -i ""{$_.FullName}"" -f ""image2"" -vcodec ""png"" ""$TempSourceImageFilesName""" | |
Write-Host "$ ""$FFmpegPath"" $ExecArgs" -ForegroundColor Green | |
$Program = RunProgram $FFmpegPath $ExecArgs | |
$Program.WaitForExit() | |
# Use "waIfu2x-caffe" to denoise and / or zoom pictures that output form video of the film. | |
Write-Host "Denoising and / or zooming pictures . . . " -ForegroundColor Yellow | |
$ExecArgs = "-i ""{0}"" -o ""{1}"" $WaIfu2xArgs" -F | |
($TempSourceFolderName -Replace "\\$"), ($TempConvertFolderName -Replace "\\$") | |
Write-Host "$ ""$WaIfu2xPath"" $ExecArgs" -ForegroundColor Green | |
$Program = RunProgram $WaIfu2xPath $ExecArgs | |
$Program.WaitForExit() | |
# Use "FFmpeg" to merge pictures and audio to the new film. | |
Write-Host "Merge pictures and audio to the new file . . . " -ForegroundColor Yellow | |
$VideoExt = [System.IO.Path]::GetExtension($_.Name) -Replace "^\." | |
$ExecArgs = "-y -i ""$TempSourceAudioFileName"" -r:v $FPS -i ""$TempConvertImageFilesName"" " + | |
"-c:v ""libx264"" -b:v $OutputBitRate -maxrate:v $OutputMaxBitRate -bufsize:v $OutputMaxBitRate " + | |
"-s:v ""${OutputWidth}x${OutputHeight}"" -aspect:v ""${InputWidth}:${InputHeight}"" " + | |
"-pix_fmt:v ""yuv444p"" -f ""$VideoExt"" ""{0}""" -F ($OutputFolderName + $_.Name) | |
Write-Host "$ ""$FFmpegPath"" $ExecArgs" -ForegroundColor Green | |
$Program = RunProgram $FFmpegPath $ExecArgs | |
$Program.WaitForExit() | |
# Clear temp files. | |
Remove-Item $TempSourceFolderName -Force -Recurse | Out-Null | |
Remove-Item $TempConvertFolderName -Force -Recurse | Out-Null | |
Write-Host "Finished to processing {$_.FullName}" -ForegroundColor Cyan | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment