Skip to content

Instantly share code, notes, and snippets.

@milnak
Last active April 22, 2024 15:53
Show Gist options
  • Save milnak/f7c5ff5fae848a694f75a53c2aee1754 to your computer and use it in GitHub Desktop.
Save milnak/f7c5ff5fae848a694f75a53c2aee1754 to your computer and use it in GitHub Desktop.
[ytdlp functions] PowerShell functions to wrap ytdlp
<#
.SYNOPSIS
Download best quality video and audio into default container.
#>
function ytdlp-v {
Param(
[Parameter(Mandatory = $true)]
[string]$Uri
)
$ytdlp_args = `
'--quiet', `
'--progress', `
'--no-simulate', `
'--format', `
'bv*+ba'
if ($Uri -like '*list=*') {
# Playlist download
'Playlist: "{0}"' -f (yt-dlp.exe --no-warnings --playlist-start 1 --playlist-end 1 --print '%(channel)s/%(playlist_title)s' $Uri)
yt-dlp.exe @ytdlp_args --print '[%(playlist_index)d/%(n_entries+1)d] %(title)s' --output '%(channel)s/%(playlist_title)s/%(title)s [%(id)s].%(ext)s' $Uri
}
else {
# Single file download
yt-dlp.exe @ytdlp_args --print '%(title)s [%(id)s]' --output '%(title)s [%(id)s].%(ext)s' $Uri
}
}
<#
.SYNOPSIS
Download audio.
#>
function ytdlp-a {
Param(
[Parameter(Mandatory = $true)]
[string]$Uri,
[Parameter(Mandatory = $true)]
[ValidateSet('best', 'aac', 'alac', 'flac', 'm4a', 'mp3', 'opus', 'vorbis', 'wav')]
[string]$Format
)
$ytdlp_args = `
'--quiet', `
'--progress', `
'--no-simulate', `
'--extract-audio', `
'--audio-format', `
$Format `
if ($Uri -like '*list=*') {
# Playlist download
'Playlist: "{0}"' -f (yt-dlp.exe --no-warnings --playlist-start 1 --playlist-end 1 --print '%(channel)s/%(playlist_title)s' $Uri)
yt-dlp.exe @ytdlp_args --print '[%(playlist_index)d/%(n_entries+1)d] %(title)s [%(id)s]' --output '%(channel)s/%(playlist_title)s/%(title)s [%(id)s].%(ext)s' $Uri
}
else {
# Single file download
yt-dlp.exe @ytdlp_args --print '%(title)s [%(id)s]' --output '%(title)s [%(id)s].%(ext)s' $Uri
}
}
<#
.SYNOPSIS
Download audio, convert to Karaoke file
#>
function ytdlp-k {
[CmdletBinding(DefaultParameterSetName = 'Uri')]
Param(
# YT URL to download audio from.
[Parameter(Mandatory, ParameterSetName = 'Uri', Position = 0)]
[Uri]$Uri,
# Keep the downloaded sound file.
[Parameter(ParameterSetName = 'Uri')]
[switch]$KeepOutput,
# Local file to extract stems from.
[Parameter(Mandatory, ParameterSetName = 'Path')]
[string[]]$Path
)
if (!(Get-Command -CommandType Application 'demucs.exe' -ErrorAction SilentlyContinue)) {
Write-Host -ForegroundColor Red 'demucs not found. Install using "https://github.com/charzy/Demucs-v4-/blob/main/docs/windows.md"'
return
}
if ($Uri) {
# Only Uri requires yt-dlp.
if (!(Get-Command -CommandType Application 'yt-dlp.exe' -ErrorAction SilentlyContinue)) {
Write-Host -ForegroundColor Red 'yt-dlp not found. Install using "scoop install main/yt-dlp"'
return
}
# after_move:filepath: see https://github.com/yt-dlp/yt-dlp#outtmpl-postprocess-note
# audio-format: forcing flac, as it seems that opus isn't supported?
$ytdlp_args = `
'--no-progress', `
'--no-simulate', `
'--extract-audio', `
'--audio-format', 'flac', `
'--restrict-filenames', `
'--print', 'after_move:filepath', `
'--output', '%(title)s [%(id)s].%(ext)s.'
Write-Host -ForegroundColor Yellow "Downloading: $Uri"
$Path = yt-dlp.exe @ytdlp_args $Uri
}
if (!(Test-Path -PathType Leaf -LiteralPath $Path)) {
'File "{0}" doesnt exist' -f $Path
return
}
$numCores = Get-WmiObject –class Win32_processor | Select-Object -ExpandProperty NumberOfCores
$cd = (Get-Location).Path
# two-stems: only separate vocals out of audio.
$demucs_args = `
'--out', $cd, `
'--mp3', `
'--mp3-bitrate', '192', `
'--filename', '{track} - {stem}.{ext}', `
'--two-stems=vocals', `
'--jobs', $numCores
Write-Host -ForegroundColor Yellow "Splitting: $Path"
demucs.exe @demucs_args $Path
# KeepOutput only applies to Uri.
if ($Uri -and !$KeepOutput) {
"Removing: $Path"
Remove-Item $Path
}
Write-Host -ForegroundColor Green ('Done! Output in {0}' -f (Join-Path $cd 'htdemucs'))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment