Add script
This commit is contained in:
		
							
								
								
									
										106
									
								
								deduper.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										106
									
								
								deduper.sh
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,106 @@
 | 
				
			|||||||
 | 
					#!/bin/bash
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					LOGFILE="dupe_cleanup.log"
 | 
				
			||||||
 | 
					ARMED=false  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [[ "$1" == "--armed" ]]; then
 | 
				
			||||||
 | 
					    ARMED=true
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					log() {
 | 
				
			||||||
 | 
					    echo "$1" | tee -a "$LOGFILE"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					get_quality() {
 | 
				
			||||||
 | 
					    file="$1"
 | 
				
			||||||
 | 
					    bitrate=$(mediainfo --Output="Audio;%BitRate%" "$file" | awk '{print $1}')
 | 
				
			||||||
 | 
					    bitdepth=$(mediainfo --Output="Audio;%BitDepth%" "$file" | awk '{print $1}')
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    bitrate=${bitrate:-0}
 | 
				
			||||||
 | 
					    bitdepth=${bitdepth:-0}
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    echo "$bitrate $bitdepth"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					echo "===============================" | tee -a "$LOGFILE"
 | 
				
			||||||
 | 
					log "Starting duplicate cleanup at $(date)"
 | 
				
			||||||
 | 
					if ! $ARMED; then
 | 
				
			||||||
 | 
					    log "DRY-RUN MODE: No files will be renamed or deleted."
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
					    log "ARMED MODE: Files **will** be renamed and deleted!"
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					echo "===============================" | tee -a "$LOGFILE"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					find . -type f -regextype posix-extended -regex ".*\([0-9]\)\.flac" | while read -r dup_file; do
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    orig_file=$(echo "$dup_file" | sed -E 's/ \([0-9]+\)\.flac/.flac/')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    candidates=()
 | 
				
			||||||
 | 
					    if [[ -f "$orig_file" ]]; then
 | 
				
			||||||
 | 
					        candidates+=("$orig_file")
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    for n in {1..9}; do
 | 
				
			||||||
 | 
					        candidate=$(echo "$orig_file" | sed "s/.flac$/ ($n).flac/")
 | 
				
			||||||
 | 
					        if [[ -f "$candidate" ]]; then
 | 
				
			||||||
 | 
					            candidates+=("$candidate")
 | 
				
			||||||
 | 
					        fi
 | 
				
			||||||
 | 
					    done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    if [[ ${#candidates[@]} -le 1 ]]; then
 | 
				
			||||||
 | 
					        continue
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    best_file=""
 | 
				
			||||||
 | 
					    best_bitrate=0
 | 
				
			||||||
 | 
					    best_bitdepth=0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    log "Checking duplicates for: $orig_file"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    for file in "${candidates[@]}"; do
 | 
				
			||||||
 | 
					        read bitrate bitdepth < <(get_quality "$file")
 | 
				
			||||||
 | 
					        log "  - Found: $file (Bitrate: $bitrate, Bitdepth: $bitdepth)"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (( bitrate > best_bitrate )) || { (( bitrate == best_bitrate )) && (( bitdepth > best_bitdepth )); }; then
 | 
				
			||||||
 | 
					            best_bitrate=$bitrate
 | 
				
			||||||
 | 
					            best_bitdepth=$bitdepth
 | 
				
			||||||
 | 
					            best_file="$file"
 | 
				
			||||||
 | 
					        fi
 | 
				
			||||||
 | 
					    done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    log "  -> Keeping: $best_file (Bitrate: $best_bitrate, Bitdepth: $best_bitdepth)"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    if [[ "$best_file" != "$orig_file" ]]; then
 | 
				
			||||||
 | 
					        log "  * Would rename: $best_file -> $orig_file"
 | 
				
			||||||
 | 
					        if $ARMED; then
 | 
				
			||||||
 | 
					            mv "$best_file" "$orig_file"
 | 
				
			||||||
 | 
					        fi
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    for file in "${candidates[@]}"; do
 | 
				
			||||||
 | 
					        if [[ "$file" != "$best_file" ]]; then
 | 
				
			||||||
 | 
					            log "  * Would delete: $file"
 | 
				
			||||||
 | 
					            if $ARMED; then
 | 
				
			||||||
 | 
					                rm "$file"
 | 
				
			||||||
 | 
					            fi
 | 
				
			||||||
 | 
					        fi
 | 
				
			||||||
 | 
					    done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    echo "" | tee -a "$LOGFILE"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					log "Cleanup completed at $(date)"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Reference in New Issue
	
	Block a user