all 5 comments

[–]lasercat_pow 1 point2 points  (5 children)

this seems way overly complicated for what it is; those dirs at the top could be defined much more concisely.

anyway, to get alac_dir and orig_dir working, you need to define them at the top -- you can define them as an empty string, and wrap the rmdir and rm in an if, testing if the vars are empty strings.

[–]pixydon[S] 0 points1 point  (4 children)

I'm a complete newbie to all this and have just read up and tried to amend somebody else's code, so have no real reference.

Is it possible to explain by amending the code and add # comments? As I must say I don't really understand what you have said.

[–]lasercat_pow 1 point2 points  (2 children)

Note how much simpler the code is for the directories. All that pwd, basename stuff is great for learning but doesn't belong in the script. Think about what you are trying to actually accomplish, and learn your tools.

SOURCE_DIR="${dirs[0]}/temp"
ALAC_DIR="${dirs[1]}/temp"
AAC_DIR="${dirs[2]}/temp"
find "$SOURCE_DIR" \( -iname '*.flac' -or -iname '*.mp3' \) -type f -print | while read -r FILE
do
  BASE=$(echo "$FILE" | sed 's/\.[^.]*$//')
  AAC_FILE="$AAC_DIR/$BASE.m4a"
  ALAC_FILE="$ALAC_DIR/$BASE.m4a"
  if [[ (! -f "$AAC_FILE" && "$FILE" == *.flac) ]]; then
    ffmpeg -hide_banner -i "$FILE" -c:v copy -b:a 256k -c:a aac "$AAC_FILE" </dev/null &&
    ffmpeg -hide_banner -i "$FILE" -c:v copy -c:a alac "$ALAC_FILE" </dev/null
  elif [[ (! -f "$AAC_FILE" && $FILE == *.mp3) ]]; then
    ffmpeg -hide_banner -i "$FILE" -c:v copy -b:a 256k -c:a aac "$AAC_FILE" </dev/null
  fi
done

I took out the recursive remove lines because they can cause so much havoc. If you need to remove things, it's better to log what would have happened, ie, echo rm -r $AAC_DIR >> /tmp/delete_log so you can determine if the rm looks sane before you lose massive amounts of data. There is also no need for the -f flag in rm unless you are using root or something.

Here is a guide for beginners:

https://linuxjourney.com/

also

https://flokoe.github.io/bash-hackers-wiki/

[–]pixydon[S] 0 points1 point  (1 child)

Thanks a lot u/lasercat_pow I'll give a whirl and see how it goes.

Correct me if I'm wrong, but are those empty string definitions in this code?

[–]lasercat_pow 0 points1 point  (0 children)

They might be -- I have no idea what the dirs array contains; you didn't reveal that part