Saturday, February 24, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 845  / 3 Years ago, wed, may 26, 2021, 1:31:01

I'm cleaning my music library and got stuck with getting rid of thousands of albums with bitrate <320kbps which gathered there for years. Checking single files bitrate and deleting whole folder by hand is realy tedious. Maybe someone here can come up with some idea which would help me with cleanup? I'd like to have after that only MP3s@320kbps and flacs. Thanks in advance!


More From » command-line

 Answers
2

Here's a shell approach. It will delete any directories that don't contain .mp3 files of the >= 320 kbps bitrate:


find /path/to/Music -type d -print0 | 
while IFS= read -r -d '' dir; do
mp3=$(find "$dir" -type f -iname '*.mp3' | head -n 1);
[ -e "$mp3" ] && [[ $(mp3info -x "$mp3" | grep -oP 'd+(?=s*kbps)') -lt 320 ]] &&
rm -rf "$(dirname "$mp3")";
done

Notes



  • This will remove any directories that contain at least one mp3 file with a bitrate less than 320. If another file exists in the same directory with the right bitrate, that will be deleted as well. This approach assumes that all files in a directory have the same bitrate.



  • This will miss files of variable bitrate.



  • It should work with any type of file name, including those with spaces, newlines or even backslashes.



  • You might need to install mp3info: sudo apt-get install mp3info



  • Run it on a test directory first.




Explanation



  • find /path/to/Music -type d -print0 : find all directories under /path/to/Music and print them separated by the null string. This is needed to deal with strange file names.



  • while IFS= read -r -d '' dir; do : go through each of the results of find, saving them in the $dir variable.



  • mp3=$(find "$dir" -type f -iname '*.mp3' | head -n 1); : save the name of the first mp3 file in this directory as $mp3.



  • [ -e "$mp3" ] : if this file exists. This is needed for skipping directories with no mp3 files.



  • [[ $(mp3info -x "$mp3" | grep -oP 'd+(?=s*kbps)') -lt 320 ]] : this checks the bitrate of $mp3. It runs mp3info, greps the bitrate and checks if it is less than 320.



  • rm -rf "$(dirname "$mp3")"; : delete the directory that contains the mp3 file. This will only be run if its bitrate is less than desired.




[#23014] Thursday, May 27, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nalystcul

Total Points: 390
Total Questions: 106
Total Answers: 115

Location: Tokelau
Member since Sun, May 7, 2023
10 Months ago
nalystcul questions
Sun, May 1, 22, 17:05, 2 Years ago
Mon, Sep 6, 21, 08:12, 3 Years ago
Fri, Mar 18, 22, 20:06, 2 Years ago
;