How does it look now?

###########################################################################################
################################# ScanMedia.ps1 ###########################################
###########################################################################################
#Requires -Version 3

<#

.SYNOPSIS
    A validation check on video files to make sure there are no errors with the file. The script is able to auto-repair some encoding issues with video files.
    The script will keep track of all scanned files that are not deleted so that it can be setup to re-scan the same directory but only newly added files will be scanned.
    Script use: Takes a PATH parameter to a folder for scanning media

.EXAMPLE
.\ScanMedia.ps1 -Path "C:\Media\Videos"
    Both recursively scans the folder C:\Media\Videos for errors

.\ScanMedia.ps1 -AutoRepair "C:\Media\Videos"
    To scan media and attempt to Auto Repair files use -AutoRepair

.\ScanMedia.ps1 -Rescan "C:\Media\Videos"
    To scan media and force a re-scan of all files use -Rescan

.\ScanMedia.ps1 -CRF 18 -Path "C:\Media\Videos" -AutoRepair
    To Change the CRF Value used for encodes, change the following

.\ScanMedia.ps1 -Path "C:\Media\Videos" -LimitCPU -AutoRepair -RemoveOriginal -RemoveRepaired
    Scans the path C:\Media\Videos with lower CPU usage and attempts to repair the files that are found to have an issue.
    The script will delete repaired files that don't repair properly and it will delete the original files of successfully repaired files.

.NOTES
    Changelog:
        1.O - Initial Script creation
        1.1 - Separated the Error Log files as some could be very big
        1.2 - Incorporated an Auto-Repair function
        1.3 - Added additional logging to CSV file for easy sorting
        1.4 - Corrected issue with detecting old error logs
        1.5 - Added check to make sure ffmpeg exists and change calling behavior
        1.6 - Implemented Join-Path to allow script to be run on non-Windows machines
        1.7 - Enable file scanned history and added a -Rescan switch to force scanning
                all files.
        1.8 - Found an Error with Get-ChildItem and -path where it wouldn't scan top
                level folders with [] in the name. Using -LiteralPath now.
        1.9 - Fixed an issue with LiteralPath ignoring ignored extensions
        1.9.1 Changed how the auto-repair function worked to duplicate plex's optimized
                version settings to create an x264 file to better address file errors. It
                can now correct more issues but not everything. Added a CRF argument so
                users can now select the quality of the repaired video files.
                Enabled AutoDelete on repaired files only that have failed checks
        1.9.2 Corrected issue with not getting the file size on a video file for a PASSED
                video file. Modified the writing of the CSV into a function to remove commas
                from file names
        2.0 - Changed ScanPath to Path. Corrected double logging of repaired files. Bugfixes.
                Moved log directories into a Log folder for better organization
                Condensed the method of writing output
        2.1 - Correct issue with logging skipped files
        2.2 - Added the ability to Limit CPU usage to half the cores detected on the machine.
                Added the ability to remove original files and all files that are found with errors.
        2.3 - Corrected issue with testing a path of a file containing "[]" characters
        3.0 - Major Update: Massive re-write and new feature introduction. New Menu for easier user interaction. New scanning user interface. Updated output and moved more information to the log file.
                Added the following new parameters:
                    -ConfigFile
                    -IdleScan
                    -BypassMenu
                    -GPUEncoding
                    -Extension
                    -ContinuousScan
                Moved the parameter overview into the user interface help menu. Created new methods and options for automation including -ConfigFile, -IdleScan, -ContinuousScan
                New built in version checker which will scan the GitHub repo on startup of the user interface and check for a new version.
                New FFMPEG utility menu which will help the user download a copy of FFMPEG and place it in the correct directory.
                New Help menu to help guide users on the functionality of the script.
                New CPU idle function that will only scan when the machine's CPU is idle and not working on other processes
                A bunch of bugfixes
        3.1 - New Feature: -TrashEnabled This command will allow you to send files are found to have errors to a single directory for further analysis and some minor bugfixes
        3.2 - Added a check to make sure write access was available to the ffmpeg binary
        3.3 - Added arguments for FFMPEG to increase compatibility with larger file formats (Should fix Too many packets buffered for output stream error)
            - Changed file extension exclusion list to inclusion list, so now it will only target media files with the extensions provided in $include
            - Changed some wording surrounding the failed deletion prompt for better understanding
        3.4 - Fixed output when scans were started on very long file paths
            - Added more feedback to let the user know a scan was in progress
        3.5 - Correct issues with running inside of PowerShell ISE

                Note: It continue to use the command line parameters for calling syntax through 3rd party applications, please include the new parameter -BypassMenu to avoid user interaction from halting the script.
                Note: If resizing the PowerShell window during a scan, the text will adjust to the new screen size on an output change. So just wait and it will refresh and correct the output.
                Note: The new scanning output will only show the results of the last 30 items scanned. The log file must be used to view previous scan results. A log viewing tool is recommended when viewing the log files.
        3.6 - Corrected issues with determining terminal size in specific use cases. Added file encoding thanks to recommendation by LordKenmou. Fixed update script checker.

    You are able to take advantage of encoding with GPU Acceleration on linux and windows if you compile the ffmpeg binaries to support nvec.
    This has not been tested with this script and is not supported but it should still be possible.
    Please refer to this link to see if your GPU is supported: https://developer.nvidia.com/ffmpeg
    1: Compile FFMPEG using this cross compile script:
    https://github.com/rdp/ffmpeg-windows-build-helpers
    2: Verify that the new NVENC encoders are now included by using the following commands against the the compiled FFMPEG binaries:
    ffmpeg -encoders
    ffmpeg -decoders
    3: Find and replace the following term "libx264" with "h264_nvenc" in this script
    Encoding should now be utilizing the GPU instead of the CPU

    Thank you to all the users for their feedback on issues and feature requests
#>

[CmdletBinding()]
param (
    [String]$Path,
    [String]$ConfigFile,
    [switch]$AutoRepair,
    [switch]$Rescan,
    [Int]$CRF = 21,
    [switch]$LimitCPU,
    [switch]$RemoveAll,
    [switch]$RemoveRepaired,
    [switch]$RemoveOriginal,
    [switch]$IAcceptResponsibility,
    [switch]$IdleScan,
    [switch]$BypassMenu,
    [switch]$GPUEncoding,
    [String]$Extension = "mp4",
    [switch]$ContinuousScan,
    [String]$TrashEnabled
)

# Included file types. Add more extensions that you want included with the media scan
$Include = ".mp4", ".avi", ".mkv", ".m4v", ".ogg", ".mpeg", ".wmv", ".flv", ".mov", ".ogv", ".mpg", ".m4a", ".asf", ".ts", ".divx"

# Script Variables
[float]$ScriptVersion = 3.6
$FullName = ""
$FileName = ""
$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$ScanHistory = Join-Path -Path $ScriptPath -ChildPath "ScanHistory.txt"
$Date = "$((Get-Date).ToString('yyyy-MM-dd'))"
$LogDir = Join-Path -Path $ScriptPath -ChildPath "log"
$LogPath = Join-Path -Path $LogDir -ChildPath "log_$Date"
$FFmpegLog = Join-Path -Path $LogPath -ChildPath "ffmpegerror.log"
$LogFile = Join-Path -Path $LogPath -ChildPath "results.log"
$CSVFile = Join-Path -Path $LogPath -ChildPath "results.csv"
$env:PATH = $env:PATH + ";."
$global:OrigLength = $null
$global:RepairLength = $null
$global:RepairedFile = $null
$global:OriginalFile = $null
$global:OriginalRemoved = $false
$global:CPULimit = 1
$global:VideoLibrary = "libx264"

# Convert all Supplied Parameters to Global variables for use in all functions
$global:Path = $Path
$global:ConfigFile = $ConfigFile
$global:AutoRepair = $AutoRepair
$global:Rescan = $Rescan
$global:CRF = $CRF
$global:LimitCPU = $LimitCPU
$global:RemoveAll = $RemoveAll
$global:RemoveRepaired = $RemoveRepaired
$global:RemoveOriginal = $RemoveOriginal
$global:IAcceptResponsibility = $IAcceptResponsibility
$global:IdleScan = $IdleScan
$global:GPUEncoding = $GPUEncoding
$global:RepairVFileExtension = $Extension
$global:ContinuousScan = $ContinuousScan
$global:TrashEnabled = $TrashEnabled

# Function to write lines to a log file
function LogWrite {
    param (
        [String]$LogString,
        [String]$Color,
        [switch]$Log)

    if (!($log)) {
        if ($Color) {
            Write-Host "$(Get-Date): $LogString" -ForegroundColor $Color
        }
        else {
            Write-Host "$(Get-Date): $LogString"
        }
    }
    Add-Content $LogFile -Value "$(Get-Date): $LogString" -Force
}

function Write-PauseMessage {
    param(
        [String]$Message)
    # Check if running PowerShell ISE
    if ($psISE) {
        Add-Type -AssemblyName System.Windows.Forms
        [System.Windows.Forms.MessageBox]::Show($Message)
    }
    else {
        Write-Host $Message -ForegroundColor Yellow
        $null = (Get-Host).UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
}

function Write-HostCenter {
    param(
        [String]$Message,
        [String]$Color)

    try {
        if ($Color) {
            Write-Host ("{0}{1}" -f (' ' * (([Math]::Max(0, (Get-Host).UI.RawUI.BufferSize.Width / 2) - [Math]::Floor($Message.Length / 2)))), $Message) -ForegroundColor $Color
        }
        else {
            Write-Host ("{0}{1}" -f (' ' * (([Math]::Max(0, (Get-Host).UI.RawUI.BufferSize.Width / 2) - [Math]::Floor($Message.Length / 2)))), $Message)
        }
    }
    # Catch exception if unable to determine terminal size and just write messages normally
    catch {
        if ($Color) {
            Write-Host $Message -ForegroundColor $Color
        }
        else {
            Write-Host $Message
        }
    }
}

# Function to attempt to repair a video object with ffmpeg.exe if -AutoRepair is selected
function RepairFile {
    param ([Object]$VideoFile)

    # Get only the file name of the Video Object
    $RepairFileName = $VideoFile.Name

    # Get only the file name without the extension of the video object and remove the brackets
    $RepairFileBaseName = $VideoFile.BaseName -replace '[][]', ''

    # Get all the variables to create the another file in the same directory with _repaired in the filename
    $Dir = $VideoFile.DirectoryName
    $RepairVFileName = "$RepairFileBaseName" + "_repaired.$global:RepairVFileExtension"
    $RepairFullName = Join-Path -Path $Dir -ChildPath $RepairVFileName

    if (!(Test-Path -LiteralPath $RepairFullName)) {
        LogWrite "Creating Repair File: $RepairFullName" -Log

        $RepairFileName = $RepairFileName -replace '[][]', ''
        $RepairFileName = $RepairFileName + "_repair.log"

        $ErrorLog = Join-Path -Path $LogPath -ChildPath $RepairFileName

        # Attempt to repair the video file with FFmpeg
        if ($global:LimitCPU) {
            ffmpeg.exe -y -i $VideoFile.FullName -max_muxing_queue_size 1024 -c:v $global:VideoLibrary -crf $global:CRF -c:a aac -q:a 100 -strict -2 -threads $global:CPULimit -movflags faststart -level 41 $RepairFullName 2> $ErrorLog
        }
        else {
            ffmpeg.exe -y -i $VideoFile.FullName -max_muxing_queue_size 1024 -c:v $global:VideoLibrary -crf $global:CRF -c:a aac -q:a 100 -strict -2 -movflags faststart -level 41 $RepairFullName 2> $ErrorLog
        }

        # Check to see if the repaired file still has errors
        $RepairFile = Get-Item $RepairFullName

        # Check the scan history file to see if the file has been scanned
        $ScanHistoryCheck = (Get-Content $ScanHistory -Encoding utf8 | Select-String -pattern $RepairFile.FullName -SimpleMatch)

        if ($RepairFile.Name.Length -lt 1) {
            LogWrite "There was an issue creating the repaired file with FFmpeg" -Log
        }
        elseif ($null -ne $ScanHistoryCheck) {
            CheckFile $RepairFile -AutoRepaired -RescanVideo
        }
        else {
            CheckFile $RepairFile -AutoRepaired
        }
    }
    else {
        LogWrite "Skipping repair: Repaired File already exists." -Log
        LogWrite "Repair file found: $RepairFullName" -Log
        $OriginalFile = Get-ChildItem -LiteralPath $global:OriginalFile.FullName
        Update-VideoArray -VideoFile $OriginalFile -Status "Failed" -Update
        Write-ScanOutput
    }
}

# Function to check a supplied video object with ffmpeg.exe
function CheckFile {
    param (
        [Object]$VideoFile,
        [switch]$AutoRepaired,
        [switch]$RescanVideo
    )

    # If the file is a new file being check, clear out the variables
    if (!($AutoRepaired)) {
        $global:RepairLength = $null
        $global:RepairedFile = $null
        $global:OrigLength = $null
        $global:OriginalFile = $null
    }

    # Reset the Deleted value
    $Deleted = $false

    # Get the full name of the Video Object with path included
    $FullName = $VideoFile.FullName

    # Get only the name of the video file
    $FileName = $VideoFile.Name

    # Counter for the number of files that have been scanned
    $global:VideoCount = $global:VideoCount + 1

    # Save the length and object of the video file to make sure the repaired video file length matches original
    if ($AutoRepaired) {
        $global:RepairLength = GetLength $VideoFile
        $global:RepairedFile = $VideoFile
    }
    else {
        $global:OrigLength = GetLength $VideoFile
        $global:OriginalFile = $VideoFile
    }

    if ($RescanVideo) {
        Update-VideoArray -VideoFile $VideoFile -Status "Re-Scanning"
    }
    else {
        Update-VideoArray -VideoFile $VideoFile -Status "Scanning"
    }

    LogWrite "Scanning File: $FullName" -Log
    Write-ScanOutput -Scanning

    if (Test-Path $FFmpegLog) {
        try {
            Remove-Item -LiteralPath $FFmpegLog -Force
        }
        catch {
            $ErrorMessage = $_.Exception.Message
            LogWrite "Error while removing old FFmpeg Log: $ErrorMessage" -Color Red
            LogWrite "Scan cannot continue without the ability to modify: $FFmpegLog"
            exit 1
        }
    }

    # Scan the file with FFmpeg
    if ($global:LimitCPU) {
        ffmpeg.exe -v error -i $FullName -max_muxing_queue_size 1024 -f null -threads $global:CPULimit - >$FFmpegLog 2>&1
    }
    else {
        ffmpeg.exe -v error -i $FullName -max_muxing_queue_size 1024 -f null - >$FFmpegLog 2>&1
    }

    # Check to see if the FFmpeg error log was empty
    if ($null -eq (Get-Content $FFmpegLog)) {
        # Get information to log to the CSV file
        $FileSize = "{0:N2}" -f (($VideoFile | Measure-Object -property length -sum ).sum / 1MB)
        $Date = $((Get-Date).ToString('yyyy-MM-dd'))

        # If the file is an Auto-Repaired File
        if ($AutoRepaired) {
            # File is only repaired successfully if the video file length matches original
            if (($global:RepairLength -eq $global:OrigLength) -and ($null -ne $global:RepairLength)) {
                LogWrite "File Repaired Successfully: $FullName" -Log
                Update-VideoArray -VideoFile $VideoFile -Status "Passed" -Update
                Write-ScanOutput
                Write-CSV -VidFileName $FileName -VidFilePath $FullName -TestResults "Passed" -Date $Date -VidFileSize $FileSize -VidLength $RepairLength
                $OriginalFile = Get-ChildItem -LiteralPath $global:OriginalFile.FullName
                Update-VideoArray -VideoFile $OriginalFile -Status "Failed" -Update
                Write-ScanOutput

                if ($global:RemoveOriginal -or $global:RemoveAll -or $global:TrashEnabled) {
                    # Remove the original file and rename the repair file
                    try {
                        if ($global:TrashEnabled) {
                            LogWrite "Moving the original file: $($global:OriginalFile.Name)" -Log
                            Move-Item -LiteralPath $global:OriginalFile.FullName -Destination $global:TrashEnabled -Force
                            Update-VideoArray -VideoFile $OriginalFile -Status "Moved" -Update
                        }
                        else {
                            LogWrite "Deleting the original file: $($global:OriginalFile.Name)" -Log
                            Get-ChildItem -LiteralPath $global:OriginalFile.FullName -File | Remove-Item -Force -ErrorAction Stop
                            Update-VideoArray -VideoFile $OriginalFile -Status "Deleted" -Update
                        }
                        Write-ScanOutput
                        LogWrite "Renaming Repaired file to Original File" -Log
                        $NewFileName = $global:OriginalFile.BaseName + $global:RepairedFile.Extension
                        Update-VideoArray -VideoFile $VideoFile -Status "Passed:Renamed" -Update
                        Write-ScanOutput
                        Rename-Item -Path $global:RepairedFile.FullName -NewName $NewFileName -ErrorAction Stop
                        $Deleted = $true
                        $global:OriginalRemoved = $true
                    }
                    catch {
                        $ErrorMessage = $_.Exception.Message
                        LogWrite "Error: $ErrorMessage" -Log
                    }
                }
            }
            else {
                LogWrite "ERROR: Error Found in Repaired File. Video Length does not match Original: $FullName" -Log
                LogWrite "Estimate of error location in Original File: $global:RepairLength" -Log
                Update-VideoArray -VideoFile $VideoFile -Status "Failed" -Update
                $OriginalFile = Get-ChildItem -LiteralPath $global:OriginalFile.FullName
                Update-VideoArray -VideoFile $OriginalFile -Status "Failed" -Update
                Write-ScanOutput
                $global:RepairedErrorList.Add($FileName) | Out-Null
                Write-CSV -VidFileName $FileName -VidFilePath $FullName -TestResults "Failed" -Date $Date -VidFileSize $FileSize -VidLength $RepairLength

                if ($global:RemoveAll -or $global:RemoveRepaired) {
                    LogWrite "Deleting Repaired File: $FileName" -Log
                    Update-VideoArray -VideoFile $VideoFile -Status "Deleted" -Update
                    Write-ScanOutput
                    Get-ChildItem -LiteralPath $VideoFile.FullName -File | Remove-Item -Force -ErrorAction Stop
                    $Deleted = $true
                }
            }
            # If the file is not an Auto-Repaired file
        }
        else {
            LogWrite "Scanned Successfully: $FullName" -Log
            Update-VideoArray -VideoFile $VideoFile -Status "Passed" -Update
            Write-ScanOutput
            Write-CSV -VidFileName $FileName -VidFilePath $FullName -TestResults "Passed" -Date $Date -VidFileSize $FileSize -VidLength $OrigLength
        }

        # If the video file is not a re-scanned file, add it to the scan history
        if (!$RescanVideo) {
            #If the video has been deleted, do not log the file name in ScanHistory file for scanning again at a later time.
            if (!($Deleted)) {
                Add-Content $ScanHistory $FullName -Encoding utf8
            }
        }

        # Check to see if the FFmpeg error log was not empty
    }
    elseif ($null -ne (Get-Content $FFmpegLog)) {
        # Get information to log to the CSV file
        $FileSize = "{0:N2}" -f (($VideoFile | Measure-Object -property length -sum ).sum / 1MB)
        $Date = $((Get-Date).ToString('yyyy-MM-dd'))

        if ($AutoRepaired) {
            LogWrite "ERROR: Error found in Repaired File: $FullName" -Log
            Update-VideoArray -VideoFile $VideoFile -Status "Failed" -Update
            Write-ScanOutput
            Write-CSV -VidFileName $FileName -VidFilePath $FullName -TestResults "Failed" -Date $Date -VidFileSize $FileSize -VidLength $RepairLength
            $global:RepairedErrorList.Add($FileName) | Out-Null

            if ($global:RemoveAll -or $global:RemoveRepaired) {
                LogWrite "Deleting Repaired File: $FileName" -Log
                Update-VideoArray -VideoFile $VideoFile -Status "Deleted" -Update
                Write-ScanOutput
                Get-ChildItem -LiteralPath $VideoFile.FullName -File | Remove-Item -Force -ErrorAction Stop
                $Deleted = $true
            }
        }
        else {
            LogWrite "ERROR: Error found: $FullName" -Log
            Update-VideoArray -VideoFile $VideoFile -Status "Failed" -Update
            Write-ScanOutput
            $global:ErrorList.Add($FileName) | Out-Null
            Write-CSV -VidFileName $FileName -VidFilePath $FullName -TestResults "Failed" -Date $Date -VidFileSize $FileSize -VidLength $OrigLength
        }

        $FileName = $FileName -replace '[][]', ''
        $FileName = $FileName + "_error.log"
        $ErrorLog = Join-Path -Path $LogPath  -ChildPath $FileName

        if (Test-Path $ErrorLog) {
            LogWrite "Removing Error Log : $ErrorLog" -Log
            Remove-Item -Path $ErrorLog
        }

        try {
            Rename-Item $FFmpegLog $ErrorLog
        }
        catch {
            $ErrorMessage = $_.Exception.Message
            LogWrite "ERROR: Failed to rename the FFmpeg log: $ErrorMessage" -Log
            LogWrite $ErrorMessage -Log
            $ErrorLog = Join-Path -Path $LogPath -ChildPath "GenericError.log"
            Get-Content $FFmpegLog | Add-Content $ErrorLog -Encoding utf8
            Remove-item $FFmpegLog
        }

        # Only continue if the file is not already flagged as auto-repaired
        if (!$AutoRepaired) {
            # Only continue if auto-repair is selected
            if ($global:AutoRepair) {
                Update-VideoArray -VideoFile $VideoFile -Status "Repairing" -Update
                Write-ScanOutput -Repairing
                LogWrite "Attempting to Repair : $VideoFile" -Log
                # Supply the video object to the repair function
                RepairFile $VideoFile
            }
        }

        if ($global:RemoveAll -or $global:TrashEnabled) {
            # Remove the original file even if the repair was not successful
            if (!($AutoRepaired)) {
                if ($global:OriginalRemoved -eq $false) {
                    if ($global:TrashEnabled) {
                        LogWrite "Moving the original file: $($VideoFile.Name)" -Log
                        Move-Item -LiteralPath $VideoFile.FullName -Destination $global:TrashEnabled -Force
                        Update-VideoArray -VideoFile $VideoFile -Status "Moved" -Update
                        Write-ScanOutput
                        $Deleted = $true
                    }
                    else {
                        LogWrite "Deleting the original file: $($VideoFile.Name)" -Log
                        Update-VideoArray -VideoFile $VideoFile -Status "Deleted" -Update
                        Write-ScanOutput
                        Get-ChildItem -LiteralPath $VideoFile.FullName -File | Remove-Item -Force -ErrorAction Stop
                        $Deleted = $true
                    }
                }
                $global:OriginalRemoved = $false
            }
        }

        # If the video file is not a re-scanned file, add it to the scan history
        if (!$RescanVideo) {
            #If the video has been deleted, do not log the file name in ScanHistory file for scanning again at a later time.
            if (!($Deleted)) {
                # Get the updated Original file name if the file was repaired
                $NewFileName = Join-Path -Path $global:OriginalFile.DirectoryName -ChildPath $global:OriginalFile.BaseName
                $NewFileName = $NewFileName + $global:RepairedFile.Extension
                try {
                    $NewFileObject = Get-ChildItem -LiteralPath $NewFileName -File -ErrorAction Stop
                }
                catch {
                    # If no matches are found, do nothing
                }
                if ($NewFileObject) {
                    Add-Content $ScanHistory $NewFileObject.FullName -Encoding utf8
                }
                else {
                    Add-Content $ScanHistory $FullName -Encoding utf8
                }
            }
        }
    }
}

function GetLength {
    param ([Object]$VideoFile)

    $LengthColumn = 27
    $ObjShell = New-Object -ComObject Shell.Application
    $ObjFolder = $ObjShell.Namespace($VideoFile.DirectoryName)
    $ObjFile = $ObjFolder.ParseName($VideoFile.Name)
    $VideoLength = $ObjFolder.GetDetailsOf($ObjFile, $LengthColumn)

    return $VideoLength
}

function Write-CSV {
    param (
        [String]$VidFileName,
        [String]$VidFilePath,
        [String]$TestResults,
        [String]$Date,
        [String]$VidFileSize,
        [String]$VidLength
    )

    $VidFileName = $VidFileName -replace ',', ''
    $VidFilePath = $VidFilePath -replace ',', ''
    $VidFileSize = $VidFileSize -replace ',', ''

    $CSVContent = "$VidFileName,$TestResults,$Date,$VidFileSize,$VidLength,$VidFilePath"
    try {
        Add-Content $CSVFile -Value $CSVContent -Encoding utf8
    }
    catch {
        $ErrorMessage = $_.Exception.Message
        LogWrite "ERROR: Failed to log the result to the CSV file $CSVFile" -Color Red
        LogWrite $ErrorMessage -Log
    }
}

function Get-YesNoResponse {
    param (
        [String]$Message
    )
    $MenuChoice = "n"
    Write-Host $Message -ForegroundColor Cyan
    while ($MenuChoice -ne "y") {
        Write-Host "Choice [Default: n]: " -ForegroundColor Cyan -NoNewline
        $MenuChoice = Read-Host
        if ([string]::IsNullOrEmpty($MenuChoice)) {
            $MenuChoice = "n"
        }
        if ($MenuChoice -eq "n") {
            return $false
        }
        elseif ($MenuChoice -eq "y") {
            return $true
        }
    }
}

function Get-MenuResponse {
    param (
        [String]$Message,
        [String]$Options
    )
    $MenuChoice = 0
    Write-Host $Message -ForegroundColor Cyan
    $OptionArray = $Options.Split(";")
    $OptionNumber = 1
    foreach ($Option in $OptionArray) {
        Write-Host " $($OptionNumber): $Option"
        $OptionNumber++
    }
    while ($MenuChoice -lt 1 -or $MenuChoice -gt ($OptionNumber - 1)) {
        Write-Host "Choice Number: " -ForegroundColor Cyan -NoNewline
        $MenuChoice = Read-Host
    }
    return $MenuChoice
}


function Show-Menu {
    $MenuChoice = 0
    Write-Host ""
    Write-Host "-------------------------------------------------------" -ForegroundColor Yellow
    Write-Host "                     Main Menu" -ForegroundColor Yellow
    Write-Host "-------------------------------------------------------" -ForegroundColor Yellow
    Write-Host " 1:  Quick Scan"
    Write-Host " 2:  Default Scan"
    Write-Host " 3:  Background Scan"
    Write-Host " 4:  Create a Configuration file for automated scanning"
    Write-Host " 5:  FFmpeg Utilities"
    Write-Host " 6:  Help Menu"
    Write-Host " 7:  Quit Scanning"
    Write-Host ""
    Write-Host "Please make a selection from the Menu Choices." -ForegroundColor Cyan

    while ($MenuChoice -lt 1 -or $MenuChoice -gt 7) {
        Write-Host "Choice Number: " -ForegroundColor Cyan -NoNewline
        $MenuChoice = Read-Host
    }

    switch ($MenuChoice) {
        1 {
            Get-MediaPath -ScanType "Quick"
            Select-ScanOptions -ScanType "Quick"
        }
        2 {
            Get-MediaPath -ScanType "Default"
            Select-ScanOptions -ScanType "Default"
        }
        3 {
            Get-MediaPath -ScanType "Background"
            Select-ScanOptions -ScanType "Background"
        }
        4 {
            New-Configuration
        }
        5 {
            Show-FFmpegUtilities
        }
        6 {
            Show-Help
        }
        7 {
            exit
        }
    }
}

function Get-OnlineVersion {
    # Update Check function
    try {
        $WebResponse = Invoke-WebRequest "https://gist.githubusercontent.com/Desani/129be27da7d735d7c75192ec1aa96c65/raw/ScanMedia.ps1" -UseBasicParsing
        foreach ($Line in $WebResponse.Content.Split([Environment]::NewLine)) {
            $Line -match '(?:^|\W)ScriptVersion = [0-9].[0-9](?:$|\W)' | Out-Null
            if (!([String]::IsNullOrEmpty($Matches.0))) {
                $VersionString = $Matches.0
            }
        }
        $Pos = $VersionString.IndexOf("=")
        [float]$OnlineVersion = $VersionString.Substring($Pos + 1)
        if ($OnlineVersion -gt $ScriptVersion) {
            Write-Host ""
            Write-HostCenter "*****************************************************************************" -Color Green
            Write-HostCenter "A new version is available for download. New Version: $OnlineVersion" -Color Green
            Write-HostCenter "Please update your script to the latest version at your earliest convenience" -Color Green
            Write-HostCenter "*****************************************************************************" -Color Green
            Write-Host ""
        }
    }
    catch {
        $ErrorMessage = $_.Exception.Message
        Write-Verbose "There was an issue checking online for new version"
        Write-Verbose $ErrorMessage
    }
}

function Get-MediaPath {
    param (
        [String]$ScanType
    )
    Clear-Host
    Write-Host ""
    Write-Host "-------------------------------------------------------" -ForegroundColor Yellow
    Write-Host "                   $ScanType Scan" -ForegroundColor Yellow
    Write-Host "-------------------------------------------------------" -ForegroundColor Yellow
    Write-Host ""

    if ($global:Path) {
        if (!(Test-Path $global:Path)) {
            $PathDir = "InvalidPath:\"
            Write-Host "The supplied Path folder for scanning files is invalid: $global:Path" -ForegroundColor Red
            while (!(Test-Path -LiteralPath $PathDir)) {
                Write-Host "Please enter the path that you would like to scan media files:" -ForegroundColor Cyan
                Write-Host "Note: This path should be always accessible by the System so that files can be scanned" -ForegroundColor Yellow
                Write-Host "Example: X:\Movies or Z:\Videos\Render.mp4"
                while ($PathDir -eq "InvalidPath:\") {
                    Write-Host "Path: " -ForegroundColor Cyan -NoNewline
                    $PathDir = Read-Host
                }
                if (!(Test-Path -LiteralPath $PathDir)) {
                    Write-Host "Error: The path supplied is invalid."
                    $PathDir = "InvalidPath:\"
                }
            }
            $global:Path = $PathDir
        }
        Write-Host "Using supplied path: $global:Path" -ForegroundColor Cyan
    }
    else {
        $global:Path = "InvalidPath:\"
        while (!(Test-Path -LiteralPath $global:Path)) {
            Write-Host "Please enter the path that you would like to use to scan:" -ForegroundColor Cyan
            Write-Host "Example: X:\Movies or Z:\Videos\Render.mp4"
            while ($global:Path -eq "InvalidPath:\") {
                Write-Host "Path: " -ForegroundColor Cyan -NoNewline
                $global:Path = Read-Host
            }
            if (!(Test-Path -LiteralPath $global:Path)) {
                Write-Host "Error: The path supplied is invalid."
                $global:Path = "InvalidPath:\"
            }
        }
    }

    if ($global:TrashEnabled) {
        if (!(Test-Path $global:TrashEnabled)) {
            $TrashDir = "InvalidPath:\"
            Write-Host "The supplied Trash folder for corrupt video files is invalid: $global:TrashEnabled" -ForegroundColor Red
            while (!(Test-Path -LiteralPath $TrashDir)) {
                Write-Host "Please enter the path that you would like to store videos with errors:" -ForegroundColor Cyan
                Write-Host "Note: This path should be always accessible by the System so that files can be moved" -ForegroundColor Yellow
                Write-Host "Example: C:\BadVideos or D:\Media\Temp"
                while ($TrashDir -eq "InvalidPath:\") {
                    Write-Host "Path: " -ForegroundColor Cyan -NoNewline
                    $TrashDir = Read-Host
                }
                if (!(Test-Path -LiteralPath $TrashDir)) {
                    Write-Host "Error: The path supplied is invalid."
                    $TrashDir = "InvalidPath:\"
                }
                if ($TrashDir.Contains("[") -or $TrashDir.Contains("]")) {
                    Write-Host "Error: The path supplied is contains the characters [ or ]. Please supply a path that does not contain square brackets."
                    $TrashDir = "InvalidPath:\"
                }
            }
            $global:TrashEnabled = $TrashDir
        }
        Write-Host "Using the Trash Directory: $global:TrashEnabled"
    }
}

function Select-ScanOptions {
    param (
        [String]$ScanType
    )

    switch ($ScanType) {
        { $_ -match "Quick" } {
            $global:Rescan = $true
        }
        { $_ -match "Default" } {
            if (!($global:Rescan)) {
                $global:Rescan = Get-YesNoResponse "Would you like to re-scan files that have already been scanned? (y/n)"
            }

            if (!($global:LimitCPU)) {
                $global:LimitCPU = Get-YesNoResponse "Would you like to Limit FFmpeg CPU's core usage? (y/n)"
            }

            if (!($global:AutoRepair)) {
                $global:AutoRepair = Get-YesNoResponse "Would you like to attempt to auto-repair damaged files? (y/n)"
            }

            if ((!($global:RemoveAll)) -and (!($global:RemoveRepaired)) -and (!($global:RemoveOriginal))) {
                $MenuChoice = Get-YesNoResponse "Would you like to delete failed files? (y/n)"
                if ($MenuChoice -eq "y") {
                    $Choice = Get-MenuResponse -Message "What failed files would you like to remove?" -Options "Failed Repaired Files;Failed Original Files;All Failed Files;No Deletions"
                    switch ($Choice) {
                        1 {
                            $global:RemoveRepaired = $true 
                        }
                        2 {
                            $global:RemoveOriginal = $true 
                        }
                        3 {
                            $global:RemoveAll = $true 
                        }
                        4 {  
                        }
                    }
                }
            }
            if (!($global:TrashEnabled)) {
                $TrashEnabled = Get-YesNoResponse "Would you like to enabled the trash folder and send files with errors to this folder? (y/n)"
                if ($TrashEnabled) {
                    $TrashDir = "InvalidPath:\"
                    while (!(Test-Path -LiteralPath $TrashDir)) {
                        Write-Host "Please enter the path that you would like to store videos with errors:" -ForegroundColor Cyan
                        Write-Host "Note: This path should be always accessible by the System so that files can be moved" -ForegroundColor Yellow
                        Write-Host "Example: C:\BadVideos or D:\Media\Temp"
                        while ($TrashDir -eq "InvalidPath:\") {
                            Write-Host "Path: " -ForegroundColor Cyan -NoNewline
                            $TrashDir = Read-Host
                        }
                        if (!(Test-Path -LiteralPath $TrashDir)) {
                            Write-Host "Error: The path supplied is invalid."
                            $TrashDir = "InvalidPath:\"
                        }
                        if ($TrashDir.Contains("[") -or $TrashDir.Contains("]")) {
                            Write-Host "Error: The path supplied is contains the characters [ or ]. Please supply a path that does not contain square brackets."
                            $TrashDir = "InvalidPath:\"
                        }
                    }
                    $global:TrashEnabled = $TrashDir
                }
            }
        }
        { $_ -match "Background" } {
            # Auto Limit CPU usage for a background scan
            $global:LimitCPU = $true

            if (!($global:IdleScan)) {
                $global:IdleScan = Get-YesNoResponse "Would you like to limit scanning to only when the CPU is not being used by other processes? (y/n)"
            }

            if (!($global:Rescan)) {
                $global:Rescan = Get-YesNoResponse "Would you like to re-scan files that have already been scanned? (y/n)"
            }
            if (!($global:AutoRepair)) {
                $global:AutoRepair = Get-YesNoResponse "Would you like to attempt to auto-repair damaged files? (y/n)"
            }
            if ((!($global:RemoveAll)) -and (!($global:RemoveRepaired)) -and (!($global:RemoveOriginal))) {
                $MenuChoice = Get-YesNoResponse "Would you like to delete failed files? (y/n)"
                if ($MenuChoice -eq "y") {
                    $Choice = Get-MenuResponse -Message "What failed files would you like to remove?" -Options "Failed Repaired Files;Failed Original Files;All Failed Files;No Deletions"
                    switch ($Choice) {
                        1 {
                            $global:RemoveRepaired = $true 
                        }
                        2 {
                            $global:RemoveOriginal = $true 
                        }
                        3 {
                            $global:RemoveAll = $true 
                        }
                        4 {  
                        }
                    }
                }
            }
            if (!($global:ContinuousScan)) {
                $global:ContinuousScan = Get-YesNoResponse "Would you like to setup the scan to run continuously in the background? (y/n)"
            }
            if (!($global:TrashEnabled)) {
                $TrashEnabled = Get-YesNoResponse "Would you like to enabled the trash folder and send files with errors to this folder? (y/n)"
                if ($TrashEnabled) {
                    $TrashDir = "InvalidPath:\"
                    while (!(Test-Path -LiteralPath $TrashDir)) {
                        Write-Host "Please enter the path that you would like to store videos with errors:" -ForegroundColor Cyan
                        Write-Host "Note: This path should be always accessible by the System so that files can be moved" -ForegroundColor Yellow
                        Write-Host "Example: C:\BadVideos or D:\Media\Temp"
                        while ($TrashDir -eq "InvalidPath:\") {
                            Write-Host "Path: " -ForegroundColor Cyan -NoNewline
                            $TrashDir = Read-Host
                        }
                        if (!(Test-Path -LiteralPath $TrashDir)) {
                            Write-Host "Error: The path supplied is invalid."
                            $TrashDir = "InvalidPath:\"
                        }
                        if ($TrashDir.Contains("[") -or $TrashDir.Contains("]")) {
                            Write-Host "Error: The path supplied is contains the characters [ or ]. Please supply a path that does not contain square brackets."
                            $TrashDir = "InvalidPath:\"
                        }
                    }
                    $global:TrashEnabled = $TrashDir
                }
            }
        }
    }
    New-MediaScan
}

function New-Configuration {

    $ConfigList = New-Object System.Collections.ArrayList

    Clear-Host
    Write-Host ""
    Write-HostCenter "-------------------------------------------------------" -Color Yellow
    Write-HostCenter "Write a new Configuration" -Color Yellow
    Write-HostCenter "-------------------------------------------------------" -Color Yellow
    Write-Host ""
    Write-Host (Write-WrapString "In this section you will be given a series of questions that will be used to generate a configuration JSON file. This file can be used with the -ConfigFile parameter to setup automated scanning.")

    $Path = "InvalidPath:\"
    while (!(Test-Path -LiteralPath $Path)) {
        Write-Host "Please enter the path that you would like to use to scan:" -ForegroundColor Cyan
        Write-Host "Example: X:\Movies or Z:\Videos\Render.mp4"
        while ($Path -eq "InvalidPath:\") {
            Write-Host "Path: " -ForegroundColor Cyan -NoNewline
            $Path = Read-Host
        }
        if (!(Test-Path -LiteralPath $Path)) {
            Write-Host "Error: The path supplied is invalid."
            $Path = "InvalidPath:\"
        }
    }
    $ConfigItem = New-Object PSObject -Property @{Name = "Path"; Value = $Path }
    $ConfigList.Add($ConfigItem) | Out-Null

    $LogDir = "InvalidPath:\"
    while (!(Test-Path -LiteralPath $LogDir)) {
        Write-Host "Please enter the path that you would like to store the log files:" -ForegroundColor Cyan
        Write-Host "Note: This path should be always accessible by the System so that errors can be logged" -ForegroundColor Yellow
        Write-Host "Example: C:\ScanLogs or D:\Media\ScanLogs"
        while ($LogDir -eq "InvalidPath:\") {
            Write-Host "Path: " -ForegroundColor Cyan -NoNewline
            $LogDir = Read-Host
        }
        if (!(Test-Path -LiteralPath $LogDir)) {
            Write-Host "Error: The path supplied is invalid."
            $LogDir = "InvalidPath:\"
        }
        if ($LogDir.Contains("[") -or $LogDir.Contains("]")) {
            Write-Host "Error: The path supplied is contains the characters [ or ]. Please supply a path that does not contain square brackets."
            $LogDir = "InvalidPath:\"
        }
    }
    $ConfigItem = New-Object PSObject -Property @{Name = "LogDir"; Value = $LogDir }
    $ConfigList.Add($ConfigItem) | Out-Null

    $IdleScan = Get-YesNoResponse "Would you like to limit scanning to only when the CPU is not being used by other processes? (y/n)"
    $ConfigItem = New-Object PSObject -Property @{Name = "IdleScan"; Value = $IdleScan }
    $ConfigList.Add($ConfigItem) | Out-Null

    $LimitCPU = Get-YesNoResponse "Would you like to Limit FFmpeg CPU's core usage? (y/n)"
    $ConfigItem = New-Object PSObject -Property @{Name = "LimitCPU"; Value = $LimitCPU }
    $ConfigList.Add($ConfigItem) | Out-Null

    $Rescan = Get-YesNoResponse "Would you like to re-scan files that have already been scanned? (y/n)"
    $ConfigItem = New-Object PSObject -Property @{Name = "Rescan"; Value = $Rescan }
    $ConfigList.Add($ConfigItem) | Out-Null

    $AutoRepair = Get-YesNoResponse "Would you like to attempt to auto-repair damaged files? (y/n)"
    $ConfigItem = New-Object PSObject -Property @{Name = "AutoRepair"; Value = $AutoRepair }
    $ConfigList.Add($ConfigItem) | Out-Null
    if ($AutoRepair) {
        $CRF = $null
        Write-Host "What CRF value would you like to use for Repaired Video Encodes?" -ForegroundColor Cyan
        Write-Host "Must be a value between 0 and 51"
        while (($CRF -lt 0) -or ($CRF -gt 51)) {
            Write-Host "Choice Number [Default: 21]: " -ForegroundColor Cyan -NoNewline
            $CRF = Read-Host
            if ([string]::IsNullOrEmpty($CRF)) {
                $CRF = 21
            }
        }
    }
    else {
        $CRF = 21
    }
    $ConfigItem = New-Object PSObject -Property @{Name = "CRF"; Value = $CRF }
    $ConfigList.Add($ConfigItem) | Out-Null

    $Delete = Get-YesNoResponse "Would you like to delete failed files? (y/n)"
    if ($Delete -eq "y") {
        $Choice = Get-MenuResponse -Message "What failed files would you like to remove?" -Options "Failed Repaired Files;Failed Original Files;All Failed Files;No Deletions"
        switch ($Choice) {
            1 {
                $RemoveRepaired = $true 
            }
            2 {
                $RemoveOriginal = $true 
            }
            3 {
                $RemoveAll = $true 
            }
            4 {  
            }
        }
    }
    else {
        $RemoveRepaired = $false
        $RemoveOriginal = $false
        $RemoveAll = $false
    }
    if ($RemoveRepaired -or $RemoveAll -or $RemoveOriginal) {

        $Acceptance = ""

        Write-Host "************************* WARNING *******************************" -ForegroundColor Red
        Write-Host "The commands supplied have the ability to delete original files" -ForegroundColor Yellow
        Write-Host "A backup is highly recommended when supplying these commands" -ForegroundColor Yellow
        Write-Host "Please type the following confirm you understand: IACCEPT" -ForegroundColor Yellow
        Write-Host "************************* WARNING *******************************" -ForegroundColor Red

        while ($Acceptance -ne "IACCEPT") {
            Write-Host "Please Type the above command: " -ForegroundColor Magenta -NoNewline
            $Acceptance = Read-Host
        }
    }
    $ConfigItem = New-Object PSObject -Property @{Name = "RemoveRepaired"; Value = $RemoveRepaired }
    $ConfigList.Add($ConfigItem) | Out-Null
    $ConfigItem = New-Object PSObject -Property @{Name = "RemoveOriginal"; Value = $RemoveOriginal }
    $ConfigList.Add($ConfigItem) | Out-Null
    $ConfigItem = New-Object PSObject -Property @{Name = "RemoveAll"; Value = $RemoveAll }
    $ConfigList.Add($ConfigItem) | Out-Null

    $ContinuousScan = Get-YesNoResponse "Would you like to setup the scan to run continuously in the background? (y/n)"
    $ConfigItem = New-Object PSObject -Property @{Name = "ContinuousScan"; Value = $ContinuousScan }
    $ConfigList.Add($ConfigItem) | Out-Null

    $TrashEnabled = Get-YesNoResponse "Would you like to enabled the trash folder and send files with errors to this folder? (y/n)"
    if ($TrashEnabled) {
        $TrashDir = "InvalidPath:\"
        while (!(Test-Path -LiteralPath $TrashDir)) {
            Write-Host "Please enter the path that you would like to store videos with errors:" -ForegroundColor Cyan
            Write-Host "Note: This path should be always accessible by the System so that files can be moved" -ForegroundColor Yellow
            Write-Host "Example: C:\BadVideos or D:\Media\Temp"
            while ($TrashDir -eq "InvalidPath:\") {
                Write-Host "Path: " -ForegroundColor Cyan -NoNewline
                $TrashDir = Read-Host
            }
            if (!(Test-Path -LiteralPath $TrashDir)) {
                Write-Host "Error: The path supplied is invalid."
                $TrashDir = "InvalidPath:\"
            }
            if ($TrashDir.Contains("[") -or $TrashDir.Contains("]")) {
                Write-Host "Error: The path supplied is contains the characters [ or ]. Please supply a path that does not contain square brackets."
                $TrashDir = "InvalidPath:\"
            }
        }
        $ConfigItem = New-Object PSObject -Property @{Name = "TrashEnabled"; Value = $TrashDir }
        $ConfigList.Add($ConfigItem) | Out-Null
    }

    Write-Host "Generating Configuration INI file"
    try {
        $ConfigList | ConvertTo-Json | Set-Content $LogDir\ScanConfig-$Date.json -Force -ErrorAction Stop
        $JSONPath = Join-Path -Path $LogDir -ChildPath "ScanConfig-$Date.json"
        Write-Host "Configuration Successfully Saved in: $JSONPath"
        Write-Host "Please test the new configuration out by using the command: .\ScanMedia.ps1 -ConfigFile $JSONPath"
    }
    catch {
        $ErrorMessage = $_.Exception.Message
        Write-Verbose "Error. There was an issue generating the JSON file."
        Write-Verbose $ErrorMessage
    }
}

function Show-FFmpegUtilities {
    Clear-Host
    Write-Host ""
    Write-HostCenter "-------------------------------------------------------" -Color Yellow
    Write-HostCenter "FFmpeg Utilities" -Color Yellow
    Write-HostCenter "-------------------------------------------------------" -Color Yellow
    Write-Host ""
    Write-Host "ScanMedia Script utilizes FFmpeg to scan each video. It will need to be accessible by the script by adding the location to the Environmental Variable PATH or by placing ffmpeg.exe in the same directory as the script"
    Write-Host "This menu can be used to attempt to download the newest version essentials edition and unpack it for use with this script"
    Write-Host ""
    Write-Host " 1:  Check ffmpeg.exe access from script"
    Write-Host " 2:  Download latest version of ffmpeg-release-essentials.zip"
    Write-Host " 3:  Main Menu"
    Write-Host ""
    Write-Host "Please make a selection from the Menu Choices." -ForegroundColor Cyan

    $MenuChoice = 0
    while ($MenuChoice -lt 1 -or $MenuChoice -gt 3) {
        Write-Host "Choice Number: " -ForegroundColor Cyan -NoNewline
        $MenuChoice = Read-Host
    }

    switch ($MenuChoice) {
        1 {
            if ($null -eq (Get-Command "ffmpeg.exe" -ErrorAction SilentlyContinue)) {
                Write-Host "ERROR: Unable to find ffmpeg.exe on the computer or in the local script directory" -ForegroundColor Red
                Write-Host "Please download FFmpeg and place ffmpeg.exe in: $ScriptPath or use the downloader in the FFmpeg Utilities Menu" -ForegroundColor Cyan
                Write-PauseMessage "Press a key to continue..."
                Show-FFmpegUtilities
            }
            else {
                Write-PauseMessage "Success! ffmpeg.exe is usable by this script. Press a key to continue..."
                Show-FFmpegUtilities
            }
        }
        2 {
            $Url = 'https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip'
            Write-Host "Attempting to download ffmpeg-release-essentials.zip from $Url"
            try {
                $ZipFile = Join-Path -Path $ScriptPath -ChildPath "ffmpeg-release-essentials.zip"
                if (Test-Path $ZipFile) {
                    Write-Host "Removing previous ffmpeg-release-essentials.zip download"
                    Remove-Item -Path $ZipFile -Force -ErrorAction Stop
                }
                Invoke-WebRequest -Uri $Url -OutFile $ZipFile -UseBasicParsing -ErrorAction Stop

                $Destination = Join-Path -Path $ScriptPath -ChildPath "FFmpeg"
                if (Test-Path $Destination) {
                    Write-Host "Removing previous un-packed zip folder"
                    Remove-Item -Path $Destination -Force -Recurse -ErrorAction Stop
                }
                Write-Host "Unpacking Zip File..."
                Expand-Archive -LiteralPath $ZipFile -DestinationPath $Destination -ErrorAction Stop

                $ExeFile = Join-Path -Path $ScriptPath -ChildPath "ffmpeg.exe"
                if (Test-Path $ExeFile) {
                    Write-Host "Removing previous ffmpeg.exe executable"
                    Remove-Item -Path $ExeFile -Force -ErrorAction Stop
                }
                Write-Host "Copying ffmpeg.exe to $ScriptPath"
                $FFmpegFolder = Join-Path -Path $Destination -ChildPath "ffmpeg*essentials_build"
                $GetFolder = Get-Item -Path $FFmpegFolder

                $FFmpegLocation = Join-Path -Path $GetFolder.FullName -ChildPath "\bin\ffmpeg.exe"
                Copy-Item -Path $FFmpegLocation -Destination $ExeFile

                Write-Host "Latest version of FFmpeg executable successfully downloaded." -ForegroundColor Green

            }
            catch {
                $ErrorMessage = $_.Exception.Message
                Write-Verbose $ErrorMessage
                Write-Host "There was an error while attempting to download and unzip the zip file. Please manually download the file and place ffmpeg.exe in $ScriptPath"
            }

            Write-PauseMessage "Press a key to continue..."
            Show-FFmpegUtilities
        }
        3 {  
        }
    }

    Clear-Host
    Get-OnlineVersion
    Show-Menu
}

function Show-Help {

    Clear-Host
    Write-Host ""
    Write-HostCenter "-------------------------------------------------------" -Color Yellow
    Write-HostCenter "Help Menu" -Color Yellow
    Write-HostCenter "-------------------------------------------------------" -Color Yellow
    Write-Host ""
    Write-HostCenter "Welcome to the Scan Media help screen."
    Write-Host ""
    Write-Host (Write-WrapString "This script utilizes FFmpeg, the same tool Plex uses, to decode the video stream and captures the output for any errors during playback and sends the playback errors to a log file. So essentially it plays the video in the background faster than regular speed. It then checks the error output log file to see if there is anything inside. If FFmpeg was able to cleanly play the file, it counts as a passed file. If there is any error output, an error could be anything from a container issue, a missed frame issue, media corruption or more, it counts the file as failed. So if there would be an issue with playback and a video freezing, it would be caught by this method of checking for errors. Because of the nature of the error log, any errors that show up, even simple ones, will all count as a fail and the output is captured so you can view the error log. Some simple errors are easy to fix so I have included an auto-repair feature which attempts to re-encode the file which is able to correct some issues that would cause problems during playback. It can attempt a repair and delete the original if the new file scans successfully, as an option. " )
    Write-Host ""
    Write-Host (Write-WrapString "This script can be used to scan on or more video files by targeting a single file or a directory that contains multiple files or folders. While running any powershell script, you can stop it at any time by pushing Ctrl + C.")
    Write-Host ""
    Write-Host "You can get further explanations with the following help menus:" -ForegroundColor Yellow
    Write-Host " 1:  Main Menu Help"
    Write-Host " 2:  Command line Parameter and Script Options Help"
    Write-Host " 3:  Main Menu"
    Write-Host ""
    Write-Host "Please make a selection from the Menu Choices." -ForegroundColor Cyan

    $MenuChoice = 0
    while ($MenuChoice -lt 1 -or $MenuChoice -gt 3) {
        Write-Host "Choice Number: " -ForegroundColor Cyan -NoNewline
        $MenuChoice = Read-Host
    }

    switch ($MenuChoice) {
        1 {
            Clear-Host
            Write-Host ""
            Write-HostCenter "-------------------------------------------------------" -Color Yellow
            Write-HostCenter "Main Menu Help" -Color Yellow
            Write-HostCenter "-------------------------------------------------------" -Color Yellow
            Write-Host ""
            Write-Host (Write-WrapString "The main menu consists of three scan options, the ability to generate a configuration file and FFmpeg utilities. The three scan options are designed to allow you to quickly get started scanning without the use of command line or configurations. Command line arguments or a configuration file will allow you have more control over scanning and bypass the menu completely." )
            Write-Host ""
            Write-Host "Here are the menu options and a description of their use:"
            $HelpItems = New-Object System.Collections.ArrayList
            $HelpItemBlank = New-Object PSObject -Property @{MenuItem = ""; HelpText = "" }
            $HelpItem = New-Object PSObject -Property @{MenuItem = "Quick Scan"; HelpText = "This scan option provides a quick ability to initiate a scan on a directory or file without getting some more advanced options provided with this script. Auto-Rescan is automatically enabled to provided users a fast and easy experience with scanning all of files targeted." }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "Default Scan"; HelpText = "This scan option provides the regular scan ability like Quick Scan but also exposes more options to change the scanner abilities. Unlike Quick Scan where re-scan enabled, you are asked if you would like to enable it. The following options can be configured for this scan: Re-Scan of items already scanned, LimitCPU if enabled limits the amount of CPU usage, Auto-Repair that when enabled attempts to repair files by re-encoding them to correct some errors, deletion options that allow for the deletion of bad repaired files, bad original files, or both, and the TrashEnabled which will allow you to move files found to have an error into a single directory." }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "Background Scan"; HelpText = "This scan option is designed to allow for a long scan duration or a continuous scan without massively interrupting other resources on the same machine. A CPU limit is automatically enabled for this scan. The following options can be configured for this scan: Idle Scan prevents the scanner from running if CPU usage on the machine is above 25%, Re-Scan of items already scanned, deletion options that allow for the deletion of bad repaired files, bad original files, or both, Continuous scan which will automatically start another scan in 15 minutes once a scan has completed, and the TrashEnabled which will allow you to move files found to have an error into a single directory." }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "Create a Configuration file"; HelpText = "This allows for the creation of a JSON settings file that can be used in conjunction with the command line parameter -ConfigFile. A series of questions will be asked about the type of scan you would like to run and then a file will be saved. If a configuration file is supplied at command line, no menu will be shown. This can be used to setup an automated scanning process with a program like Task Scheduler." }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "FFmpeg Utilities"; HelpText = "The option allows you to verify that the ffmpeg.exe file required by the script is configured correctly. If ffmpeg.exe is not found there is an option to download the latest version and place it into the proper location. Once downloaded you can validate that the script can access the ffmpeg.exe before proceeding to scan." }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null

            $HelpItems | Select-Object -Property MenuItem, HelpText | Format-Table -AutoSize -Wrap -HideTableHeaders

            Write-PauseMessage "Push any key to continue..."
            Show-Help
        }
        2 {
            Clear-Host
            Write-Host ""
            Write-HostCenter "-------------------------------------------------------" -Color Yellow
            Write-HostCenter "Command line Parameter and Script Options Help" -Color Yellow
            Write-HostCenter "-------------------------------------------------------" -Color Yellow
            Write-Host ""
            Write-Host (Write-WrapString "This help section will go over all of the available command line arguments with a description of their functionality and examples. All script functionality can be accessed through the command line and the script menu system can be bypassed allowing for another method for automation." )
            Write-Host "Command line parameters can be used at the same time as calling the script as seen in the example below example:"
            Write-Host ""
            Write-Host '.\ScanMedia.ps1 -Path "C:\Media\Videos" -LimitCPU -AutoRepair -RemoveOriginal -RemoveRepaired -BypassMenu'
            Write-Host ""
            Write-Host (Write-WrapString "This example bypasses the menu and scans the path C:\Media\Videos with lower CPU usage and attempts to repair the files that are found to have an issue. The script will delete repaired files that don't repair properly and it will delete the original files of successfully repaired files.")
            Write-Host ""
            Write-Host "Here is a list of parameters/options with an overview of their use:"
            $HelpItems = New-Object System.Collections.ArrayList
            $HelpItemBlank = New-Object PSObject -Property @{MenuItem = ""; HelpText = "" }
            $HelpItem = New-Object PSObject -Property @{MenuItem = "Path"; HelpText = 'This is the folder or file that the script will begin recursively scanning with FFmpeg. If running this script as administrator, you typically want to provide the full network path instead of a drive letter. Calling Syntax: -Path "C:\Media\Videos" or -Path "\\SEVERVERSHARE\Media\Videos"' }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "AutoRepair"; HelpText = "When supplied, the script will automatically attempt to repair any file that is found to have issues. Only a certain number of issues will be able to be corrected with this method and it will not be possible to repair all files. After a video is repaired, it is then scanned to check to make sure the repair was successful and verifies that the runtime matches that of the original file. Bad auto-repaired files can be automatically removed with the use of -RemovedRepaired. Calling Syntax: -AutoRepair" }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "Rescan"; HelpText = "When supplied, forces the script to ignore the Scan History File and will re-scan of all files in the path supplied. Calling Syntax: -Rescan" }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "ContinuousScan"; HelpText = "When supplied, once a scan completes a new scan will be kicked off in 15 minutes with the same parameters as the last scan. This can be used to setup automatic scanning on a directory for any new files, if previous files are ignored. Calling Syntax: -ContinuousScan" }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "CRF"; HelpText = "Stands for Constant Rate Factor, can be within the range of 0 - 51, where 0 is lossless and 51 is the worst quality possible. 21 is the default value for this script. A lower value generally leads to higher quality, and a subjectively same range is 17 - 28. Consider 17 or 18 to be visually lossless or nearly so; it should look the same or nearly the same as the input but it isn't technically lossless. The range is exponential, so increasing the CRF value +6 results in roughly half the bitrate / file size, while -6 leads to roughly twice the bitrate.  Calling Syntax: -CRF 25" }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "LimitCPU"; HelpText = "When supplied, the script will attempt to run FFmpeg with half of the available cores assigned to the current system. If 4 cores are available, FFmpeg will utilize 2. If 1 core is available to the system, this parameter will have no affect. Scan and repair time will increase with this parameter. Calling Syntax: -LimitCPU" }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "IdleScan"; HelpText = "When supplied, stops a scan from being initiated unless CPU usage is below 25%. This will stop the script from competing for resources if the machine is in use with other tasks. Once the CPU is idle a scan will continue. Calling Syntax: -IdleScan" }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "RemoveRepaired"; HelpText = "When supplied, the script will DELETE all of the repaired files that did not scan as successfully repaired. Calling Syntax: -RemoveRepaired" }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "RemoveOriginal"; HelpText = "When supplied, the script will DELETE the original video file if it was repaired successfully. It will then overwrite the original file name with the name of the successfully repaired file. It will not remove original files unless a repair was attempted and it was successful. All other original files will be un-touched. It is recommended to only run this command if a backup is in place for the media being scanned. If -IAcceptResponsibility is not supplied at run time, the script will prompt the user to type IACCEPT during runtime. Calling Syntax: -RemoveOriginal" }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "RemoveAll"; HelpText = "When supplied, will DELETE all of the files that are found to have errors and is not dependant of if they are able to be successfully repaired. It is recommended to only run this command if a backup is in place for the media being scanned. If -IAcceptResponsibility is not supplied at run time, the script will prompt the user to type IACCEPT during runtime. Calling Syntax: -RemoveAll" }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "IAcceptResponsibility"; HelpText = "When supplied, the user accepts responsibility when supplying parameters that will potentially delete original files. This should only be used when you understand the risks involved. Calling Syntax: -IAcceptResponsibility" }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "ConfigFile"; HelpText = 'When supplied, the script will load the configuration saved in the supplied JSON file and initiate a scan with those values. This will allow you to save a popular scan with the settings you require and then call the script with the configuration file for automatic scanning. This could be used in conjunction with scheduling software to setup an automated method to monitor directories. Calling Syntax: -ConfigFile "C:\ScanFiles\ScanConfig-2020-11-30.json" ' }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "BypassMenu"; HelpText = "When supplied, the starting interactive menu will be bypassed so that no user interaction is required to initiate a scan. If calling the script using command line arguments where no user interaction is wanted, supply this parameter to ensure there is no interruptions when starting a scan. Not required when using the parameter -ConfigFile. The -Path parameter is required for no user interaction. Calling Syntax: -BypassMenu " }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "GPUEncoding"; HelpText = "When supplied, FFmpeg with utilize the GPU to encode files, lowering CPU usage, by changing the encoding library being used to h264_nvenc. Note: This requires both your GPU and the FFmpeg binary to support GPU encoding. This will not work with the default FFmpeg binary file. To utilize this feature you need to find out what is required for GPU encoding through FFmpeg. Calling Syntax: -GPUEncoding " }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "Extension"; HelpText = 'When supplied, will change the extension of files repaired with FFmpeg. To be used in conjunction with -GPUEncoding or else the default of .mp4 will be compatible with the default encoding library. Calling Syntax: -Extension ".mkv" ' }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null
            $HelpItem = New-Object PSObject -Property @{MenuItem = "TrashEnabled"; HelpText = 'When supplied, will move all files found with issues will be moved to the folder specified for remediation. This option will take priority over -RemoveAll or -RemoveOriginal if both are called. Calling Syntax: -TrashEnabled "X:\Media\BadFiles"' }
            $HelpItems.Add($HelpItem) | Out-Null
            $HelpItems.Add($HelpItemBlank) | Out-Null

            $HelpItems | Select-Object -Property MenuItem, HelpText | Format-Table -AutoSize -Wrap -HideTableHeaders

            Write-PauseMessage "Push any key to continue..."
            Show-Help
        }
        3 {

        }
    }

    Clear-Host
    Get-OnlineVersion
    Show-Menu
}

function Update-VideoArray {
    param (
        [String]$Status,
        [Object]$VideoFile,
        [switch]$Update)

    if ($global:VideoList.Length -eq 500) {
        $global:VideoList.RemoveAt(0)
    }
    if ($Update) {
        $MediaFile = New-Object PSObject -Property @{Name = $VideoFile.Name; Path = $VideoFile.DirectoryName; Status = $Status; FullName = $VideoFile.FullName }

        $ScanList = $global:VideoList | Where-Object { ($_.FullName -eq $VideoFile.FullName) }
        if ($ScanList) {
            foreach ($file in $ScanList) {
                $global:VideoList.Remove($file)
            }
        }
        $global:VideoList.Add($MediaFile) | Out-Null
    }
    else {
        $MediaFile = New-Object PSObject -Property @{Name = $VideoFile.Name; Path = $VideoFile.DirectoryName; Status = $Status; FullName = $VideoFile.FullName }
        $global:VideoList.Add($MediaFile) | Out-Null
    }
}

function Write-ScanOutput {
    param (
        [switch]$Scanning,
        [switch]$Repairing
    )

    Clear-Host

    # Check if script is running in PowerShell ISE which does not expose console size
    if ($Host.UI.SupportsVirtualTerminal) {
        $Width = (Get-Host).UI.RawUI.WindowSize.Width
        $Height = (Get-Host).UI.RawUI.WindowSize.Height
    }
    else {
        $Width = (Get-Host).UI.RawUI.BufferSize.Width
        $Height = 50
    }

    # Set minimum values on terminal size
    if ($Width -lt 50) {
        $Width = 120
    }
    if ($Height -lt 25) {
        $Height = 30
    }

    $OutputPath = $global:Path.subString(0, [System.Math]::Min(($Width - 20), $global:Path.Length))

    if ($global:Path.Length -gt ($Width - 20)) {
        $OutputPath = $OutputPath + "..."
    }

    Write-Host ""
    Write-HostCenter "##################################################################################################################" -Color Cyan
    Write-HostCenter "Scanning $OutputPath" -Color Cyan
    Write-HostCenter "##################################################################################################################" -Color Cyan
    Write-Host ""

    if ($global:AutoRepair) {
        Write-HostCenter "Files Scanned: $($global:VideoCount) | Errors Found: $($global:ErrorList.Count) | Repair Errors: $($global:RepairedErrorList.Count)"
    }
    else {
        Write-HostCenter "Files Scanned: $($global:VideoCount) | Errors Found: $($global:ErrorList.Count)"
    }

    $global:VideoList | Select-Object -Property Name, Status, Path | Select-Object -Last ($Height - 13) | Format-Table -AutoSize | Out-String -Stream | ForEach-Object {
        if ($_.Contains(" Skipped ")) {
            Write-Host $_ -ForegroundColor Gray
        }
        elseif ($_.Contains(" Passed ")) {
            Write-Host $_ -ForegroundColor Green
        }
        elseif ($_.Contains(" Failed ")) {
            Write-Host $_ -ForegroundColor Red
        }
        elseif ($_.Contains(" Deleted ")) {
            Write-Host $_ -ForegroundColor Yellow
        }
        elseif ($_.Contains(" Passed:Renamed ")) {
            Write-Host $_ -ForegroundColor Green
        }
        elseif ($_.Contains(" Moved ")) {
            Write-Host $_ -ForegroundColor Yellow
        }
        else {
            Write-Host $_
        }
    }

    if ($Scanning) {
        Write-HostCenter "Please wait, Scan in progress..."
    }
    if ($Repairing) {
        Write-HostCenter "Please wait, Repair in progress..."
    }
}

function Write-WrapString {
    param (
        [String]$Str
    )

    # Check if script is running in PowerShell ISE which does not expose console size
    if ($Host.UI.SupportsVirtualTerminal) {
        $Width = (Get-Host).UI.RawUI.WindowSize.Width
    }
    else {
        $Width = (Get-Host).UI.RawUI.BufferSize.Width
    }

    # Holds the final version of $Str with newlines
    $StrWithNewLines = ""
    # Current line, never contains more than screen width
    $CurLine = ""
    # Loop over the words and write a line out just short of window size
    foreach ($Word in $Str.Split(" ")) {
        # Lets see if adding a word makes our string longer then window width
        $CheckLinePlusWord = $CurLine + " " + $Word
        if ($CheckLinePlusWord.length -gt $Width) {
            # With the new word we've gone over width
            # append newline before we append new word
            $StrWithNewLines += [Environment]::Newline
            # Reset current line
            $CurLine = ""
        }
        # Append word to current line and final str
        $CurLine += $Word + " "
        $StrWithNewLines += $Word + " "
    }
    # return our word wrapped string
    return $StrWithNewLines
}

function New-MediaScan {
    # Test to see if the log directory exists and create it if not
    if (!(Test-Path $LogDir)) {
        New-Item -ItemType Directory -Force -Path $LogDir | Out-Null
    }

    if (!(Test-Path $LogPath)) {
        New-Item -ItemType Directory -Force -Path $LogPath | Out-Null
    }

    if ($global:RemoveAll -or $global:RemoveOriginal) {
        if (!($global:IAcceptResponsibility)) {
            $Acceptance = ""

            LogWrite "************************* WARNING *******************************" -Color Red
            LogWrite "The commands supplied have the ability to delete original files" -Color Yellow
            LogWrite "A backup is highly recommended when supplying these commands" -Color Yellow
            LogWrite "Please type the following confirm you understand: IACCEPT" -Color Yellow
            LogWrite "************************* WARNING *******************************" -Color Red

            while ($Acceptance -ne "IACCEPT") {
                Write-Host "Please Type the above command: " -ForegroundColor Magenta -NoNewline
                $Acceptance = Read-Host
            }
        }
    }

    LogWrite "Starting script $($MyInvocation.MyCommand.Name)" -Log

    # Test the CSV file path and create the CSV with the header line if it does not exist
    if (!(Test-Path $CSVFile)) {
        Set-Content $CSVFile -Value "File Name,FFmpeg Test,Check Date,File Size (MB),Video Length,Location"
    }

    if (!(Test-Path $ScanHistory)) {
        Set-Content $ScanHistory -Value "This is a history of all scanned items. Please do not delete or modify this file."
        LogWrite "Creating a history file: $ScanHistory "
        LogWrite "Please do not remove this file if you would like to keep a history of scanned items."
    }

    if ($global:AutoRepair) {
        LogWrite "Auto-Repair has been enabled" -Color Cyan
    }

    if ($global:Rescan) {
        LogWrite "Media Rescan has been enabled. All files will be scanned." -Color Cyan
    }

    if ($global:RemoveRepaired) {
        LogWrite "WARNING: Auto-Delete has been enabled for repaired files. All repaired files that have errors will be automatically removed" -Color Yellow
    }

    if ($global:RemoveAll) {
        LogWrite "WARNING: Auto-Delete has been enabled for all files. All files that have errors will be automatically removed" -Color Yellow
    }

    if ($global:RemoveOriginal) {
        LogWrite "WARNING: Auto-Delete has been enabled for original files. All original files that have errors will be automatically removed after a repair has completed successfully" -Color Yellow
    }

    if ($global:IAcceptResponsibility) {
        LogWrite "You have acknowledged that original files will potentially be removed when using RemoveAll or RemoveOriginal and supplying -IAcceptResponsibility" -Color Magenta
    }

    if ($global:LimitCPU) {
        LogWrite "Determining the number of CPUs to utilize for lower CPU usage." -Color Cyan
        $global:CPULimit = (Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors / 2
        if ($global:CPULimit -lt 1) {
            $global:CPULimit = 1
        }
        LogWrite "Limiting the CPU usage for FFmpeg to $global:CPULimit core(s)" -Color Cyan
    }

    if ($global:IdleScan) {
        LogWrite "IdleScan has been enabled. A file will only be scanned if CPU usage is below 25 percent" -Color Cyan
    }

    if ($global:GPUEncoding) {
        LogWrite "GPU Encoding has been enabled. This requires that FFmpeg and your GPU both support GPU encoding. This should only be enabled if you have verified functionality." -Color Cyan
        $global:VideoLibrary = "h264_nvenc"
    }

    if ($global:ContinuousScan) {
        LogWrite "Continuous Scanning has been enabled. 15 minutes after a scan is completed a new scan will be started using the same values as the pervious scan" -Color Cyan
    }

    if ($global:TrashEnabled) {
        LogWrite "Trash has been enabled. Video files with errors found will be moved to the folder: $global:TrashEnabled" -Color Cyan
    }

    # Check to see if FFmpeg exists and if it is installed on the local machine and added to path
    if ($null -eq (Get-Command "ffmpeg.exe" -ErrorAction SilentlyContinue)) {
        LogWrite "ERROR: Unable to find ffmpeg.exe on the computer or in the local script directory" -Color Red
        LogWrite "Please download FFmpeg and place ffmpeg.exe in: $ScriptPath or use the downloader in the FFmpeg Utilities Menu" -Color Cyan
        LogWrite "Exiting Script" -Color Red
        exit
    }

    # Check to make sure the path supplied to scan exists for the current user
    if (!(Test-Path -LiteralPath $global:Path)) {
        LogWrite "ERROR: Unable to access the directory: $global:Path" -Color Red
        if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
            LogWrite "Running as administrator is not required and can cause issues accessing network folders that have been mapped. Please supply the direct share path I.E. \\SERVER\SHARE\PATH"
        }
        LogWrite "Exiting Script" -Color Red
        exit
    }

    Write-Verbose "Path: $global:Path"
    Write-Verbose "LogDir: $global:LogDir"
    Write-Verbose "ConfigFile: $global:ConfigFile"
    Write-Verbose "AutoRepair: $global:AutoRepair"
    Write-Verbose "Rescan: $global:Rescan"
    Write-Verbose "CRF: $global:CRF"
    Write-Verbose "LimitCPU: $global:LimitCPU"
    Write-Verbose "CPULimit: $global:CPULimit"
    Write-Verbose "RemoveAll: $global:RemoveAll"
    Write-Verbose "RemoveRepaired: $global:RemoveRepaired"
    Write-Verbose "RemoveOriginal: $global:RemoveOriginal"
    Write-Verbose "IAcceptResponsibility: $global:IAcceptResponsibility"
    Write-Verbose "IdleScan: $global:IdleScan"
    Write-Verbose "GPUEncoding: $global:GPUEncoding"
    Write-Verbose "VideoLibrary: $global:VideoLibrary"
    Write-Verbose "RepairVFileExtension: $global:RepairVFileExtension"
    Write-Verbose "ContinuousScan: $global:ContinuousScan"
    Write-Verbose "TrashEnabled: $global:TrashEnabled"

    if ((!($global:ConfigFile)) -and (!($BypassMenu))) {
        Write-PauseMessage "Ready to begin scan on $global:Path Press any key to continue..."
    }

    Clear-Host
    $ContinuousScan = $true

    while ($ContinuousScan) {

        $global:VideoList = New-Object System.Collections.ArrayList
        $global:VideoList.Capacity = 500
        $global:ErrorList = New-Object System.Collections.ArrayList
        $global:RepairedErrorList = New-Object System.Collections.ArrayList
        $global:VideoCount = 0
        $global:SkipCount = 0

        switch ($true) {
            $IsWindows {
                <# PowerShell 6+
                LogWrite "Running on a Windows Machine with PowerShell version 6+" -Log
            }
            $IsMacOS {
                LogWrite "Running on a Mac OS with PowerShell CORE" -Log
            }
            $IsLinux {
                LogWrite "Running on a Linux OS with PowerShell CORE" -Log
            }
            Default {
                <# Windows PowerShell
                LogWrite "Running on a Windows Machine with PowerShell version below 6" -Log
            }
        }

        $Timer = [system.diagnostics.stopwatch]::StartNew()

        LogWrite "" -Log
        LogWrite "##################################################################################################################" -Log
        LogWrite "Beginning Scan on $global:Path" -Log
        LogWrite "##################################################################################################################" -Log
        LogWrite "" -Log

        Write-ScanOutput

        # Get all the child items of the supplied directory and attempt to scan the files with the function CheckFile
        Get-ChildItem -LiteralPath $global:Path -File -Recurse | Where-Object { $Include -contains $_.Extension } | ForEach-Object {

            if ($global:IdleScan) {
                $CPUAverage = Get-CimInstance Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select-Object Average
                while ($CPUAverage.Average -gt 25) {
                    Write-Verbose "CPU usage above 25 percent. Pausing for 5 minutes"
                    Write-Verbose "Current CPU Usage: $($CPUAverage.Average)"
                    Start-Sleep -Seconds 300
                    $CPUAverage = Get-CimInstance Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select-Object Average
                }
            }

            # Check the scan history file to see if the file has been scanned
            $ScanHistoryCheck = (Get-Content $ScanHistory -Encoding utf8 | Select-String -pattern $_.FullName -SimpleMatch)

            # If the file exists in the scan history and Rescan has not been enabled, skip
            if (($null -ne $ScanHistoryCheck) -and ($global:Rescan -eq $false)) {
                Update-VideoArray -VideoFile $_ -Status "Skipped"
                LogWrite "Skipping scanned file: $($_.Name)" -Log
                $global:SkipCount = $global:SkipCount + 1
                Write-ScanOutput
            }
            # If the file exists in the scan history and Rescan has been  enabled, scan the file
            elseif (($null -ne $ScanHistoryCheck) -and ($global:Rescan -eq $true)) {
                CheckFile $_ -RescanVideo
            }
            else {
                CheckFile $_
            }
        }

        Write-ScanOutput
        $TimeSpan = New-TimeSpan -Seconds $Timer.Elapsed.TotalSeconds
        $Elapsed = '{0:000}h:{1:00}m:{2:00}s' -f $TimeSpan.Hours, $TimeSpan.Minutes, $TimeSpan.Seconds
        $Timer.Stop()
        LogWrite "Number of Files Scanned: $global:VideoCount"
        LogWrite "Elapsed Time: $($Elapsed)"
        LogWrite "Scan Log file: $LogFile"
        if ($global:ErrorList.Count -gt 0) {
            LogWrite "Number of files that were found with an error: $($global:ErrorList.Count)" -Color Yellow
            LogWrite "Printing out filename(s) with errors into the log file"
            foreach ($file in $global:ErrorList) {
                LogWrite "$file" -Log
            }
        }
        if ($global:RepairedErrorList.Count -gt 0) {
            LogWrite "Number of repaired files that were found with an error: $($global:RepairedErrorList.Count)" -Color Yellow
        }
        if ($global:SkipCount -gt 0) {
            LogWrite "Number of files skipped: $($global:SkipCount)"
        }

        if (!($global:ContinuousScan)) {
            $ContinuousScan = $false
            LogWrite "Scan Complete."
            if ((!($BypassMenu)) -or (!($global:ConfigFile))) {
                Write-PauseMessage "Push any key to quit the script..."
            }
        }
        else {
            LogWrite "Starting new scan in 15 minutes"
            Start-Sleep -Seconds 900
        }
    }
}

Clear-Host

if ($ScriptPath.Contains("[") -or $ScriptPath.Contains("]")) {
    Write-Host "ERROR: Invalid Script Path. Due to how PowerShell handles [] characters. Please run the script from a path that does not contain [ or ]." -ForegroundColor Yellow
    Write-Error "ERROR: Invalid Script Path. Due to how PowerShell handles [] characters. Please run the script from a path that does not contain [ or ]."
    exit 1
}

# Change the location of the working directory to where the script is launched from
Set-Location -Path $ScriptPath

if ((!$global:ConfigFile) -and (!$BypassMenu)) {
    Write-Host ""
    Write-Host ""
    Write-HostCenter "##################################################################################################################" -Color Cyan
    Write-HostCenter "Scan Media PowerShell Script" -Color Cyan
    Write-HostCenter "##################################################################################################################" -Color Cyan
    Write-Host ""
    Write-Host ""
    Write-Host "$(Get-Date) Initializing ScanMedia Script"
    Write-Host "$(Get-Date) Script Version: $ScriptVersion"
    Write-Host "$(Get-Date) Program's GitHub: https://gist.github.com/Desani/129be27da7d735d7c75192ec1aa96c65"
    Get-OnlineVersion
    Show-Menu
}
else {
    if ($global:ConfigFile) {
        # Convert JSON Values to the Global Variables
        try {
            $JSON = Get-Content $ConfigFile | Out-String | ConvertFrom-Json
        }
        catch {
            $ErrorMessage = $_.Exception.Message
            Write-Error "Error. There was an issue reading from the JSON file."
            Write-Host $ErrorMessage
            exit 1
        }

        $PathHash = $JSON | Where-Object { ($_.Name -eq "Path") }
        $global:Path = $PathHash.Value

        $LogDirHash = $JSON | Where-Object { ($_.Name -eq "LogDir") }
        $global:LogDir = $LogDirHash.Value

        $LogPath = Join-Path -Path $global:LogDir -ChildPath "Log_$Date"
        $FFmpegLog = Join-Path -Path $LogPath -ChildPath "ffmpegerror.log"
        $LogFile = Join-Path -Path $LogPath -ChildPath "results.log"
        $CSVFile = Join-Path -Path $LogPath -ChildPath "results.csv"

        $IdleScanHash = $JSON | Where-Object { ($_.Name -eq "IdleScan") }
        $global:IdleScan = $IdleScanHash.Value

        $LimitCPUHash = $JSON | Where-Object { ($_.Name -eq "LimitCPU") }
        $global:LimitCPU = $LimitCPUHash.Value

        $RescanHash = $JSON | Where-Object { ($_.Name -eq "Rescan") }
        $global:Rescan = $RescanHash.Value

        $AutoRepairHash = $JSON | Where-Object { ($_.Name -eq "AutoRepair") }
        $global:AutoRepair = $AutoRepairHash.Value

        $CRFHash = $JSON | Where-Object { ($_.Name -eq "CRF") }
        $global:CRF = $CRFHash.Value

        $RemoveRepairedHash = $JSON | Where-Object { ($_.Name -eq "RemoveRepaired") }
        $global:RemoveRepaired = $RemoveRepairedHash.Value

        $RemoveOriginalHash = $JSON | Where-Object { ($_.Name -eq "RemoveOriginal") }
        $global:RemoveOriginal = $RemoveOriginalHash.Value

        $RemoveAllHash = $JSON | Where-Object { ($_.Name -eq "RemoveAll") }
        $global:RemoveAll = $RemoveAllHash.Value

        $ContinuousScanHash = $JSON | Where-Object { ($_.Name -eq "ContinuousScan") }
        $global:ContinuousScan = $ContinuousScanHash.Value

        $TrashEnabledHash = $JSON | Where-Object { ($_.Name -eq "TrashEnabled") }
        if ($TrashEnabledHash) {
            $global:TrashEnabled = $TrashEnabledHash.Value
        }

        $global:IAcceptResponsibility = $true

        New-MediaScan
    }
    elseif ($BypassMenu) {
        Get-MediaPath -ScanType "Custom"
        New-MediaScan
    }
}