Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #----------------------------------------------------------------------------------------------------------------------------
- # Bighead's Nier Automata Movie Criware Upscaling/Interpolation Script Deluxe Super Pro v0.1
- # For accompanying images, see this link: https://imgur.com/a/neir-automata-movie-upscale-script-folder-hierarchy-gf3zdlQ
- #----------------------------------------------------------------------------------------------------------------------------
- # I'm hoping that those who are looking to set out on the journey that I have, that this script will help guide them in the
- # right direction. Information is scarce when it comes to USM videos, and it's even more scarce when it comes to creating
- # H.264 USM videos. Commands fed into the programs have been broken down for easier viewing and editing. Some commands are
- # just a string that is appended to, others are an array of strings that PowerShell is able to break down when fed to the
- # executables. This is just how I did it, a batch script would have also been sufficient.
- #----------------------------------------------------------------------------------------------------------------------------
- # Base folder.
- $BaseFolder = Split-Path $script:MyInvocation.MyCommand.Path
- $host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(120, 20000)
- #----------------------------------------------------------------------------------------------------------------------------
- # The output dimensions of the final video.
- $Width = "2560"
- $Height = "1440"
- # Frames per second to interpolate to.
- $FPS = "60"
- # The bitrate of the videos.
- $BitRate = "35000"
- $MaxRate = "40000"
- $BufSize = "40000"
- #----------------------------------------------------------------------------------------------------------------------------
- # USM Toolkit, requires both VGStream and FFMPeg:
- # https://github.com/Rikux3/UsmToolkit
- # https://github.com/vgmstream/vgmstream -- extract to USMToolKit root folder to "vgmstream" folder
- # https://github.com/BtbN/FFmpeg-Builds -- extract "ffmpeg.exe" to USMToolkit root folder
- $USMTool = $BaseFolder + "\00 - Tools\UsmToolkit\UsmToolkit.exe"
- # Topaz Video AI FFMPEG (it's expensive):
- # https://www.topazlabs.com/topaz-video-ai
- $TopazAI = $BaseFolder + "\00 - Tools\Topaz\App\Topaz Video AI\ffmpeg.exe"
- # VideoLAN x264 encoder:
- # https://www.videolan.org/developers/x264.html
- $x264Enc = $BaseFolder + "\00 - Tools\x264\x264.exe"
- # CriWare SDK Tools
- # Search for: "criware sdk archive.org"
- $CriMed = $BaseFolder + "\00 - Tools\Sofdec2\crimed.exe"
- $SofDec2 = $BaseFolder + "\00 - Tools\Sofdec2\sofdec2enc.exe"
- #----------------------------------------------------------------------------------------------------------------------------
- # Paths to videos.
- $USMPath = $BaseFolder + "\01 - Input USM"
- $M2VPath = $BaseFolder + "\02 - M2V Files"
- $MP4Path = $BaseFolder + "\03 - Upscaled MP4"
- $USMDone = $BaseFolder + "\04 - Output USM"
- # Temporary path.
- $TempPath = $USMDone + "\temp"
- #----------------------------------------------------------------------------------------------------------------------------
- # Loop through the USM files and process each file to the end until a new USM is created.
- foreach ($File in Get-ChildItem -LiteralPath $USMPath)
- {
- #----------------------------------------------------------------------------------------------------------------------------
- # SECTION 01: DEMUX TO M2V
- #----------------------------------------------------------------------------------------------------------------------------
- # Verify the correct file type.
- if ($File.Extension -ne ".usm") { continue }
- # Create the working folder if it does exists.
- if (!(Test-Path -LiteralPath $TempPath -ErrorAction "SilentlyContinue")) { New-Item -Path $TempPath -ItemType "Directory" | Out-Null }
- # Demux the input file.
- & $USMTool extract $File.FullName
- # Paths to the generated files.
- $M2VFile = $USMPath + "\" + $File.BaseName + ".m2v"
- # Move the file to the output folder.
- Move-Item -LiteralPath $M2VFile -Destination $M2VPath -Force
- #----------------------------------------------------------------------------------------------------------------------------
- # PATHS TO FILES
- #----------------------------------------------------------------------------------------------------------------------------
- # Update the path to where the file is now.
- $M2VFile = $M2VPath + "\" + $File.BaseName + ".m2v"
- $MP4File = $MP4Path + "\" + $File.BaseName + ".mp4"
- $USMFile = $USMDone + "\" + $File.BaseName + ".usm"
- $Garbage = $BaseFolder + "\x264_lookahead.clbin"
- #----------------------------------------------------------------------------------------------------------------------------
- # SECTION 02: TOPAZ AI (Makes use of its own version of ffmpeg, Topaz AI filtering is done via "-filter_complex" arguments.)
- #----------------------------------------------------------------------------------------------------------------------------
- # Topaz model params are just one long string, assemble the interpolation params.
- $Topaz_TVAIFIParams = 'tvai_fi=model=chr-2:'
- $Topaz_TVAIFIParams += 'slowmo=1:'
- $Topaz_TVAIFIParams += 'rdt=0.01:'
- $Topaz_TVAIFIParams += 'fps=' + $FPS + ':'
- $Topaz_TVAIFIParams += 'device=0:'
- $Topaz_TVAIFIParams += 'vram=1:'
- $Topaz_TVAIFIParams += 'instances=1'
- # Do the same for the upscaling model params.
- $Topaz_TVAIUPParams = 'tvai_up=model=prob-4:'
- $Topaz_TVAIUPParams += 'scale=0:'
- $Topaz_TVAIUPParams += 'w='+ $Width +':h=' + $Height + ':'
- $Topaz_TVAIUPParams += 'preblur=0.1:'
- $Topaz_TVAIUPParams += 'noise=0.2:'
- $Topaz_TVAIUPParams += 'details=1:'
- $Topaz_TVAIUPParams += 'halo=0.04:'
- $Topaz_TVAIUPParams += 'blur=0:'
- $Topaz_TVAIUPParams += 'compression=0.11:'
- $Topaz_TVAIUPParams += 'estimate=8:'
- $Topaz_TVAIUPParams += 'blend=1:'
- $Topaz_TVAIUPParams += 'device=0:'
- $Topaz_TVAIUPParams += 'vram=1:'
- $Topaz_TVAIUPParams += 'instances=1'
- # Do the same for misc filtering stuff.
- $Topaz_ScaleParams = 'scale=w=' + $Width + ':'
- $Topaz_ScaleParams += 'h=' + $Height + ':'
- $Topaz_ScaleParams += 'flags=lanczos:'
- $Topaz_ScaleParams += 'threads=0:'
- $Topaz_ScaleParams += 'force_original_aspect_ratio=decrease'
- # Do the same for the padding params.
- $Topaz_PadParams = 'pad=' + $Width + ':' + $Height + ':-1:-1:color=black'
- # Compile them all into the complex filter command.
- $Topaz_FilterParam = $Topaz_TVAIFIParams + ',' + $Topaz_TVAIUPParams + ',' + $Topaz_ScaleParams + ',' + $Topaz_PadParams
- # Create a list to store the full Topaz parameters.
- $TopazParams = New-Object System.Collections.Generic.List[string]
- # Add the parameters to the lists.
- $TopazParams.Add('-hide_banner')
- $TopazParams.Add('-i')
- $TopazParams.Add($M2VFile)
- $TopazParams.Add('-sws_flags')
- $TopazParams.Add('spline+accurate_rnd+full_chroma_int')
- $TopazParams.Add('-filter_complex')
- $TopazParams.Add($Topaz_FilterParam)
- $TopazParams.Add('-c:v')
- $TopazParams.Add('h264_nvenc')
- $TopazParams.Add('-profile:v')
- $TopazParams.Add('high')
- $TopazParams.Add('-pix_fmt')
- $TopazParams.Add('yuv420p')
- $TopazParams.Add('-g')
- $TopazParams.Add('30')
- $TopazParams.Add('-preset')
- $TopazParams.Add('p7')
- $TopazParams.Add('-tune')
- $TopazParams.Add('hq')
- $TopazParams.Add('-rc')
- $TopazParams.Add('constqp')
- $TopazParams.Add('-qp')
- $TopazParams.Add('18')
- $TopazParams.Add('-rc-lookahead')
- $TopazParams.Add('20')
- $TopazParams.Add('-spatial_aq')
- $TopazParams.Add('1')
- $TopazParams.Add('-aq-strength')
- $TopazParams.Add('15')
- $TopazParams.Add('-b:v')
- $TopazParams.Add('0')
- $TopazParams.Add('-an')
- $TopazParams.Add('-map_metadata')
- $TopazParams.Add('0')
- $TopazParams.Add('-map_metadata:s:v')
- $TopazParams.Add('0:s:v')
- $TopazParams.Add('-fps_mode:v')
- $TopazParams.Add('passthrough')
- $TopazParams.Add('-movflags')
- $TopazParams.Add('frag_keyframe+empty_moov+delay_moov+write_colr')
- $TopazParams.Add('-bf')
- $TopazParams.Add('0')
- $TopazParams.Add($MP4File)
- # Create the upscaled interpolated result.
- & $TopazAI $TopazParams
- #----------------------------------------------------------------------------------------------------------------------------
- # SECTION 03: H.264 ENCODER
- #----------------------------------------------------------------------------------------------------------------------------
- # I found the easiest way to handle arguments is just add them to lists and let PowerShell sort out how they are added.
- $x264Params = New-Object System.Collections.Generic.List[string]
- $CriMedParams = New-Object System.Collections.Generic.List[string]
- $SofDec2Params = New-Object System.Collections.Generic.List[string]
- # Paths to H.264 temporary files.
- $StatsFile = $TempPath + "\vecenc.stats"
- $Output264 = $TempPath + "\" + $File.BaseName + ".264"
- $CriMed264 = $TempPath + "\" + $File.BaseName + ".264_med"
- # Set up the h264 parameters.
- $x264Params.Add('"' + $MP4File + '"')
- $x264Params.Add('--stats "' + $StatsFile + '"')
- $x264Params.Add('--fps ' + $FPS)
- $x264Params.Add('--level 5.2')
- $x264Params.Add('--slices 1')
- $x264Params.Add('--no-scenecut')
- $x264Params.Add('--profile high')
- $x264Params.Add('--threads 0')
- $x264Params.Add('--lookahead-threads 0')
- $x264Params.Add('--aud')
- $x264Params.Add('--direct auto')
- $x264Params.Add('--me umh')
- $x264Params.Add('--nal-hrd vbr')
- $x264Params.Add('--bitrate ' + $BitRate)
- $x264Params.Add('--vbv-maxrate ' + $MaxRate)
- $x264Params.Add('--vbv-bufsize ' + $BufSize)
- $x264Params.Add('--ref 2')
- $x264Params.Add('--bframes 0')
- $x264Params.Add('--b-adapt 0')
- $x264Params.Add('--keyint 30')
- $x264Params.Add('-o "' + $Output264 + '"')
- $x264Params.Add('--opencl')
- $x264Params.Add('--pass 1')
- # Start the first pass.
- Start-Process -FilePath $x264Enc -ArgumentList $x264Params -NoNewWindow -Wait
- # Remove the first pass and add the second.
- $x264Params.Remove('--pass 1') | Out-Null
- $x264Params.Add('--pass 2')
- # Start the second pass.
- Start-Process -FilePath $x264Enc -ArgumentList $x264Params -NoNewWindow -Wait
- #----------------------------------------------------------------------------------------------------------------------------
- # SECTION 04: CRIMED SHRINK OR SOMETHING
- #----------------------------------------------------------------------------------------------------------------------------
- # Add the arguments.
- $CriMedParams.Add('shrink')
- $CriMedParams.Add('-t264')
- $CriMedParams.Add('-dseit0020')
- $CriMedParams.Add('-kvui "' + $CriMed264 + '" "' + $Output264 + '"')
- # I'm still not 100% sure what this does but it seems necessary.
- Start-Process -FilePath $CriMed -ArgumentList $CriMedParams -NoNewWindow -Wait
- #----------------------------------------------------------------------------------------------------------------------------
- # SECTION 05: CREATE FINAL USM VIA SOFDEC2
- #----------------------------------------------------------------------------------------------------------------------------
- # Add the arguments.
- $SofDec2Params.Add('-video00="' + $CriMed264 + '"')
- $SofDec2Params.Add('-out="' + $USMFile + '"')
- $SofDec2Params.Add('-gui_mode=ON')
- $SofDec2Params.Add('-disp_size=' + $Width + ',' + $Height)
- # We are finally to the point a USM file can be created.
- Start-Process -FilePath $SofDec2 -ArgumentList $SofDec2Params -NoNewWindow -Wait
- #----------------------------------------------------------------------------------------------------------------------------
- # SECTION 06: CLEANUP
- #----------------------------------------------------------------------------------------------------------------------------
- # Remove the files we no longer need.
- Remove-Item -LiteralPath $Garbage -ErrorAction "SilentlyContinue" -Force
- Remove-Item -LiteralPath $M2VFile -ErrorAction "SilentlyContinue" -Force
- Remove-Item -LiteralPath $MP4File -ErrorAction "SilentlyContinue" -Force
- Remove-Item -LiteralPath $TempPath -ErrorAction "SilentlyContinue" -Recurse -Force
- }
- Write-Host ""
- Write-Host "Finished. Press any key to close this window."
- [void][System.Console]::ReadKey()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement