portability, NONUMBER SEPARATOR and VERBOSITY variables, numbers from 00
This commit is contained in:
		
							parent
							
								
									824bbc7d6f
								
							
						
					
					
						commit
						8d25557220
					
				
					 1 changed files with 89 additions and 50 deletions
				
			
		
							
								
								
									
										139
									
								
								get-tracks.sh
									
										
									
									
									
								
							
							
						
						
									
										139
									
								
								get-tracks.sh
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -18,23 +18,23 @@ reverse_word_order(){
 | 
			
		|||
 | 
			
		||||
# bc is mandatory: arythmetic operations are very limited in ash.
 | 
			
		||||
get_seconds(){
 | 
			
		||||
	local n=0
 | 
			
		||||
	local v=0
 | 
			
		||||
	number_position=0
 | 
			
		||||
	value=0
 | 
			
		||||
 | 
			
		||||
	values=$(echo "$*" | sed 's/:/\
 | 
			
		||||
/g' | sed "s/^0//")
 | 
			
		||||
	for i in $(reverse_word_order $values); do
 | 
			
		||||
		case $n in
 | 
			
		||||
			0) v=$(echo "$v +         $i " | bc);;
 | 
			
		||||
			1) v=$(echo "$v + (60   * $i)" | bc);;
 | 
			
		||||
			2) v=$(echo "$v + (3600 * $i)" | bc);;
 | 
			
		||||
		case $number_position in
 | 
			
		||||
			0) value=$(echo "$value +         $i " | bc);;
 | 
			
		||||
			1) value=$(echo "$value + (60   * $i)" | bc);;
 | 
			
		||||
			2) value=$(echo "$value + (3600 * $i)" | bc);;
 | 
			
		||||
			*) echo "invalid timecode $*"; exit 1;;
 | 
			
		||||
		esac
 | 
			
		||||
 | 
			
		||||
		n=$((n+1))
 | 
			
		||||
		number_position=$((number_position+1))
 | 
			
		||||
	done
 | 
			
		||||
 | 
			
		||||
	echo $v
 | 
			
		||||
	echo $value
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Get a more usable time representation for the beginning and the end of songs.
 | 
			
		||||
| 
						 | 
				
			
			@ -44,66 +44,80 @@ get_values(){
 | 
			
		|||
	time_file="$2"
 | 
			
		||||
	total_length=$(soxi -D "${audio_file}" | sed "s/\..*//") # integer values only
 | 
			
		||||
 | 
			
		||||
	n=0
 | 
			
		||||
	track_number=1
 | 
			
		||||
 | 
			
		||||
	while read X; do
 | 
			
		||||
 | 
			
		||||
		if [ $n -ne 0 ]; then
 | 
			
		||||
			to=$(get_time $X)
 | 
			
		||||
			to_s=$(get_seconds $to)
 | 
			
		||||
			echo -e "$from_s\t$to_s\t$title"
 | 
			
		||||
		if [ $track_number -ne 1 ]; then
 | 
			
		||||
			to_unformatted=$(get_time $X)
 | 
			
		||||
			to_s=$(get_seconds $to_unformatted)
 | 
			
		||||
			echo "$from_s	$to_s	$title"
 | 
			
		||||
		fi
 | 
			
		||||
 | 
			
		||||
		#echo $X
 | 
			
		||||
		from=$(get_time $X)
 | 
			
		||||
		from_s=$(get_seconds $from)
 | 
			
		||||
		from_unformatted=$(get_time $X)
 | 
			
		||||
		from_s=$(get_seconds $from_unformatted)
 | 
			
		||||
		title=$(get_title $X)
 | 
			
		||||
 | 
			
		||||
		if [ -z "${WITH_NUMBER}" ]; then
 | 
			
		||||
			:
 | 
			
		||||
		else
 | 
			
		||||
			title="${n}${WITH_NUMBER}${title}"
 | 
			
		||||
		if [ "$NONUMBER" = "" ]; then
 | 
			
		||||
			track_n=$track_number
 | 
			
		||||
			if [ $track_number -lt 10 ]; then
 | 
			
		||||
				track_n="0$track_number"
 | 
			
		||||
			fi
 | 
			
		||||
			title="${track_n}${SEPARATOR}${title}"
 | 
			
		||||
		fi
 | 
			
		||||
 | 
			
		||||
		n=$(echo $n + 1 | bc)
 | 
			
		||||
		track_number=$(echo $track_number + 1 | bc)
 | 
			
		||||
	done < "${time_file}"
 | 
			
		||||
 | 
			
		||||
	echo -e "$from_s\t$total_length\t$title"
 | 
			
		||||
	echo "$from_s	$total_length	$title"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
run_ffmpeg(){
 | 
			
		||||
	local file=$1
 | 
			
		||||
	local from=$2
 | 
			
		||||
	local duration=$3
 | 
			
		||||
	local title=$4
 | 
			
		||||
	file=$1
 | 
			
		||||
	from=$2
 | 
			
		||||
	duration=$3
 | 
			
		||||
	final_title=$4
 | 
			
		||||
 | 
			
		||||
	if [ "${SIMULATION}" = 1 ]; then
 | 
			
		||||
		[ -z "${QUIET}" ] && echo "ffmpeg -loglevel error -ss '$from' -t '$duration' -i '${file}' '${title}'"
 | 
			
		||||
	else
 | 
			
		||||
		[ -z "${QUIET}" ] && echo "ffmpeg -loglevel error -ss '$from' -t '$duration' -i '${file}' '${title}'"
 | 
			
		||||
		$(< /dev/null ffmpeg -loglevel quiet -ss "$from" -t "$duration" -i "${file}" "${title}")
 | 
			
		||||
	LOG_LEVEL="-loglevel error"
 | 
			
		||||
	FROM="-ss $from"
 | 
			
		||||
	DURATION="-t $duration"
 | 
			
		||||
	INPUT_FILE="-i $file"
 | 
			
		||||
	OUTPUT_FILE="$final_title"
 | 
			
		||||
 | 
			
		||||
	case "v$VERBOSITY" in
 | 
			
		||||
		v0)
 | 
			
		||||
			;;
 | 
			
		||||
		v1)
 | 
			
		||||
			echo "extracting '$final_title'"
 | 
			
		||||
			;;
 | 
			
		||||
		v2)
 | 
			
		||||
			echo "ffmpeg $LOG_LEVEL $FROM $DURATION $INPUT_FILE '$OUTPUT_FILE'"
 | 
			
		||||
			;;
 | 
			
		||||
		*)
 | 
			
		||||
			echo "verbosity is not set properly" >&2
 | 
			
		||||
			exit 1
 | 
			
		||||
			;;
 | 
			
		||||
	esac
 | 
			
		||||
 | 
			
		||||
	if [ "$SIMULATION" = "" ]; then
 | 
			
		||||
		$(< /dev/null ffmpeg $LOG_LEVEL $FROM $DURATION $INPUT_FILE "$OUTPUT_FILE")
 | 
			
		||||
	fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
rip(){
 | 
			
		||||
	n=0
 | 
			
		||||
	from=0
 | 
			
		||||
	to=0
 | 
			
		||||
	track_start_s=0
 | 
			
		||||
	track_end_s=0
 | 
			
		||||
 | 
			
		||||
	audio_file="$1"
 | 
			
		||||
	time_file="$2"
 | 
			
		||||
 | 
			
		||||
	[ "$FORMAT" = "" ] && echo "default format: opus" && FORMAT="opus"
 | 
			
		||||
 | 
			
		||||
	#echo "from	to	duration	title"
 | 
			
		||||
	get_values "$audio_file" "$time_file" | while read LINE; do
 | 
			
		||||
		from=$(echo $LINE | cut -d ' ' -f 1)
 | 
			
		||||
		to=$(echo $LINE | cut -d ' ' -f 2)
 | 
			
		||||
		title=$(echo $LINE | cut -d ' ' -f 3-)
 | 
			
		||||
		duration=$(echo "$to - $from" | bc)
 | 
			
		||||
		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-)
 | 
			
		||||
		track_duration=$(echo "$track_end_s - $track_start_s" | bc)
 | 
			
		||||
 | 
			
		||||
		run_ffmpeg "${audio_file}" "${from}" "${duration}" "${title}.${FORMAT}"
 | 
			
		||||
		n=$((n + 1))
 | 
			
		||||
		run_ffmpeg "${audio_file}" "${track_start_s}" "${track_duration}" "${track_title}.${FORMAT}"
 | 
			
		||||
	done
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -115,11 +129,15 @@ usage(){
 | 
			
		|||
	echo "song-list line format example: 1:30 My second track of the playlist"
 | 
			
		||||
	echo "show output format: start end title"
 | 
			
		||||
	echo
 | 
			
		||||
	echo "envvar: SIMULATION [0|1] (do not invoke ffmpeg)"
 | 
			
		||||
	echo "envvar: FORMAT [mp3,ogg,opus,…] (see the ffmpeg documentation)"
 | 
			
		||||
	echo "envvar: WITH_NUMBER [separator] (not null = write song number, with this separator)"
 | 
			
		||||
	echo "        example: WITH_NUMBER=_ Song names will be 1_song.opus 2_song.opus…"
 | 
			
		||||
	echo "envvar: QUIET (if set to any value, ffmpeg commands are not displayed)"
 | 
			
		||||
	echo "envvar: SIMULATION, if non empty, do not invoke ffmpeg"
 | 
			
		||||
	echo "envvar: NONUMBER, if non empty, do not write song number"
 | 
			
		||||
	echo "envvar: FORMAT [mp3,ogg,opus,…], see the ffmpeg documentation"
 | 
			
		||||
	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"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
if [ $# -lt 1 ]; then
 | 
			
		||||
| 
						 | 
				
			
			@ -130,12 +148,32 @@ fi
 | 
			
		|||
command=$1
 | 
			
		||||
shift
 | 
			
		||||
 | 
			
		||||
if [ "$FORMAT" = "" ]; then
 | 
			
		||||
	echo "default FORMAT: opus"
 | 
			
		||||
	FORMAT="opus"
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if [ "$VERBOSITY" = "" ]; then
 | 
			
		||||
	echo "default VERBOSITY: 1"
 | 
			
		||||
	VERBOSITY=1
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Assume that there should be a separator.
 | 
			
		||||
if [ "$SEPARATOR" = "" ]; then
 | 
			
		||||
	echo "default SEPARATOR: ' - '"
 | 
			
		||||
	SEPARATOR=" - "
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if [ "$SIMULATION" != "" ]; then
 | 
			
		||||
	echo "SIMULATION envvar is set: this is a simulation."
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
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"
 | 
			
		||||
			echo "Usage: $0 show music-file time-stamps-file" >&2
 | 
			
		||||
			exit 1
 | 
			
		||||
		fi
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -146,12 +184,13 @@ case "x-${command}" in
 | 
			
		|||
 | 
			
		||||
		# Takes the audio file in first parameter
 | 
			
		||||
		if [ $# -ne 2 ]; then
 | 
			
		||||
			echo "Usage: $0 show music-file time-stamps-file"
 | 
			
		||||
			echo "Usage: $0 show music-file time-stamps-file" >&2
 | 
			
		||||
			exit 1
 | 
			
		||||
		fi
 | 
			
		||||
 | 
			
		||||
		rip "$1" "$2"
 | 
			
		||||
		;;
 | 
			
		||||
 | 
			
		||||
	*)
 | 
			
		||||
		usage 1>&2
 | 
			
		||||
		exit 1
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue