Add trim-audio and add-audio-to-video scripts

This commit is contained in:
Michael Campagnaro 2023-08-02 15:11:25 -04:00
parent e3812590a2
commit d7977096a7
4 changed files with 116 additions and 6 deletions

View File

@ -0,0 +1,58 @@
#!/usr/bin/env bash
# Re-encodes the video with a new primary audio channel.
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
vid_filename=$(basename -- "$1")
audio_filename=$(basename -- "$2")
output_name="$3"
if [[ $vid_filename == "" || $audio_filename == "" || $output_name == "" ]]; then
printf "${BOLD}${RED}Usage: add-audio-to-video <video filename> <audio filename> <output name>${NORMAL}\n"
exit 1
fi
vid_extension="${vid_filename##*.}"
vid_filename="${vid_filename%.*}"
audio_extension="${audio_filename##*.}"
audio_filename="${audio_filename%.*}"
output="${output_name}.$vid_extension"
audio_encoding_opts="-c:a copy"
if [[ $audio_extension == "wav" ]]; then
# Re-encode the audio.
audio_encoding_opts="-c:a aac -b:a 192k"
fi
printf "\n${YELLOW}${BOLD}Adding audio '$audio_filename.$audio_extension' to '$vid_filename.$vid_extension' | output: $output${NORMAL}\n"
# -shortest handles the inputs not being the same duration. Conversion will stop when the shortest input's end is reached.
# Alternatively you can loop with "-stream_loop -1"
ffmpeg -y -stats -loglevel level+error -i "$vid_filename.$vid_extension" -i "$audio_filename.$audio_extension" -c:v copy -map 0:v:0 -map 1:a:0 $audio_encoding_opts -shortest "$output"
printf "\n${GREEN}${BOLD}Finished adding audio${NORMAL}\n\n"

51
dotfiles/bin/trim-audio Normal file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env bash
# Re-encodes the audio to get a more accurate seek time.
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=$(basename -- "$1")
output_name="$2"
start_time="$3"
end_time="$4"
if [[ $filename == "" || $output_name == "" || $start_time == "" || $end_time == "" ]]; then
printf "${BOLD}${RED}Usage: trim-audio <filename> <output name> <start time HH:MM:SS> <end time HH:MM:SS>${NORMAL}\n"
exit 1
fi
extension="${filename##*.}"
filename="${filename%.*}"
output="${output_name}.$extension"
timing_args="-ss $start_time -to $end_time"
printf "\n${YELLOW}${BOLD}Trimming '$filename.$extension' | output: $output | start: $start_time | end: $end_time${NORMAL}\n"
# You might have issues if the file has multiple video streams or embedded subtitles. The -map 0 arg is typically given
# when copying a video stream, but I'm not sure if it's appropriate to use here.
# Trying out async to keep video and audio synced.
ffmpeg -y -stats -loglevel level+error $timing_args -accurate_seek -async 1 -i "$filename.$extension" -c copy "$output"
printf "\n${GREEN}${BOLD}Finished trimming${NORMAL}\n\n"

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Re-encodes the video to get a more accurate timeline. If you want fast trimming at the expense of accuracy then use trim-video-fast. # Re-encodes the video to get a more accurate seek time. If you want fast trimming at the expense of accuracy then use trim-video-fast.
if which tput >/dev/null 2>&1; then if which tput >/dev/null 2>&1; then
ncolors=$(tput colors) ncolors=$(tput colors)
@ -44,7 +44,8 @@ printf "\n${YELLOW}${BOLD}Trimming '$filename.$extension' | output: $output | st
# You might have issues if the file has multiple video streams or embedded subtitles. The -map 0 arg is typically given # You might have issues if the file has multiple video streams or embedded subtitles. The -map 0 arg is typically given
# when copying a video stream, but I'm not sure if it's appropriate to use here. # when copying a video stream, but I'm not sure if it's appropriate to use here.
ffmpeg -y -stats -loglevel level+error $timing_args -accurate_seek -i "$filename.$extension" -c:v libx264 -c:a copy "$output" # Trying out async to keep video and audio synced.
ffmpeg -y -stats -loglevel level+error $timing_args -accurate_seek -async 1 -i "$filename.$extension" -c:v libx264 -c:a copy "$output"
printf "\n${GREEN}${BOLD}Finished trimming${NORMAL}\n\n" printf "\n${GREEN}${BOLD}Finished trimming${NORMAL}\n\n"

View File

@ -1,9 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# A fast video trim that might result in some weird playback cursor timings, # A fast video trim that might result in some weird seek timings, e.g. video
# e.g. video player might show negative seconds at the start of the vid or the # player might show negative seconds at the start of the vid or the playback
# playback time might oscillate a bit. Use trim-video for accurate trimming at # time might oscillate a bit. Use trim-video for accurate trimming at the cost
# the cost of a much slower processing time. # of a much slower processing time.
if which tput >/dev/null 2>&1; then if which tput >/dev/null 2>&1; then
ncolors=$(tput colors) ncolors=$(tput colors)