Code grooming.

master
Karchnu 2021-04-06 17:24:26 +02:00
parent 390659b000
commit f36c7d604e
1 changed files with 17 additions and 22 deletions

View File

@ -56,7 +56,7 @@ to_ascii(){
from_hex # Convert back from hex. from_hex # Convert back from hex.
} }
process_durations(){ process_end_of_songs(){
awk -v NONUMBER="$NONUMBER" -v SEPARATOR="$SEPARATOR" ' awk -v NONUMBER="$NONUMBER" -v SEPARATOR="$SEPARATOR" '
BEGIN { BEGIN {
@ -118,21 +118,20 @@ first_column_to_seconds(){
# Get a more usable time representation for the beginning and the end of songs. # Get a more usable time representation for the beginning and the end of songs.
process_time_file(){ process_time_file(){
to_ascii | first_column_to_seconds | process_durations to_ascii | first_column_to_seconds | process_end_of_songs
} }
run_ffmpeg(){ run_ffmpeg(){
file=$1 file=$1
from=$2 from=$2
duration=$3 to=$3
final_title=$4 final_title=$4
LOG_LEVEL="-loglevel error" LOG_LEVEL="-loglevel error"
FROM="-ss $from" FROM="-ss $from"
if [ "$duration" = "" ]; then TO=""
DURATION="" if [ "$to" != "" ]; then
else TO="-to $to"
DURATION="-t $duration"
fi fi
INPUT_FILE="-i $file" INPUT_FILE="-i $file"
OUTPUT_FILE="$final_title" OUTPUT_FILE="$final_title"
@ -144,7 +143,7 @@ run_ffmpeg(){
echo "extracting '$final_title'" echo "extracting '$final_title'"
;; ;;
v2) v2)
echo "ffmpeg $LOG_LEVEL $FROM $DURATION $INPUT_FILE '$OUTPUT_FILE'" echo "ffmpeg $LOG_LEVEL $FROM $TO $INPUT_FILE '$OUTPUT_FILE'"
;; ;;
*) *)
echo "verbosity is not set properly" >&2 echo "verbosity is not set properly" >&2
@ -153,28 +152,24 @@ run_ffmpeg(){
esac esac
if [ "$SIMULATION" = "" ]; then if [ "$SIMULATION" = "" ]; then
$(< /dev/null ffmpeg $LOG_LEVEL $FROM $DURATION $INPUT_FILE "$OUTPUT_FILE") $(< /dev/null ffmpeg $LOG_LEVEL $FROM $TO $INPUT_FILE "$OUTPUT_FILE")
fi fi
} }
rip(){ rip(){
track_start_s=0
track_end_s=0
audio_file="$1" audio_file="$1"
time_file="$2" time_file="$2"
process_time_file < "$time_file" | while read LINE; do process_time_file < "$time_file" | while read LINE; do
track_start_s=$(echo $LINE | cut -d ' ' -f 1) track_start=$(echo $LINE | cut -d ' ' -f 1)
track_end_s=$(echo $LINE | cut -d ' ' -f 2) track_end=$(echo $LINE | cut -d ' ' -f 2)
track_title=$(echo $LINE | cut -d ' ' -f 3-) track_title=$(echo $LINE | cut -d ' ' -f 3-)
if [ "$track_end_s" != "END_OF_FILE" ]; then
track_duration=$(echo "$track_end_s - $track_start_s" | bc) if [ "$track_end" = "END_OF_FILE" ]; then
else track_end=""
track_duration=""
fi fi
run_ffmpeg "${audio_file}" "${track_start_s}" "${track_duration}" "${track_title}.${FORMAT}" run_ffmpeg "${audio_file}" "${track_start}" "${track_end}" "${track_title}.${FORMAT}"
done done
} }
@ -192,9 +187,9 @@ usage(){
echo "envvar: SEPARATOR [separator] (default: ' - '), write song number, with this separator" echo "envvar: SEPARATOR [separator] (default: ' - '), write song number, with this separator"
echo " example with SEPARATOR='_': song names will be 01_song.opus 02_song.opus…" echo " example with SEPARATOR='_': song names will be 01_song.opus 02_song.opus…"
echo "envvar: VERBOSITY [0-3] (default: 1)" echo "envvar: VERBOSITY [0-3] (default: 1)"
echo " VERBOSITY 0: no output exept errors from ffmpeg" echo " verbosity 0: no output exept errors from ffmpeg"
echo " VERBOSITY 1: simple indications on the current track being extracted" echo " verbosity 1: simple indications on the current track being extracted"
echo " VERBOSITY 2: print actual ffmpeg commands the script currently runs" echo " verbosity 2: print actual ffmpeg commands the script currently runs"
} }
if [ $# -lt 1 ]; then if [ $# -lt 1 ]; then