├── Folder Actions ├── README.md └── src │ └── flac-to-itunes.bash ├── README.md └── Services └── src └── open-in-tmux.bash /Folder Actions/README.md: -------------------------------------------------------------------------------- 1 | # FLAC to iTunes 2 | 3 | Automatic encoding and covers embedding workflow for OS X. 4 | 5 | ## Installation 6 | 7 | ``` 8 | brew install ffmpeg atomicparsley python3 9 | brew install gnu-sed --with-default-names 10 | pip3 install sacad 11 | ``` 12 | 13 | ## Workflow 14 | 15 | Unzip and put "FLAC to iTunes importer.workflow" into ```~/Library/Workflows/Applications/Folder Actions/``` 16 | 17 | ## Usage 18 | 19 | 1) Folder action setup 20 | 2) Attach 21 | 3) Copy album into workflow folder and tracks will be automatic imported in iTunes 22 | 23 | -------------------------------------------------------------------------------- /Folder Actions/src/flac-to-itunes.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ################################## 4 | # FLAC to iTunes importer v0.5.1 # 5 | ################################## 6 | 7 | export PATH="/usr/local/bin:$PATH" 8 | 9 | processAlbumFolder() { 10 | SRC=$1 11 | DST="$HOME/Music/iTunes/iTunes Media/Automatically Add to iTunes" 12 | 13 | if [ ! -d "$DST" ]; then 14 | ln -s $HOME/Music/iTunes/iTunes\ Media/Automatically\ Add\ to\ iTunes.localized $HOME/Music/iTunes/iTunes\ Media/Automatically\ Add\ to\ iTunes 15 | mkdir -p $DST 16 | fi 17 | 18 | # sleep while download active 19 | while [ $(ps aux | grep lftp | grep -v grep | wc -l) -gt "0" ]; do 20 | sleep 5 21 | done 22 | 23 | LABEL=$(xattr "$SRC" | grep flac_imported | wc -l) 24 | FOUND=$(find "$SRC" -name "*.flac" | wc -l) 25 | 26 | COVER_NAME="converter_cover.jpg" 27 | META_NAME="converter_metadata.txt" 28 | 29 | COVER_PATH="$SRC/$COVER_NAME" 30 | META_PATH="$SRC/$META_NAME" 31 | 32 | # set parsed label 33 | xattr -w flac_imported true "$SRC" 34 | 35 | if [ "$LABEL" -eq "0" ] && [ -n "$SRC" ] && [ "$FOUND" -gt "0" ]; then 36 | FIRST=$(find "$SRC" -name "*.flac" -print | head -n 1) 37 | 38 | # trying to extract cover from current track 39 | ffmpeg -i "$FIRST" "$COVER_PATH" 40 | FF=$? 41 | 42 | # convert for test AP 43 | ffmpeg -y -i "$FIRST" -c:a alac -vn "$FIRST.m4a" 44 | 45 | # trying to embed cover 46 | AtomicParsley "$FIRST.m4a" --artwork "$COVER_PATH" --overWrite 47 | AP=$? 48 | 49 | # if failed, trying download from sacad 50 | if [ "${FF}" -ne "0" ] || [ "${AP}" -ne "0" ]; then 51 | ffmpeg -i "$FIRST" -f ffmetadata "$META_PATH" 52 | 53 | ARTIST=$(cat "$META_PATH" | grep -i artist= | head -1 | sed -e "s/artist=//I" | xargs) 54 | ALBUM_ARTIST=$(cat "$META_PATH" | grep -i "^album.*artist=" | head -1 | sed -e "s/album.*artist=//I" | xargs) 55 | ALBUM=$(cat "$META_PATH" | grep -i album= | head -1 | sed -e "s/album=//I" | sed -e "s/(.*digipack.*)\|(.*deluxe.*)\|(.*bonus.*)\|(.*japan.*)\|(.*remaster.*)\|(.*mfsl.*)//I" | xargs) 56 | 57 | if [ "$ALBUM_ARTIST" ]; then 58 | ARTIST=$ALBUM_ARTIST 59 | fi 60 | 61 | SACAD=$(sacad -d "$ARTIST" "$ALBUM" 600 "$COVER_PATH" 2>&1) 62 | 63 | if [[ $SACAD == *"No results"* ]] 64 | then 65 | ALBUM=$(cat "$META_PATH" | grep -i album= | head -1 | sed -e "s/album=//I" | sed -e "s/(.*)//I" | xargs) 66 | sacad -d "$ARTIST" "$ALBUM" 600 "$COVER_PATH" 67 | fi 68 | 69 | echo "Detected artist/album: '$ARTIST/$ALBUM'" 70 | echo "Path: $COVER_PATH" 71 | 72 | rm "$META_PATH" 73 | fi 74 | 75 | find "$SRC" -name "*.flac" -exec ffmpeg -y -i '{}' -c:a alac -vn '{}'.m4a \; 76 | find "$SRC" -name "*.m4a" -exec AtomicParsley '{}' --artwork "$COVER_PATH" --overWrite \; 77 | find "$SRC" -name "*.m4a" -exec mv '{}' "$DST" \; 78 | 79 | rm "$COVER_PATH" 80 | 81 | /usr/bin/osascript -e 'display notification "New ALAC tracks imported " with title "iTunes"' 82 | fi 83 | } 84 | 85 | LOCK="/tmp/flac-to-itunes.lock" 86 | trap "rm -f $LOCK" SIGINT SIGTERM 87 | 88 | if [ -e "$LOCK" ] || [ -z "$1" ] 89 | then 90 | echo "flac to itunes is running already." 91 | exit 92 | else 93 | touch "$LOCK" 94 | 95 | while [ $(find "$BASE/"* -maxdepth 0 -type d | wc -l) -gt $(find "$BASE/"* -maxdepth 0 -type d -exec xattr "{}" \+ | grep flac_imported | wc -l) ]; do 96 | BASE=$(dirname "$1") 97 | 98 | IFS=$'\n' 99 | 100 | for DIRECTORY in $(find "$BASE/"* -maxdepth 0 -type d); 101 | do 102 | IMPORTED=$(xattr "$DIRECTORY" | grep flac_imported | wc -l) 103 | if [ "$IMPORTED" -eq "0" ]; then 104 | echo "Album: $DIRECTORY" >> /tmp/flac_imported 105 | 106 | processAlbumFolder "$DIRECTORY" 107 | fi 108 | done 109 | done 110 | 111 | rm -f "$LOCK" 112 | trap - SIGINT SIGTERM 113 | exit 114 | fi -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # macOS automation 2 | 3 | ## Services 4 | 5 | Open in tmux workflow v0.1 – [Download workflow](https://github.com/glushchenko/macos-automation/releases/download/latest/open.in.tmux.v0.1.zip) 6 | 7 | ## Folder Actions 8 | 9 | ### Automatic encoding and covers embedding workflow for OS X 10 | 11 | FLAC to iTunes importer v0.5.1 – [Download workflow](https://github.com/glushchenko/macos-automation/releases/download/latest/flac.to.itunes.importer.v0.5.1.zip) 12 | -------------------------------------------------------------------------------- /Services/src/open-in-tmux.bash: -------------------------------------------------------------------------------- 1 | export PATH=/usr/local/bin:$PATH 2 | 3 | PWD=$1 4 | CURRENT=$(basename "$PWD") 5 | 6 | SESSION_EXIST=$(tmux ls | grep attached | wc -l) 7 | 8 | if [ $SESSION_EXIST -gt 0 ]; then 9 | open /Applications/Utilities/Terminal.app 10 | tmux neww -c "$PWD" -n "$CURRENT" 11 | else 12 | osascript -e 'tell application "Terminal" to do script "tmux"' 13 | sleep 1 14 | tmux neww -c "$PWD" -n "$CURRENT" 15 | 16 | # reorder for position 1 17 | tmux move-window -t 2 18 | tmux kill-window -t 1 19 | tmux move-window -t 1 20 | 21 | open /Applications/Utilities/Terminal.app 22 | fi --------------------------------------------------------------------------------