Skip to content

Instantly share code, notes, and snippets.

@tylerhall
Created August 8, 2021 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerhall/4c8be3b595dfcef6d35ccc1f10c37d8d to your computer and use it in GitHub Desktop.
Save tylerhall/4c8be3b595dfcef6d35ccc1f10c37d8d to your computer and use it in GitHub Desktop.
Trying to cleanup my music library this morning. It was easier to write this script in Swift than try and figure out the equivalent in Bash.
// Trying to cleanup my music library this morning.
// It was easier to write this script in Swift than
// try and figure out the equivalent in Bash.
//
// I'm using this in conjunction with Beets <https://beets.io/>.
//
// Prior to importing music into my library with Beets, this
// script attempts to callout any problematic album folders that:
//
// 1. Contain BOTH .flac and at least one lossy format
// 2. Contain MULTIPLE lossy formats
import Foundation
let topLevelMusicDir = "/Volumes/12TB/media/music/"
guard let artists = try? FileManager.default.contentsOfDirectory(atPath: topLevelMusicDir) else { exit(0) }
for artist in artists {
if let albums = try? FileManager.default.contentsOfDirectory(atPath: topLevelMusicDir + artist) {
for album in albums {
let folder = topLevelMusicDir + artist + "/" + album
var hasFLAC = false
var hasMP3 = false
var hasAAC = false
var hasM4A = false
var count = 0
if let files = try? FileManager.default.contentsOfDirectory(atPath: folder) {
for f in files {
let haystack = f.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
if !hasFLAC && haystack.hasSuffix(".flac") { hasFLAC = true; count += 1 }
if !hasMP3 && haystack.hasSuffix(".mp3") { hasMP3 = true; count += 1 }
if !hasAAC && haystack.hasSuffix(".aac") { hasAAC = true; count += 1 }
if !hasM4A && haystack.hasSuffix(".m4a") { hasM4A = true; count += 1 }
if count > 1 {
print("Has multiple lossy: \(folder)")
break
}
}
if hasFLAC && (hasMP3 || hasAAC || hasM4A) {
print("Has FLAC and lossy: \(folder)")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment