KilroytheKilljoy

Video Preparation Script_16

Apr 19th, 2021 (edited)
1,228
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #[USER CONFIGURATION]
  2. $watermarkOpacity = "0.25";
  3. $watermarkSource = @("E:\Atukam\watermark_640.png", "E:\Atukam\watermark_320.png");
  4.  
  5. $operatingMode = "1"
  6. #0: Skip final video package creation
  7. #1: Perform final video package creation
  8.  
  9. #[INPUT FILE ACQUISITION]
  10. #Get the input file and format the working path and name variables
  11. $sourceFile = Read-Host -Prompt "Input the full path to the source video (or drag it onto this window)";
  12. $sourceFile = $sourceFile.Trim('"');
  13. $projectLoc = Split-Path -Path $sourceFile;
  14. $projectLoc += "\";
  15. #(i know i can just use -LeafBase here instead but this is for compatibility with pre-6.0 powershell)
  16. $workingName = Split-Path -Path $sourceFile -Leaf;
  17. $workingName = $workingName -creplace '(?=\.).*', '';
  18.  
  19. #[WORKSPACE DIRECTORY CREATION]
  20. cd "$projectLoc"
  21. #Create the temporary working folder inside the source file's root directory
  22. Write-Host "Creating temporary working folder..."
  23. New-Item -Path ".\" -Name "$workingName" -ItemType "directory"
  24. $workingFolder = -join($projectLoc, $workingName, "\");
  25.  
  26. #[INPUT SPECIFICATION ACQUISITION]
  27. #Detect the dimensions of the input footage
  28. $inputWidth = ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=nokey=1:noprint_wrappers=1 "$sourceFile"
  29. $inputHeight = ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=nokey=1:noprint_wrappers=1 "$sourceFile"
  30. #Detect the pixel aspect ratio
  31. $aspectQuery = ffprobe -v error -select_streams v:0 -show_entries stream=sample_aspect_ratio -of default=nokey=1:noprint_wrappers=1 "$sourceFile"
  32. #Handle any errors with pixel aspect ratio detection.
  33. Switch ($aspectQuery){
  34.     {$_ -eq "N/A"} {
  35.         $pixelAspect = 1;
  36.         Write-Host "Error determining pixel aspect ratio. Defaulting to 1.";
  37.     };
  38.     {$_ -ne "N/A"} {
  39.         #Convert the pixel aspect ratio to decimal form
  40.         $pixelAspect = $aspectQuery.split(":");
  41.         $pixelAspect = ([int]$pixelAspect[0] / [int]$pixelAspect[1]);
  42.         #Correct aspect if operating with non-square pixel aspect ratio
  43.         If ($pixelAspect -ne "1"){
  44.             $inputWidth = ([int]$inputWidth * $pixelAspect);
  45.             Write-Host "Aspect-correcting width to $inputWidth."
  46.         };
  47.     };
  48. };
  49. #Grab the total number of frames
  50. Write-Host "Getting fame count..."
  51. #NOTE: this method will have issues with certain formats.
  52. $frameCount = ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 "$sourceFile"
  53. #Backup method to handle incompatibilities with certain formats.
  54. If ($frameCount -eq "N/A"){
  55.     Write-Host "Error acquiring frame count from input file's metadata. Counting frames manually..."
  56.     $frameCount = ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 "$sourceFile";
  57. };
  58. Write-Host "Frame count of $frameCount detected."
  59.  
  60. #[VIDEO OUTPUT SPECIFICATION DECLARATION]
  61. #Determine the optimal target dimensions to avoid upscaling sub-SD input
  62. Switch ($pixelAspect){
  63.     #Check if the video width is greater than its height
  64.     {$_ -gt 1} {
  65.         Switch ($inputWidth){
  66.             {[int]$inputWidth -ge "640"}{
  67.                 #Constrain to a maximum resolution of 640x480
  68.                 [int]$targetWidth = "640";
  69.                 [int]$targetHeight = "480";
  70.                 $watermarkSource = $watermarkSource[0];
  71.             };
  72.             {[int]$inputWidth -lt "640"}{
  73.                 #Constrain to a maximum resolution of 320x240
  74.                 [int]$targetWidth = "320";
  75.                 [int]$targetHeight = "240";
  76.                 $watermarkSource = $watermarkSource[1];
  77.             };
  78.         };
  79.     };
  80.     #Check if the video height is greater than or equal to its width
  81.     {$_ -le 1} {
  82.         Switch ($inputHeight){
  83.             {[int]$inputHeight -ge "480"}{
  84.                 #Constrain to a maximum resolution of 640x480
  85.                 [int]$targetWidth = "640";
  86.                 [int]$targetHeight = "480";
  87.                 $watermarkSource = $watermarkSource[0];
  88.             };
  89.             {[int]$inputHeight -lt "480"}{
  90.                 #Constrain to a maximum resolution of 320x240
  91.                 [int]$targetWidth = "320";
  92.                 [int]$targetHeight = "240";
  93.                 $watermarkSource = $watermarkSource[1];
  94.             };
  95.         };
  96.     };
  97. };
  98. #Check if the output height is divisible by 2, and decrement it by 1 until it is (for h.264 compatibility)
  99. while(1 -eq $outHeight % 2){
  100.     $outHeight--;
  101. };
  102.  
  103. #[THUMBNAIL OUTPUT SPECIFICATION DECLARATION]
  104. [int]$xOffset = 0;
  105. [int]$yOffset = 0;
  106. Switch ($inputWidth){
  107.     {[int]$inputWidth -gt [int]$inputHeight} {
  108.         $outWidth = $targetWidth;
  109.         $scaleFactor = $outWidth / $inputWidth;
  110.         $outHeight = [Math]::Round(([int]$inputHeight * $scaleFactor), 0);
  111.         Write-Host "Constrained by width of $outWidth";
  112.         #Determine the Y offset value for thumbnail letterboxing
  113.         [int]$yOffset = ((($targetHeight * -1) + $outHeight) / 2);
  114.         Write-Host "Using vertical offset of $yOffset for thumbnail letterboxing."
  115.     };
  116.     {[int]$inputWidth -le [int]$inputHeight} {
  117.         $outHeight = $targetHeight;
  118.         $scaleFactor = $outHeight / $inputHeight;
  119.         $outWidth = [Math]::Round(([int]$inputWidth * $scaleFactor), 0);
  120.         Write-Host "Constrained by height of $outHeight";
  121.         #Determine the X offset value for thumbnail pillarboxing
  122.         [int]$xOffset = ((($targetWidth * -1) + $outWidth) / 2);
  123.         Write-Host "Using horizontal offset of $xOffset for thumbnail pillarboxing.";
  124.     };
  125. };
  126.  
  127. #[THUMBNAIL OUTPUT]
  128. #Determine what frame the midpoint of the video occurs at
  129. $middleFrame = [Math]::Round($frameCount / 2, 0) - 1;
  130. Write-Host "Video midpoint occurs at frame $middleFrame"
  131. Write-Host "Using frame $middleFrame for thumbnail source."
  132. #Grab the midpoint frame as an image file
  133. Write-Host "Extracting thumbnail..."
  134. ffmpeg -i "$sourceFile" -vf "select=eq(n\,$middleFrame),scale=($outWidth):($outHeight),pad=width=($targetWidth):height=($targetHeight):x=($xOffset):y=($yOffset):color=black" -vframes 1 ($workingFolder + $workingName + "_web_thumb.jpg")
  135. Write-Host "Thumbnail successfully extracted."
  136.  
  137. #[VIDEO OUTPUT]
  138. #Rescale and add watermark
  139. Write-Host "Processing video..."
  140. ffmpeg -i $sourceFile -i $watermarkSource -filter_complex "[0:v]scale=($outWidth):($outHeight),setsar=1[scaled],[1:v]format=argb,colorchannelmixer=aa=($watermarkOpacity)[watermark],[scaled][watermark]overlay=0:0[out]" -map [out] -map 0:a -c:v h264_nvenc -rc:v vbr -cq:v 19 -b:v 750k -maxrate:v 1500k -profile:v high ($workingFolder + $workingName + "_web.mp4")
  141. Write-Host "Video successfully processed."
  142.  
  143. if ($operatingMode -eq "1"){
  144.  
  145. #[PACKAGE OUTPUT]
  146. #Pack the files into an archive
  147. $toZip = -join($workingFolder, "*.*")
  148. $zippedUp = -join($projectLoc, $workingName, ".zip")
  149. Compress-Archive -Path $toZip -DestinationPath $zippedUp
  150. Rename-Item -Path $zippedUp -NewName "$workingName.vpak"
  151. Write-Host "Video package successfully created."
  152.  
  153. #[CLEANUP]
  154. #Delete the working folder and its contents
  155. Remove-Item $workingFolder -Recurse
  156.  
  157. };
  158.  
  159. Read-Host -Prompt "All processing has concluded. Press any key to close this window."
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×