dotfiles/dotfiles/bin/extract-audio-from-video-and-transcode

66 lines
1.9 KiB
Bash

#!/usr/bin/env bash
# Extracts audio from a video and transcodes it based on the supplied params. If you just want the audio
# from the video as-is then use extract-audio-from-video
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
MAGENTA="$(tput setaf 5)"
CYAN="$(tput setaf 6)"
BOLD="$(tput bold)"
NORMAL="$(tput sgr0)"
else
RED=""
GREEN=""
YELLOW=""
BLUE=""
MAGENTA=""
CYAN=""
BOLD=""
NORMAL=""
fi
filename="$1"
output_name="$2"
sample_rate="$3"
channel_count="$4"
start_time="$5"
end_time="$6"
output_base=$(basename -- "$output_name")
output_extension="${output_base##*.}"
if [[ $filename == "" || $output_extension == "" || $output_extension == $output_base ]]; then
printf "${BOLD}${RED}Usage: extract-audio-from-video-and-transcode <filename> <output name w/ extension> <optional: sample rate. Defaults to 44100> <optional: channel count. Defaults to 2> <optional: start time HH:MM:SS> <optional: end time HH:MM:SS>${NORMAL}\n"
exit 1
fi
if [[ $sample_rate == "" ]]; then
sample_rate="44100"
fi
if [[ $channel_count == "" ]]; then
channel_count="2"
fi
timing_args=""
if [[ $start_time != "" ]]; then
timing_args="-ss $start_time "
fi
if [[ $end_time != "" ]]; then
timing_args+="-to $end_time"
fi
printf "\n${YELLOW}${BOLD}Extracting audio from '$filename' | output: '$output_name' | sample rate: $sample_rate | channels: $channel_count | start: $start_time | end: $end_time${NORMAL}\n"
ffmpeg -y -stats -loglevel level+error $timing_args -accurate_seek -i "$filename" -ar $sample_rate -ac $channel_count -map a "$output_name"
printf "\n${GREEN}${BOLD}Done extracting audio from '$filename' | output '$output_name'${NORMAL}\n\n"