noisereduce (2133B)
1 #!/usr/bin/sh 2 3 usage () 4 { 5 printf "Usage : noisereduce <input video file> <output video file>\n" 6 exit 7 } 8 9 # Tests for requirements 10 ifinstalled ffmpeg || { echo >&2 "We require 'ffmpeg' but it's not installed."; exit 1; } 11 ifinstalled sox || { echo >&2 "We require 'ffmpeg' but it's not installed."; exit 1; } 12 13 if [ "$#" -ne 2 ] 14 then 15 usage 16 fi 17 18 if [ ! -e "$1" ] 19 then 20 printf "File not found: %s\n" "$1" 21 exit 22 fi 23 24 if [ -e "$2" ] 25 then 26 printf "File %s already exists, overwrite? [y/N]\n: " "$2" 27 read -r yn 28 case $yn in 29 [Yy]* ) ;; 30 * ) exit;; 31 esac 32 fi 33 34 inBasename=$(basename "$1") 35 inExt="${inBasename##*.}" 36 37 isVideoStr=$(ffprobe -v warning -show_streams "$1" | grep codec_type=video) 38 if [ -n "$isVideoStr" ] 39 then 40 isVideo=1 41 printf "Detected %s as a video file\n" "$inBasename" 42 else 43 isVideo=0 44 printf "Detected %s as an audio file\n" "$inBasename" 45 fi 46 47 printf "Sample noise start time [00:00:00]: " 48 read -r sampleStart 49 if [ -z "$sampleStart" ] ; then sampleStart="00:00:00"; fi 50 printf "Sample noise end time [00:00:00.900]: " 51 read -r sampleEnd 52 if [ -z "$sampleEnd" ] ; then sampleEnd="00:00:00.900"; fi 53 printf "Noise reduction amount [0.21]: " 54 read -r sensitivity 55 if [ -z "$sensitivity" ] ; then sensitivity="0.21"; fi 56 57 58 tmpVidFile="/tmp/noiseclean_tmpvid.$inExt" 59 tmpAudFile="/tmp/noiseclean_tmpaud.wav" 60 noiseAudFile="/tmp/noiseclean_noiseaud.wav" 61 noiseProfFile="/tmp/noiseclean_noise.prof" 62 tmpAudCleanFile="/tmp/noiseclean_tmpaud-clean.wav" 63 64 printf "Cleaning noise on %s...\n" "$1" 65 66 if [ $isVideo -eq "1" ]; then 67 ffmpeg -v warning -y -i "$1" -qscale:v 0 -vcodec copy -an "$tmpVidFile" 68 ffmpeg -v warning -y -i "$1" -qscale:a 0 "$tmpAudFile" 69 else 70 cp "$1" "$tmpAudFile" 71 fi 72 ffmpeg -v warning -y -i "$1" -vn -ss "$sampleStart" -t "$sampleEnd" "$noiseAudFile" 73 sox "$noiseAudFile" -n noiseprof "$noiseProfFile" 74 sox "$tmpAudFile" "$tmpAudCleanFile" noisered "$noiseProfFile" "$sensitivity" 75 if [ $isVideo -eq "1" ]; then 76 ffmpeg -v warning -y -i "$tmpAudCleanFile" -i "$tmpVidFile" -vcodec copy -qscale:v 0 -qscale:a 0 "$2" 77 else 78 cp "$tmpAudCleanFile" "$2" 79 fi 80 81 printf "Done"