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