From 0e8a90dd819ac19d2191a7b8077b336b136c6556 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Tue, 6 Apr 2021 16:09:15 +0200 Subject: [PATCH] Comments. --- get-tracks.sh | 55 ++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/get-tracks.sh b/get-tracks.sh index 5c21518..8f77430 100755 --- a/get-tracks.sh +++ b/get-tracks.sh @@ -1,31 +1,34 @@ #!/usr/bin/env sh +# From a single byte in hexadecimal per line +# to lines ending with 0a (hex for '\n'). regroup_lines(){ awk ' BEGIN { - first=1 + line_start=1 } { - if (first == 1) + if (line_start == 1) line = $1; else line = line " " $1; - first = 0; + line_start = 0; if ($1 == "0a") { print line; - first = 1 + line_start = 1 } } END { - if (first == 0) + if (line_start == 0) print line } ' } +# From ’ to ' simple_quote(){ sed "s/e2 80 99/27/g" } @@ -34,20 +37,23 @@ remove_multibyte_characters(){ sed "s/e2 80 .. //g" } -to_hex_one_colomun(){ +# Convert input into hexadecimal and a single byte per line. +to_hex_one_column(){ xxd -p -c 1 } +# Reverse hexadecimal to original value. from_hex(){ xxd -p -r } +# Remove non ascii characters, convert "’" to "'". to_ascii(){ - to_hex_one_colomun | - regroup_lines | - simple_quote | - remove_multibyte_characters | - from_hex + to_hex_one_column | # Convert input into hexadecimal and a single byte per line. + regroup_lines | # Required to easily match multi-byte characters. + simple_quote | # Convert "’" to "'". + remove_multibyte_characters | # Remove non ascii values. + from_hex # Convert back from hex. } process_durations(){ @@ -112,8 +118,7 @@ first_column_to_seconds(){ # Get a more usable time representation for the beginning and the end of songs. process_time_file(){ - time_file="$1" - to_ascii < "$time_file" | first_column_to_seconds | process_durations + to_ascii | first_column_to_seconds | process_durations } run_ffmpeg(){ @@ -159,7 +164,7 @@ rip(){ audio_file="$1" 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_end_s=$(echo $LINE | cut -d ' ' -f 2) track_title=$(echo $LINE | cut -d ' ' -f 3-) @@ -175,7 +180,7 @@ rip(){ usage(){ echo "usage: $0 command" - echo "command: show " + echo "command: show " echo "command: rip " echo echo "song-list line format example: 1:30 My second track of the playlist" @@ -187,9 +192,9 @@ usage(){ 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 "envvar: VERBOSITY [0-3] (default: 1)" - echo " verbosity 0: no output exept errors from ffmpeg" - echo " verbosity 1: simple indications on the current track being extracted" - echo " verbosity 2: print actual ffmpeg commands the script currently runs" + echo " VERBOSITY 0: no output exept errors from ffmpeg" + echo " VERBOSITY 1: simple indications on the current track being extracted" + echo " VERBOSITY 2: print actual ffmpeg commands the script currently runs" } if [ $# -lt 1 ]; then @@ -234,10 +239,6 @@ if [ "$SIMULATION" != "" ]; then echo "SIMULATION envvar is set: this is a simulation." fi -# soxi provides the total length of the music file. -#which soxi 2>/dev/null -#total_length=$(soxi -D "${audio_file}" | sed "s/\..*//") # integer values only - /dev/null 2>/dev/null if [ $? -ne 0 ]; then echo "xxd: you don't have an xxd program with '-p' option." 1>&2 @@ -254,12 +255,12 @@ case "x-${command}" in x-show) # Takes the audio file in first parameter - if [ $# -ne 2 ]; then - echo "Usage: $0 show music-file time-stamps-file" >&2 + if [ $# -ne 1 ]; then + echo "Usage: $0 show time-stamps-file" >&2 exit 1 fi - process_time_file "$2" + process_time_file < "$1" ;; x-rip) @@ -273,10 +274,6 @@ case "x-${command}" in rip "$1" "$2" ;; - x-input) - to_ascii < "$1" - ;; - *) usage 1>&2 exit 1