Skip to content

Instantly share code, notes, and snippets.

@Kas-tle
Created December 24, 2023 04:50
Show Gist options
  • Save Kas-tle/5a63422a8a35fb2523607c99f2ffbd32 to your computer and use it in GitHub Desktop.
Save Kas-tle/5a63422a8a35fb2523607c99f2ffbd32 to your computer and use it in GitHub Desktop.
$playlist = Read-Host -Prompt "Enter playlist"
if (-not (Test-Path 'songs')) {
New-Item -Path 'songs' -ItemType Directory | Out-Null
}
# Download all songs
yt-dlp --ignore-errors --extract-audio --audio-format vorbis --audio-quality 5 --output "songs/%(video_autonumber)03d.%(ext)s" --yes-playlist "$playlist"
# Loop through each .ogg file in the directory
Get-ChildItem -Filter "songs/*.ogg" | ForEach-Object {
# Extract the base name of the file without the extension
$baseName = $_.BaseName
# Create a new directory with the base name
New-Item -Path "rp/assets/music/sounds/$baseName" -ItemType Directory -Force
# Use ffmpeg to split the .ogg file into 30-second segments in the new directory
ffmpeg -i $_.FullName -f segment -segment_time 30 -c copy "rp/assets/music/sounds/$baseName/%02d.ogg"
}
# Initialize an ordered dictionary to hold all JSON data
$jsonData = [ordered]@{}
# Loop through each subdirectory and .ogg file, maintaining the file system order
Set-Location "rp/assets/music/sounds"
Get-ChildItem -Directory | Sort-Object Name | ForEach-Object {
$subDir = $_.Name
Get-ChildItem -Path $_.FullName -Filter *.ogg | Sort-Object Name | ForEach-Object {
$baseName = $_.BaseName
$soundKey = "music.$subDir.$baseName"
$jsonData[$soundKey] = @{
"sounds" = @(
@{
"name" = "music:$subDir/$baseName"
"stream" = $true
}
)
}
}
}
# Return to the original directory
Set-Location "../../../.."
# Convert the ordered dictionary to a JSON object
$jsonString = $jsonData | ConvertTo-Json -Depth 10
# Write the JSON string to the sounds.json file in the current directory
Set-Content -Path "rp/assets/music/sounds.json" -Value $jsonString
# Ensure the functions directory exists
$functionsRelDir = "dp/data/music/functions"
if (-not (Test-Path "$functionsRelDir")) {
New-Item -Path "$functionsRelDir" -ItemType Directory | Out-Null
}
# Base directory for the functions
$functionsBaseDir = Resolve-Path "$functionsRelDir"
Set-Location "rp/assets/music/sounds"
# Get all .ogg files in the subdirectories
$oggFiles = Get-ChildItem -Recurse -Filter *.ogg
# Loop through each .ogg file to calculate its duration
foreach ($file in $oggFiles) {
# Use ffmpeg to get the duration in seconds
$output = & ffmpeg -i $file.FullName -hide_banner -f null - 2>&1
$durationLine = $output | Select-String "Duration: ([0-9:.]+)"
$duration = $durationLine.Matches.Groups[1].Value
# Convert duration to Minecraft ticks (20 ticks = 1 second)
$timeSpan = [TimeSpan]::Parse($duration)
$ticks = [Math]::Round(($timeSpan.TotalSeconds * 20), 0)
# Determine the subdirectory for this file's function
$subDirName = if ($file.Directory.Name) { $file.Directory.Name } else { "root" }
$songDir = Join-Path "$functionsBaseDir" $subDirName
# Ensure the song directory exists
if (-not (Test-Path $songDir)) {
New-Item -Path $songDir -ItemType Directory | Out-Null
}
# Write individual function file for this sound
$functionPath = Join-Path $songDir "$($file.BaseName).mcfunction"
$nextFile = $oggFiles | Where-Object { $_.FullName -gt $file.FullName } | Select-Object -First 1
$nextFunction = if ($nextFile) { "music:$($nextFile.Directory.Name)/$($nextFile.BaseName)" } else { "music:001/00" }
$content = "execute in minecraft:overworld run playsound music:music.$subDirName.$($file.BaseName) master @a ~ ~1000 ~ 999999 1 1`nschedule function $nextFunction $($ticks)t append"
Set-Content -Path $functionPath -Value $content
}
Set-Location "../../../.."
# Create pack.mcmeta files
$packMcmeta = @{
"pack" = @{
"pack_format" = 18
"description" = "Music Pack"
}
} | ConvertTo-Json -Depth 10
Set-Content -Path "rp/pack.mcmeta" -Value $packMcmeta
Set-Content -Path "dp/pack.mcmeta" -Value $packMcmeta
Get-ChildItem -Path "rp/" | Compress-Archive -DestinationPath "rp.zip"
Get-ChildItem -Path "dp/" | Compress-Archive -DestinationPath "dp.zip"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment