Utils
This module contains the configuration object for the utils package. The attributes of this configuration object correspond with the "utils" key of config.yaml
UtilsConfig
Bases: BaseConfigFormatter
Configuration object for the utils package.
Source code in djtools/utils/config.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
__init__(*args, **kwargs)
Constructor.
Raises:
Type | Description |
---|---|
RuntimeError
|
aws_profile must be set for check_tracks. |
Source code in djtools/utils/config.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
bitrate_validation(value)
classmethod
Validates audio_bitrate is in the range and casts it to a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
audio_bitrate field |
required |
Raises:
Type | Description |
---|---|
ValueError
|
audio_bitrate must be in the range [36, 320] |
Returns:
Type | Description |
---|---|
str
|
String representing the bit rate. |
Source code in djtools/utils/config.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
format_validation(model)
classmethod
Logs a warning message to install FFmpeg if audio_format isn't wav.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
UtilsConfig
|
The validated model instance. |
required |
Returns:
Type | Description |
---|---|
UtilsConfig
|
The validated model instance. |
Source code in djtools/utils/config.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
This module is used to compare tracks from Spotify playlists and / or local directories to see if there is any overlap with the contents of the Beatcloud.
compare_tracks(config, beatcloud_tracks=None)
Compares tracks from Spotify / local with Beatcloud tracks.
Gets track titles and artists from Spotify playlist(s) and / or file names from local directories, and get file names from the beatcloud. Then compute the Levenshtein similarity between their product in order to identify any overlapping tracks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
BaseConfig
|
Configuration object. |
required |
beatcloud_tracks
|
Optional[List[str]]
|
Cached list of tracks from S3. |
None
|
Returns:
Type | Description |
---|---|
Tuple[List[str], List[str]]
|
Tuple with a list of all Beatcloud tracks and list of full paths to matching Beatcloud tracks. |
Source code in djtools/utils/check_tracks.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
This module is used to normalize audio files in one or more directories.
normalize(config)
Gets local tracks and normalizes them.
Tracks will be overwritten and have a headroom equal to audio_headroom.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
BaseConfig
|
Configuration object. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
Must have local tracks to normalize. |
Source code in djtools/utils/normalize_audio.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
This module is used to process an audio recording.
Given a recording of multiple tracks and a Spotify playlist, use the information from the Spotify API to:
- split the recording into individual files
- name these files with the title and artist(s)
- populate the title, artist, and album tags
- normalize the audio so the headroom is audio_headroom decibels
- export the files with the configured audio_bitrate and audio_format
process(config)
Process a recording whose contents map to tracks in a Spotify playlist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
BaseConfig
|
Configuration object. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
The configured recording_playlist must both exist in spotify_playlists.yaml and have tracks in it. |
Source code in djtools/utils/process_recording.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
This module is used to download tracks from "url_download". For example, a Soundcloud playlist can be made and the URL of that playlist can be provided to download all those tracks and rename them to cleanup the digits appended to the files by the youtube-dl package.
fix_up(_file)
Removes digits appended to file name by youtube-dl.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_file
|
Path
|
Music file name. |
required |
Returns:
Type | Description |
---|---|
Path
|
Cleaned up music file name. |
Source code in djtools/utils/url_download.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
url_download(config)
Downloads music files from a provided URL using the youtube-dl package.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
BaseConfig
|
Configuration object. |
required |
Source code in djtools/utils/url_download.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
This module contains helper functions that are not specific to any particular sub-package of this library.
compute_distance(spotify_playlist, spotify_track, beatcloud_track, threshold)
Qualifies a match between a Spotify track and a beatcloud track using Levenshtein similarity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spotify_playlist
|
str
|
Playlist that Spotify track belongs to. |
required |
spotify_track
|
str
|
Spotify track title and artist name. |
required |
beatcloud_track
|
str
|
Beatcloud track title and artist name |
required |
threshold
|
float
|
Levenshtein similarity threshold for acceptance. |
required |
Returns:
Type | Description |
---|---|
Tuple[str, float]
|
Tuple of Spotify playlist, Spotify "TRACK TITLE - ARTIST NAME", beatcloud "TRACK TITLE - ARTIST NAME", Levenshtein similarity. |
Source code in djtools/utils/helpers.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
find_matches(compare_tracks, beatcloud_tracks, config)
Computes the Levenshtein similarity between beatcloud tracks the given tracks to compare with and returns those that match above a threshold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
compare_tracks
|
Dict[str, Set[str]]
|
Dictionary with either local directory or Spotify playlist keys and filenames or title and artists values. |
required |
beatcloud_tracks
|
List[str]
|
Beatcloud track titles and artist names. |
required |
config
|
BaseConfig
|
Configuration object. |
required |
Returns:
Type | Description |
---|---|
List[Tuple[str, float]]
|
List of tuples of track location (directory or playlist), track name, Beatcloud track, and Levenshtein distance. |
Source code in djtools/utils/helpers.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
get_beatcloud_tracks(bucket_url)
Lists all the music files in S3 and parses out the track titles and artist names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bucket_url
|
URL to an AWS S3 API compliant bucket. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
Beatcloud track titles and artist names. |
Source code in djtools/utils/helpers.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
get_local_tracks(config)
Aggregates the files from one or more local directories in a dictionary mapped with parent directories.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
BaseConfig
|
Configuration object. |
required |
Returns:
Type | Description |
---|---|
Dict[str, List[str]]
|
Local file names keyed by parent directory. |
Source code in djtools/utils/helpers.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
get_playlist_tracks(spotify, playlist_id)
Queries Spotify API for a playlist and pulls tracks from it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spotify
|
Spotify
|
Spotify client. |
required |
playlist_id
|
str
|
Playlist ID of Spotify playlist to pull tracks from. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
Playlist_id must correspond with a valid Spotify playlist. |
Returns:
Type | Description |
---|---|
List[Dict]
|
List of Spotify track results. |
Source code in djtools/utils/helpers.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|
get_spotify_tracks(config, playlists)
Aggregates the tracks from one or more Spotify playlists into a dictionary mapped with playlist names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
BaseConfig
|
Configuration object. |
required |
playlists
|
List[str]
|
List of Spotify playlist name. |
required |
Returns:
Type | Description |
---|---|
Dict[str, List[Dict]]
|
Spotify tracks keyed by playlist name. |
Source code in djtools/utils/helpers.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
initialize_logger()
Initializes logger from configuration.
Returns:
Type | Description |
---|---|
Tuple[Logger, str]
|
Tuple containing Logger and associated log file. |
Source code in djtools/utils/helpers.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
|
make_path(func)
Decorator for converting Path-typed args to Paths.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable
|
Callable being decorated with this function. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
args annotated with a pathlib.Path need to be able to have Paths created from them. |
RuntimeError
|
kwargs annotated with a pathlib.Path need to be able to have Paths created from them. |
Returns:
Type | Description |
---|---|
Callable
|
The Callable being wrapped by this decorator. |
Source code in djtools/utils/helpers.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
|
process_parallel(config, audio, track, write_path)
Normalize and export track with tags.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
BaseConfig
|
Configuration object. |
required |
audio
|
AudioSegment
|
Audio for a track. |
required |
track
|
Dict
|
Metadata for track audio. |
required |
write_path
|
Path
|
Destination for exported audio. |
required |
Returns:
Type | Description |
---|---|
Path
|
Path that the file was written to. |
Source code in djtools/utils/helpers.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
|
reverse_title_and_artist(path_lookup)
Reverses the title and artist parts of the filename.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_lookup
|
Dict[str, str]
|
Mapping of filenames to file paths. |
required |
Returns:
Type | Description |
---|---|
Dict[str, str]
|
Mapping with the title and artist in the filenames reversed. |
Source code in djtools/utils/helpers.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
|
trim_initial_silence(audio, track_durations, trim_amount, silence_thresh=-50, min_silence_ms=5, step_size=100)
Heuristic for determining the amount of leading silence to trim.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
audio
|
AudioSegment
|
Audio with leading silence. |
required |
track_durations
|
List[int]
|
List of track durations. |
required |
trim_amount
|
Union[int, TrimInitialSilenceMode]
|
Number of milliseconds to trim off the beginning. |
required |
silence_thresh
|
Optional[float]
|
Maximum decibel level that's still considered silence. |
-50
|
min_silence_ms
|
Optional[int]
|
Surrounding milliseconds of each track to check for silence. |
5
|
step_size
|
Optional[int]
|
Initial step size when checking for leading silences. |
100
|
Returns:
Name | Type | Description |
---|---|---|
AudioSegment |
AudioSegment
|
Audio with the beginning silence trimmed off. |
Source code in djtools/utils/helpers.py
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
|