Skip to content

Instantly share code, notes, and snippets.

@koloved
Last active May 19, 2025 14:10
Show Gist options
  • Save koloved/dad73f942f92b592763dc7be7308a7b8 to your computer and use it in GitHub Desktop.
Save koloved/dad73f942f92b592763dc7be7308a7b8 to your computer and use it in GitHub Desktop.
ShareX AV1 Avif Auto-Config/Installer Script
<#
.SYNOPSIS
ShareX AV1 Avif Auto-Config/Installer Script
.DESCRIPTION
This script automates the configuration of ShareX with custom FFmpeg video/image conversion tasks.
It will:
1. Install/update FFmpeg
2. Set After capture tasks -> Perform actions option ON
3. Add AVIF conversion Actions
4. Restart ShareX with new configuration
.NOTES
Version: 1.0
Author: koloved
Last Modified: 2025-01-21
#>
# Step 0: Terminate ShareX if running
try {
$shareXProcess = Get-Process -Name "ShareX" -ErrorAction Stop
$shareXProcess | Stop-Process -Force
Write-Host "ShareX process terminated successfully."
}
catch {
Write-Host "ShareX is not currently running."
}
# Define the download URL for FFmpeg
$ffmpegUrl = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-git-full.7z"
# Define paths
$ffmpegDir = "C:\ffmpeg"
$ffmpegBinDir = "$ffmpegDir\bin"
$ffmpegExePath = "$ffmpegBinDir\ffmpeg.exe"
$configPath = "$env:USERPROFILE\Documents\ShareX\ApplicationConfig.json"
$shareXPath = "${env:ProgramFiles}\ShareX\ShareX.exe"
# Step 1: Download FFmpeg
Write-Host "Downloading FFmpeg..."
$tempDir = "$env:TEMP\ffmpeg"
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
$archivePath = "$tempDir\ffmpeg.7z"
Invoke-WebRequest -Uri $ffmpegUrl -OutFile $archivePath
# Step 2: Check for 7-Zip
$7zipPath = "C:\Program Files\7-Zip\7z.exe"
$use7zip = Test-Path $7zipPath
# Step 3: Extract files
Write-Host "Extracting FFmpeg..."
$extractTempDir = "$tempDir\extracted"
New-Item -ItemType Directory -Path $extractTempDir -Force | Out-Null
if ($use7zip) {
& $7zipPath x "-o$extractTempDir" $archivePath -y | Out-Null
}
else {
Expand-Archive -Path $archivePath -DestinationPath $extractTempDir -Force
}
# Step 4: Copy files to target directory
$versionedFolder = Get-ChildItem -Path $extractTempDir -Directory | Select-Object -First 1
$extractedBinPath = Join-Path $versionedFolder.FullName "bin"
if (Test-Path $extractedBinPath) {
New-Item -ItemType Directory -Path $ffmpegBinDir -Force | Out-Null
Copy-Item -Path "$extractedBinPath\*" -Destination $ffmpegBinDir -Recurse -Force
}
else {
Write-Host "FFmpeg bin directory not found in extracted files!"
exit
}
# Step 5: Verify installation
if (-not (Test-Path $ffmpegExePath)) {
Write-Host "FFmpeg installation failed!"
exit
}
Write-Host "FFmpeg installed successfully at $ffmpegExePath"
# Step 6: Update ApplicationConfig.json
if (Test-Path $configPath) {
$config = Get-Content $configPath -Raw | ConvertFrom-Json
}
else {
$config = New-Object PSObject
}
# Ensure DefaultTaskSettings exists
if (-not $config.PSObject.Properties['DefaultTaskSettings']) {
$config | Add-Member -MemberType NoteProperty -Name "DefaultTaskSettings" -Value (New-Object PSObject)
}
# Update AfterCaptureJob
$dtSettings = $config.DefaultTaskSettings
if ($dtSettings.PSObject.Properties['AfterCaptureJob']) {
$jobs = $dtSettings.AfterCaptureJob -split ', ' | Where-Object { $_ }
if (-not $jobs.Contains("PerformActions")) {
$dtSettings.AfterCaptureJob = ($jobs + "PerformActions") -join ", "
}
}
else {
$dtSettings | Add-Member -MemberType NoteProperty -Name "AfterCaptureJob" -Value "CopyImageToClipboard, SaveImageToFile, PerformActions" -Force
}
# Update ExternalPrograms
$customActions = @(
[PSCustomObject]@{
IsActive = $true
Name = "Convert Video to avif"
Path = $ffmpegExePath
Args = '-i "$input" -c:v libsvtav1 -crf 35 -preset 6 -an -y "$output"'
OutputExtension = "avif"
Extensions = "mp4"
HiddenWindow = $true
DeleteInputFile = $true
},
[PSCustomObject]@{
IsActive = $true
Name = "Convert PNG to avif"
Path = $ffmpegExePath
Args = '-i "$input" -c:v libaom-av1 -still-picture 1 -crf 34 -cpu-used 4 "$output"'
OutputExtension = "avif"
Extensions = "png"
HiddenWindow = $true
DeleteInputFile = $true
}
)
if (-not $dtSettings.PSObject.Properties['ExternalPrograms']) {
$dtSettings | Add-Member -MemberType NoteProperty -Name "ExternalPrograms" -Value @()
}
# Remove existing entries and add new ones
$dtSettings.ExternalPrograms = @($dtSettings.ExternalPrograms | Where-Object {
$_.Name -notin @("Convert Video to avif", "Convert PNG to avif")
}) + $customActions
# Save compact JSON
$json = $config | ConvertTo-Json -Depth 10 -Compress
[System.IO.File]::WriteAllText($configPath, $json, [System.Text.Encoding]::UTF8)
Write-Host "Configuration updated successfully."
# Step 7: Restart ShareX
if (Test-Path $shareXPath) {
Start-Process -FilePath $shareXPath
Write-Host "ShareX restarted successfully."
}
else {
Write-Host "ShareX not found at $shareXPath - please launch manually."
}
# Cleanup temporary files
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment