Skip to content

Instantly share code, notes, and snippets.

@shellheim
Last active March 7, 2024 12:26
Show Gist options
  • Save shellheim/1f53efb6a5055d6b62d4055bb96b44ed to your computer and use it in GitHub Desktop.
Save shellheim/1f53efb6a5055d6b62d4055bb96b44ed to your computer and use it in GitHub Desktop.
Trim audio files interactively. This script asks for a start and end time and cuts the specified part out and deleted the old file.
#!/bin/bash
echo "This program clips audio files"
read -r -e -p "Enter the song name: " song
read -r -e -p "Start: " start
# Color variables
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # Clear the text after colored text
# Print echo function
color_echo() {
local colored_text="${2}${1}${NC}"
echo -e "$colored_text"
}
# Removes backslashes, single quotes, and double quotes from the song name
read -r -e -p "End: " end
song="${song//\\/}" # Backslash
song="${song//\'/}" # Single Quote
song="${song//\"/}" # Double Quote
# Checks if the song file exists
if [ ! -f "$song" ]; then
color_echo "Song file not found: $song" "${RED}"
exit 1
fi
tempSong="${song%.*}_backup.${song##*.}" # Assign temporary song another name
ffmpeg -i "$song" -ss "$start" -to "$end" "$tempSong" && rm "$song" && mv "$tempSong" "$song"
status="$?"
if [ $status -eq 0 ]; then
color_echo "Clipping Completed for $song" "${GREEN}"
else
color_echo "Clipping Falied for $song" "${RED}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment