Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[USER CONFIGURATION]
- $watermarkOpacity = "0.25";
- $watermarkSource = @("E:\Atukam\watermark_640.png", "E:\Atukam\watermark_320.png");
- $operatingMode = "1"
- #0: Skip final video package creation
- #1: Perform final video package creation
- #[INPUT FILE ACQUISITION]
- #Get the input file and format the working path and name variables
- $sourceFile = Read-Host -Prompt "Input the full path to the source video (or drag it onto this window)";
- $sourceFile = $sourceFile.Trim('"');
- $projectLoc = Split-Path -Path $sourceFile;
- $projectLoc += "\";
- #(i know i can just use -LeafBase here instead but this is for compatibility with pre-6.0 powershell)
- $workingName = Split-Path -Path $sourceFile -Leaf;
- $workingName = $workingName -creplace '(?=\.).*', '';
- #[WORKSPACE DIRECTORY CREATION]
- cd "$projectLoc"
- #Create the temporary working folder inside the source file's root directory
- Write-Host "Creating temporary working folder..."
- New-Item -Path ".\" -Name "$workingName" -ItemType "directory"
- $workingFolder = -join($projectLoc, $workingName, "\");
- #[INPUT SPECIFICATION ACQUISITION]
- #Detect the dimensions of the input footage
- $inputWidth = ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=nokey=1:noprint_wrappers=1 "$sourceFile"
- $inputHeight = ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=nokey=1:noprint_wrappers=1 "$sourceFile"
- #Detect the pixel aspect ratio
- $aspectQuery = ffprobe -v error -select_streams v:0 -show_entries stream=sample_aspect_ratio -of default=nokey=1:noprint_wrappers=1 "$sourceFile"
- #Handle any errors with pixel aspect ratio detection.
- Switch ($aspectQuery){
- {$_ -eq "N/A"} {
- $pixelAspect = 1;
- Write-Host "Error determining pixel aspect ratio. Defaulting to 1.";
- };
- {$_ -ne "N/A"} {
- #Convert the pixel aspect ratio to decimal form
- $pixelAspect = $aspectQuery.split(":");
- $pixelAspect = ([int]$pixelAspect[0] / [int]$pixelAspect[1]);
- #Correct aspect if operating with non-square pixel aspect ratio
- If ($pixelAspect -ne "1"){
- $inputWidth = ([int]$inputWidth * $pixelAspect);
- Write-Host "Aspect-correcting width to $inputWidth."
- };
- };
- };
- #Grab the total number of frames
- Write-Host "Getting fame count..."
- #NOTE: this method will have issues with certain formats.
- $frameCount = ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 "$sourceFile"
- #Backup method to handle incompatibilities with certain formats.
- If ($frameCount -eq "N/A"){
- Write-Host "Error acquiring frame count from input file's metadata. Counting frames manually..."
- $frameCount = ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 "$sourceFile";
- };
- Write-Host "Frame count of $frameCount detected."
- #[VIDEO OUTPUT SPECIFICATION DECLARATION]
- #Determine the optimal target dimensions to avoid upscaling sub-SD input
- Switch ($pixelAspect){
- #Check if the video width is greater than its height
- {$_ -gt 1} {
- Switch ($inputWidth){
- {[int]$inputWidth -ge "640"}{
- #Constrain to a maximum resolution of 640x480
- [int]$targetWidth = "640";
- [int]$targetHeight = "480";
- $watermarkSource = $watermarkSource[0];
- };
- {[int]$inputWidth -lt "640"}{
- #Constrain to a maximum resolution of 320x240
- [int]$targetWidth = "320";
- [int]$targetHeight = "240";
- $watermarkSource = $watermarkSource[1];
- };
- };
- };
- #Check if the video height is greater than or equal to its width
- {$_ -le 1} {
- Switch ($inputHeight){
- {[int]$inputHeight -ge "480"}{
- #Constrain to a maximum resolution of 640x480
- [int]$targetWidth = "640";
- [int]$targetHeight = "480";
- $watermarkSource = $watermarkSource[0];
- };
- {[int]$inputHeight -lt "480"}{
- #Constrain to a maximum resolution of 320x240
- [int]$targetWidth = "320";
- [int]$targetHeight = "240";
- $watermarkSource = $watermarkSource[1];
- };
- };
- };
- };
- #Check if the output height is divisible by 2, and decrement it by 1 until it is (for h.264 compatibility)
- while(1 -eq $outHeight % 2){
- $outHeight--;
- };
- #[THUMBNAIL OUTPUT SPECIFICATION DECLARATION]
- [int]$xOffset = 0;
- [int]$yOffset = 0;
- Switch ($inputWidth){
- {[int]$inputWidth -gt [int]$inputHeight} {
- $outWidth = $targetWidth;
- $scaleFactor = $outWidth / $inputWidth;
- $outHeight = [Math]::Round(([int]$inputHeight * $scaleFactor), 0);
- Write-Host "Constrained by width of $outWidth";
- #Determine the Y offset value for thumbnail letterboxing
- [int]$yOffset = ((($targetHeight * -1) + $outHeight) / 2);
- Write-Host "Using vertical offset of $yOffset for thumbnail letterboxing."
- };
- {[int]$inputWidth -le [int]$inputHeight} {
- $outHeight = $targetHeight;
- $scaleFactor = $outHeight / $inputHeight;
- $outWidth = [Math]::Round(([int]$inputWidth * $scaleFactor), 0);
- Write-Host "Constrained by height of $outHeight";
- #Determine the X offset value for thumbnail pillarboxing
- [int]$xOffset = ((($targetWidth * -1) + $outWidth) / 2);
- Write-Host "Using horizontal offset of $xOffset for thumbnail pillarboxing.";
- };
- };
- #[THUMBNAIL OUTPUT]
- #Determine what frame the midpoint of the video occurs at
- $middleFrame = [Math]::Round($frameCount / 2, 0) - 1;
- Write-Host "Video midpoint occurs at frame $middleFrame"
- Write-Host "Using frame $middleFrame for thumbnail source."
- #Grab the midpoint frame as an image file
- Write-Host "Extracting thumbnail..."
- 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")
- Write-Host "Thumbnail successfully extracted."
- #[VIDEO OUTPUT]
- #Rescale and add watermark
- Write-Host "Processing video..."
- 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")
- Write-Host "Video successfully processed."
- if ($operatingMode -eq "1"){
- #[PACKAGE OUTPUT]
- #Pack the files into an archive
- $toZip = -join($workingFolder, "*.*")
- $zippedUp = -join($projectLoc, $workingName, ".zip")
- Compress-Archive -Path $toZip -DestinationPath $zippedUp
- Rename-Item -Path $zippedUp -NewName "$workingName.vpak"
- Write-Host "Video package successfully created."
- #[CLEANUP]
- #Delete the working folder and its contents
- Remove-Item $workingFolder -Recurse
- };
- Read-Host -Prompt "All processing has concluded. Press any key to close this window."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement