Last active
April 14, 2024 18:33
-
-
Save masterflitzer/2b018fb367ab94a27e10af80b572d5fc to your computer and use it in GitHub Desktop.
Simple script to convert any audio/video to M4A using drag'n'drop on shortcut after first run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Version 5.1 | |
Set-StrictMode -Version Latest | |
if ($args.Length -eq 0) { | |
$shell = New-Object -ComObject WScript.Shell | |
$shortcut = $shell.CreateShortcut($PSCommandPath.Replace(".ps1", "") + " (Drag'n'Drop).lnk") | |
$pwsh = "pwsh" | |
if (!$(Get-Command pwsh -ErrorAction SilentlyContinue)) { | |
Write-Output "PowerShell (pwsh) not found, falling back to Windows PowerShell (powershell)" | |
$pwsh = "powershell" | |
} | |
$shortcut.TargetPath = $pwsh | |
$shortcut.Arguments = "-NoLogo -NoExit -NoProfile -ExecutionPolicy Bypass -Command ""& { $PSCommandPath `$args }""" | |
$shortcut.Save() | |
exit 0 | |
} | |
if ($args.Length -ne 1) { | |
Write-Error "Expected exactly 1 argument, but found $($args.Length)" | |
exit 1 | |
} | |
if (!$(Get-Command ffmpeg -ErrorAction SilentlyContinue) -or !$(Get-Command ffprobe -ErrorAction SilentlyContinue)) { | |
$consent = Read-Host "This script requires FFmpeg, do you want to install it using winget (winget install --id Gyan.FFmpeg -e)? [y/n]" | |
if ($consent.Trim().ToLower() -ne "y") { | |
Write-Output "Please install FFmpeg manually and try again" | |
exit 0 | |
} | |
if (!$(Get-Command winget -ErrorAction SilentlyContinue)) { | |
Write-Error "It seems like you don't have winget installed, either update your Windows version or get it from https://github.com/microsoft/winget-cli and try again" | |
exit 1 | |
} | |
Write-Output "Trying to install FFmpeg" | |
winget install --id Gyan.FFmpeg -e | |
if ($?) { | |
Write-Output "Successfully installed FFmpeg" | |
} else { | |
Write-Error "Failed to install FFmpeg" | |
exit 1 | |
} | |
} | |
$inputFile = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $args[0])) | |
$outputFile = [System.IO.Path]::ChangeExtension($inputFile, ".m4a") | |
if (!(Test-Path $inputFile)) { | |
Write-Error "Input file not found: ""$inputFile""" | |
exit 1 | |
} | |
if (Test-Path $outputFile) { | |
Write-Error "Output file already exists: ""$outputFile""" | |
exit 1 | |
} | |
$ffprobe = ffprobe -loglevel warning -print_format json -show_format -show_streams "$inputFile" | ConvertFrom-Json | |
$audioChannelCount = ($ffprobe.streams | Where-Object codec_type -eq "audio")[0].channels | |
$audioBitrate = 160 | |
if ($audioChannelCount -gt 2) { | |
$audioBitrate = 64 * $audioChannelCount | |
} | |
ffmpeg -benchmark -i "$inputFile" -map "0:a" -c:a "aac" -b:a "${audioBitrate}k" -movflags +faststart -map_metadata "-1" -map_chapters "-1" -metadata:g title="" -metadata:g description="" "$outputFile" | |
if ($?) { | |
Write-Output "`nSuccessfully converted ""$inputFile"" to ""$outputFile"" with FFmpeg`n" | |
exit 0 | |
} else { | |
Write-Error "`nFailed to convert ""$inputFile"" to ""$outputFile"" with FFmpeg`n" | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment