1
0
Fork 0
dotfiles/.local/bin/trash/noisereduce

82 lines
2.0 KiB
Plaintext
Raw Normal View History

2022-07-04 21:36:33 +02:00
#!/usr/bin/sh
2023-02-07 17:19:53 +01:00
usage() {
printf "Usage : noisereduce <input video file> <output video file>\n"
exit
2022-07-04 21:36:33 +02:00
}
# Tests for requirements
2023-02-07 17:19:53 +01:00
ifinstalled ffmpeg || {
echo >&2 "We require 'ffmpeg' but it's not installed."
exit 1
}
ifinstalled sox || {
echo >&2 "We require 'ffmpeg' but it's not installed."
exit 1
}
2022-07-04 21:36:33 +02:00
2023-02-07 17:19:53 +01:00
if [ "$#" -ne 2 ]; then
usage
2022-07-04 21:36:33 +02:00
fi
2023-02-07 17:19:53 +01:00
if [ ! -e "$1" ]; then
printf "File not found: %s\n" "$1"
exit
2022-07-04 21:36:33 +02:00
fi
2023-02-07 17:19:53 +01:00
if [ -e "$2" ]; then
printf "File %s already exists, overwrite? [y/N]\n: " "$2"
read -r yn
case $yn in
[Yy]*) ;;
*) exit ;;
esac
2022-07-04 21:36:33 +02:00
fi
inBasename=$(basename "$1")
inExt="${inBasename##*.}"
isVideoStr=$(ffprobe -v warning -show_streams "$1" | grep codec_type=video)
2023-02-07 17:19:53 +01:00
if [ -n "$isVideoStr" ]; then
isVideo=1
printf "Detected %s as a video file\n" "$inBasename"
2022-07-04 21:36:33 +02:00
else
2023-02-07 17:19:53 +01:00
isVideo=0
printf "Detected %s as an audio file\n" "$inBasename"
2022-07-04 21:36:33 +02:00
fi
printf "Sample noise start time [00:00:00]: "
read -r sampleStart
2023-02-07 17:19:53 +01:00
if [ -z "$sampleStart" ]; then sampleStart="00:00:00"; fi
2022-07-04 21:36:33 +02:00
printf "Sample noise end time [00:00:00.900]: "
read -r sampleEnd
2023-02-07 17:19:53 +01:00
if [ -z "$sampleEnd" ]; then sampleEnd="00:00:00.900"; fi
2022-07-04 21:36:33 +02:00
printf "Noise reduction amount [0.21]: "
read -r sensitivity
2023-02-07 17:19:53 +01:00
if [ -z "$sensitivity" ]; then sensitivity="0.21"; fi
2022-07-04 21:36:33 +02:00
tmpVidFile="/tmp/noiseclean_tmpvid.$inExt"
tmpAudFile="/tmp/noiseclean_tmpaud.wav"
noiseAudFile="/tmp/noiseclean_noiseaud.wav"
noiseProfFile="/tmp/noiseclean_noise.prof"
tmpAudCleanFile="/tmp/noiseclean_tmpaud-clean.wav"
printf "Cleaning noise on %s...\n" "$1"
if [ $isVideo -eq "1" ]; then
2023-02-07 17:19:53 +01:00
ffmpeg -v warning -y -i "$1" -qscale:v 0 -vcodec copy -an "$tmpVidFile"
ffmpeg -v warning -y -i "$1" -qscale:a 0 "$tmpAudFile"
2022-07-04 21:36:33 +02:00
else
2023-02-07 17:19:53 +01:00
cp "$1" "$tmpAudFile"
2022-07-04 21:36:33 +02:00
fi
ffmpeg -v warning -y -i "$1" -vn -ss "$sampleStart" -t "$sampleEnd" "$noiseAudFile"
sox "$noiseAudFile" -n noiseprof "$noiseProfFile"
sox "$tmpAudFile" "$tmpAudCleanFile" noisered "$noiseProfFile" "$sensitivity"
if [ $isVideo -eq "1" ]; then
2023-02-07 17:19:53 +01:00
ffmpeg -v warning -y -i "$tmpAudCleanFile" -i "$tmpVidFile" -vcodec copy -qscale:v 0 -qscale:a 0 "$2"
2022-07-04 21:36:33 +02:00
else
2023-02-07 17:19:53 +01:00
cp "$tmpAudCleanFile" "$2"
2022-07-04 21:36:33 +02:00
fi
printf "Done"