Advertisement
BigheadSMZ

Nier Automata Movie Upscale Script

Dec 31st, 2024 (edited)
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 12.19 KB | Source Code | 0 0
  1. #----------------------------------------------------------------------------------------------------------------------------
  2.  
  3. # Bighead's Nier Automata Movie Criware Upscaling/Interpolation Script Deluxe Super Pro v0.1
  4. # For accompanying images, see this link: https://imgur.com/a/neir-automata-movie-upscale-script-folder-hierarchy-gf3zdlQ
  5.  
  6. #----------------------------------------------------------------------------------------------------------------------------
  7.  
  8. # 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
  9. # right direction. Information is scarce when it comes to USM videos, and it's even more scarce when it comes to creating
  10. # H.264 USM videos. Commands fed into the programs have been broken down for easier viewing and editing. Some commands are
  11. # just a string that is appended to, others are an array of strings that PowerShell is able to break down when fed to the
  12. # executables. This is just how I did it, a batch script would have also been sufficient.
  13.  
  14. #----------------------------------------------------------------------------------------------------------------------------
  15.  
  16. # Base folder.
  17. $BaseFolder = Split-Path $script:MyInvocation.MyCommand.Path
  18. $host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(120, 20000)
  19.  
  20. #----------------------------------------------------------------------------------------------------------------------------
  21.  
  22. # The output dimensions of the final video.
  23. $Width  = "2560"
  24. $Height = "1440"
  25.  
  26. # Frames per second to interpolate to.
  27. $FPS = "60"
  28.  
  29. # The bitrate of the videos.
  30. $BitRate = "35000"
  31. $MaxRate = "40000"
  32. $BufSize = "40000"
  33.  
  34. #----------------------------------------------------------------------------------------------------------------------------
  35. # USM Toolkit, requires both VGStream and FFMPeg:
  36. # https://github.com/Rikux3/UsmToolkit
  37. # https://github.com/vgmstream/vgmstream  -- extract to USMToolKit root folder to "vgmstream" folder
  38. # https://github.com/BtbN/FFmpeg-Builds   -- extract "ffmpeg.exe" to USMToolkit root folder
  39. $USMTool = $BaseFolder + "\00 - Tools\UsmToolkit\UsmToolkit.exe"
  40.  
  41. # Topaz Video AI FFMPEG (it's expensive):
  42. # https://www.topazlabs.com/topaz-video-ai
  43. $TopazAI = $BaseFolder + "\00 - Tools\Topaz\App\Topaz Video AI\ffmpeg.exe"
  44.  
  45. # VideoLAN x264 encoder:
  46. # https://www.videolan.org/developers/x264.html
  47. $x264Enc = $BaseFolder + "\00 - Tools\x264\x264.exe"
  48.  
  49. # CriWare SDK Tools
  50. # Search for: "criware sdk archive.org"
  51. $CriMed  = $BaseFolder + "\00 - Tools\Sofdec2\crimed.exe"
  52. $SofDec2 = $BaseFolder + "\00 - Tools\Sofdec2\sofdec2enc.exe"
  53.  
  54. #----------------------------------------------------------------------------------------------------------------------------
  55.  
  56. # Paths to videos.
  57. $USMPath  = $BaseFolder + "\01 - Input USM"
  58. $M2VPath  = $BaseFolder + "\02 - M2V Files"
  59. $MP4Path  = $BaseFolder + "\03 - Upscaled MP4"
  60. $USMDone  = $BaseFolder + "\04 - Output USM"
  61.  
  62. # Temporary path.
  63. $TempPath = $USMDone + "\temp"
  64.  
  65. #----------------------------------------------------------------------------------------------------------------------------
  66. # Loop through the USM files and process each file to the end until a new USM is created.
  67. foreach ($File in Get-ChildItem -LiteralPath $USMPath)
  68. {
  69.   #----------------------------------------------------------------------------------------------------------------------------
  70.   # SECTION 01: DEMUX TO M2V
  71.   #----------------------------------------------------------------------------------------------------------------------------
  72.  
  73.   # Verify the correct file type.
  74.   if ($File.Extension -ne ".usm") { continue }
  75.  
  76.   # Create the working folder if it does exists.
  77.   if (!(Test-Path -LiteralPath $TempPath -ErrorAction "SilentlyContinue")) { New-Item -Path $TempPath -ItemType "Directory" | Out-Null }
  78.  
  79.   # Demux the input file.
  80.   & $USMTool extract $File.FullName
  81.  
  82.   # Paths to the generated files.
  83.   $M2VFile = $USMPath + "\" + $File.BaseName + ".m2v"
  84.  
  85.   # Move the file to the output folder.
  86.   Move-Item -LiteralPath $M2VFile -Destination $M2VPath -Force
  87.  
  88.   #----------------------------------------------------------------------------------------------------------------------------
  89.   # PATHS TO FILES
  90.   #----------------------------------------------------------------------------------------------------------------------------
  91.  
  92.   # Update the path to where the file is now.
  93.   $M2VFile = $M2VPath + "\" + $File.BaseName + ".m2v"
  94.   $MP4File = $MP4Path + "\" + $File.BaseName + ".mp4"
  95.   $USMFile = $USMDone + "\" + $File.BaseName + ".usm"
  96.   $Garbage = $BaseFolder + "\x264_lookahead.clbin"
  97.  
  98.   #----------------------------------------------------------------------------------------------------------------------------
  99.   # SECTION 02: TOPAZ AI (Makes use of its own version of ffmpeg, Topaz AI filtering is done via "-filter_complex" arguments.)
  100.   #----------------------------------------------------------------------------------------------------------------------------
  101.  
  102.   # Topaz model params are just one long string, assemble the interpolation params.
  103.   $Topaz_TVAIFIParams =  'tvai_fi=model=chr-2:'
  104.   $Topaz_TVAIFIParams += 'slowmo=1:'
  105.   $Topaz_TVAIFIParams += 'rdt=0.01:'
  106.   $Topaz_TVAIFIParams += 'fps=' + $FPS + ':'
  107.   $Topaz_TVAIFIParams += 'device=0:'
  108.   $Topaz_TVAIFIParams += 'vram=1:'
  109.   $Topaz_TVAIFIParams += 'instances=1'
  110.  
  111.   # Do the same for the upscaling model params.
  112.   $Topaz_TVAIUPParams =  'tvai_up=model=prob-4:'
  113.   $Topaz_TVAIUPParams += 'scale=0:'
  114.   $Topaz_TVAIUPParams += 'w='+ $Width +':h=' + $Height + ':'
  115.   $Topaz_TVAIUPParams += 'preblur=0.1:'
  116.   $Topaz_TVAIUPParams += 'noise=0.2:'
  117.   $Topaz_TVAIUPParams += 'details=1:'
  118.   $Topaz_TVAIUPParams += 'halo=0.04:'
  119.   $Topaz_TVAIUPParams += 'blur=0:'
  120.   $Topaz_TVAIUPParams += 'compression=0.11:'
  121.   $Topaz_TVAIUPParams += 'estimate=8:'
  122.   $Topaz_TVAIUPParams += 'blend=1:'
  123.   $Topaz_TVAIUPParams += 'device=0:'
  124.   $Topaz_TVAIUPParams += 'vram=1:'
  125.   $Topaz_TVAIUPParams += 'instances=1'
  126.  
  127.   # Do the same for misc filtering stuff.
  128.   $Topaz_ScaleParams =  'scale=w=' + $Width + ':'
  129.   $Topaz_ScaleParams += 'h=' + $Height + ':'
  130.   $Topaz_ScaleParams += 'flags=lanczos:'
  131.   $Topaz_ScaleParams += 'threads=0:'
  132.   $Topaz_ScaleParams += 'force_original_aspect_ratio=decrease'
  133.  
  134.   # Do the same for the padding params.
  135.   $Topaz_PadParams = 'pad=' + $Width + ':' + $Height + ':-1:-1:color=black'
  136.  
  137.   # Compile them all into the complex filter command.
  138.   $Topaz_FilterParam = $Topaz_TVAIFIParams + ',' + $Topaz_TVAIUPParams + ',' + $Topaz_ScaleParams + ',' + $Topaz_PadParams
  139.  
  140.   # Create a list to store the full Topaz parameters.
  141.   $TopazParams = New-Object System.Collections.Generic.List[string]
  142.  
  143.   # Add the parameters to the lists.
  144.   $TopazParams.Add('-hide_banner')
  145.   $TopazParams.Add('-i')
  146.   $TopazParams.Add($M2VFile)
  147.   $TopazParams.Add('-sws_flags')
  148.   $TopazParams.Add('spline+accurate_rnd+full_chroma_int')
  149.   $TopazParams.Add('-filter_complex')
  150.   $TopazParams.Add($Topaz_FilterParam)
  151.   $TopazParams.Add('-c:v')
  152.   $TopazParams.Add('h264_nvenc')
  153.   $TopazParams.Add('-profile:v')
  154.   $TopazParams.Add('high')
  155.   $TopazParams.Add('-pix_fmt')
  156.   $TopazParams.Add('yuv420p')
  157.   $TopazParams.Add('-g')
  158.   $TopazParams.Add('30')
  159.   $TopazParams.Add('-preset')
  160.   $TopazParams.Add('p7')
  161.   $TopazParams.Add('-tune')
  162.   $TopazParams.Add('hq')
  163.   $TopazParams.Add('-rc')
  164.   $TopazParams.Add('constqp')
  165.   $TopazParams.Add('-qp')
  166.   $TopazParams.Add('18')
  167.   $TopazParams.Add('-rc-lookahead')
  168.   $TopazParams.Add('20')
  169.   $TopazParams.Add('-spatial_aq')
  170.   $TopazParams.Add('1')
  171.   $TopazParams.Add('-aq-strength')
  172.   $TopazParams.Add('15')
  173.   $TopazParams.Add('-b:v')
  174.   $TopazParams.Add('0')
  175.   $TopazParams.Add('-an')
  176.   $TopazParams.Add('-map_metadata')
  177.   $TopazParams.Add('0')
  178.   $TopazParams.Add('-map_metadata:s:v')
  179.   $TopazParams.Add('0:s:v')
  180.   $TopazParams.Add('-fps_mode:v')
  181.   $TopazParams.Add('passthrough')
  182.   $TopazParams.Add('-movflags')
  183.   $TopazParams.Add('frag_keyframe+empty_moov+delay_moov+write_colr')
  184.   $TopazParams.Add('-bf')
  185.   $TopazParams.Add('0')
  186.   $TopazParams.Add($MP4File)
  187.  
  188.   # Create the upscaled interpolated result.
  189.   & $TopazAI $TopazParams
  190.  
  191.   #----------------------------------------------------------------------------------------------------------------------------
  192.   # SECTION 03: H.264 ENCODER
  193.   #----------------------------------------------------------------------------------------------------------------------------
  194.  
  195.   # I found the easiest way to handle arguments is just add them to lists and let PowerShell sort out how they are added.
  196.   $x264Params    = New-Object System.Collections.Generic.List[string]
  197.   $CriMedParams  = New-Object System.Collections.Generic.List[string]
  198.   $SofDec2Params = New-Object System.Collections.Generic.List[string]
  199.  
  200.   # Paths to H.264 temporary files.
  201.   $StatsFile = $TempPath + "\vecenc.stats"
  202.   $Output264 = $TempPath + "\" + $File.BaseName + ".264"
  203.   $CriMed264 = $TempPath + "\" + $File.BaseName + ".264_med"
  204.  
  205.   # Set up the h264 parameters.
  206.   $x264Params.Add('"' + $MP4File + '"')
  207.   $x264Params.Add('--stats "' + $StatsFile + '"')
  208.   $x264Params.Add('--fps ' + $FPS)
  209.   $x264Params.Add('--level 5.2')
  210.   $x264Params.Add('--slices 1')
  211.   $x264Params.Add('--no-scenecut')
  212.   $x264Params.Add('--profile high')
  213.   $x264Params.Add('--threads 0')
  214.   $x264Params.Add('--lookahead-threads 0')
  215.   $x264Params.Add('--aud')
  216.   $x264Params.Add('--direct auto')
  217.   $x264Params.Add('--me umh')
  218.   $x264Params.Add('--nal-hrd vbr')
  219.   $x264Params.Add('--bitrate ' + $BitRate)
  220.   $x264Params.Add('--vbv-maxrate ' + $MaxRate)
  221.   $x264Params.Add('--vbv-bufsize ' + $BufSize)
  222.   $x264Params.Add('--ref 2')
  223.   $x264Params.Add('--bframes 0')
  224.   $x264Params.Add('--b-adapt 0')
  225.   $x264Params.Add('--keyint 30')
  226.   $x264Params.Add('-o "' + $Output264 + '"')
  227.   $x264Params.Add('--opencl')
  228.   $x264Params.Add('--pass 1')
  229.  
  230.   # Start the first pass.
  231.   Start-Process -FilePath $x264Enc -ArgumentList $x264Params -NoNewWindow -Wait
  232.  
  233.   # Remove the first pass and add the second.
  234.   $x264Params.Remove('--pass 1') | Out-Null
  235.   $x264Params.Add('--pass 2')
  236.  
  237.   # Start the second pass.
  238.   Start-Process -FilePath $x264Enc -ArgumentList $x264Params -NoNewWindow -Wait
  239.  
  240.   #----------------------------------------------------------------------------------------------------------------------------
  241.   # SECTION 04: CRIMED SHRINK OR SOMETHING
  242.   #----------------------------------------------------------------------------------------------------------------------------
  243.  
  244.   # Add the arguments.
  245.   $CriMedParams.Add('shrink')
  246.   $CriMedParams.Add('-t264')
  247.   $CriMedParams.Add('-dseit0020')
  248.   $CriMedParams.Add('-kvui "' + $CriMed264 + '" "' + $Output264 + '"')
  249.  
  250.   # I'm still not 100% sure what this does but it seems necessary.
  251.   Start-Process -FilePath $CriMed -ArgumentList $CriMedParams -NoNewWindow -Wait
  252.  
  253.   #----------------------------------------------------------------------------------------------------------------------------
  254.   # SECTION 05: CREATE FINAL USM VIA SOFDEC2
  255.   #----------------------------------------------------------------------------------------------------------------------------
  256.  
  257.   # Add the arguments.
  258.   $SofDec2Params.Add('-video00="' + $CriMed264 + '"')
  259.   $SofDec2Params.Add('-out="' + $USMFile + '"')
  260.   $SofDec2Params.Add('-gui_mode=ON')
  261.   $SofDec2Params.Add('-disp_size=' + $Width + ',' + $Height)
  262.  
  263.   # We are finally to the point a USM file can be created.
  264.   Start-Process -FilePath $SofDec2 -ArgumentList $SofDec2Params -NoNewWindow -Wait
  265.  
  266.   #----------------------------------------------------------------------------------------------------------------------------
  267.   # SECTION 06: CLEANUP
  268.   #----------------------------------------------------------------------------------------------------------------------------
  269.  
  270.   # Remove the files we no longer need.
  271.   Remove-Item -LiteralPath $Garbage  -ErrorAction "SilentlyContinue" -Force
  272.   Remove-Item -LiteralPath $M2VFile  -ErrorAction "SilentlyContinue" -Force
  273.   Remove-Item -LiteralPath $MP4File  -ErrorAction "SilentlyContinue" -Force
  274.   Remove-Item -LiteralPath $TempPath -ErrorAction "SilentlyContinue" -Recurse -Force
  275. }
  276. Write-Host ""
  277. Write-Host "Finished. Press any key to close this window."
  278. [void][System.Console]::ReadKey()
  279.  
  280.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement