booksplit 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/sh
  2. # Requires ffmpeg (audio splitting) and my `tag` wrapper script.
  3. [ ! -f "$2" ] && printf "The first file should be the audio, the second should be the timecodes.\\n" && exit
  4. echo "Enter the album/book title:"; read -r booktitle
  5. echo "Enter the artist/author:"; read -r author
  6. echo "Enter the publication year:"; read -r year
  7. inputaudio="$1"
  8. # Get a safe file name from the book.
  9. escbook="$(echo "$booktitle" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g")"
  10. ! mkdir -p "$escbook" && echo "Do you have write access in this directory?" && exit 1
  11. # As long as the extension is in the tag script, it'll work.
  12. ext="opus"
  13. #ext="${1#*.}"
  14. # Get the total number of tracks from the number of lines.
  15. total="$(wc -l < "$2")"
  16. while read -r x;
  17. do
  18. end="$(echo "$x" | cut -d' ' -f1)"
  19. [ -n "$start" ] &&
  20. echo "From $start to $end; $track $title"
  21. file="$escbook/$(printf "%.2d" "$track")-$esctitle.$ext"
  22. [ -n "$start" ] && echo "Splitting \"$title\"..." &&
  23. ffmpeg -nostdin -y -loglevel error -i "$inputaudio" -ss "$start" -to "$end" -vn -c copy "$file" &&
  24. echo "Tagging \"$title\"..." && tag -a "$author" -A "$booktitle" -t "$title" -n "$track" -N "$total" -d "$year" "$file"
  25. title="$(echo "$x" | cut -d' ' -f 2-)"
  26. esctitle="$(echo "$title" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g")"
  27. track="$((track+1))"
  28. start="$end"
  29. done < "$2"
  30. # The last track must be done outside the loop.
  31. echo "From $start to the end: $title"
  32. file="$escbook/$(printf "%.2d" "$track")-$esctitle.$ext"
  33. echo "Splitting \"$title\"..." && ffmpeg -nostdin -y -loglevel error -i "$inputaudio" -ss "$start" -vn -c copy "$file" &&
  34. echo "Tagging \"$title\"..." && tag -a "$author" -A "$booktitle" -t "$title" -n "$track" -N "$total" -d "$year" "$file"