Last active
May 16, 2020 21:25
-
-
Save oleksabor/0c926afb01d336d13fde963f89ff95f4 to your computer and use it in GitHub Desktop.
bash script to cut mp3 silence at the start and at the end. ffmpeg silencedetect is used
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ "$1" == "" ]; then | |
echo "no input file name"; | |
exit 1; | |
else | |
mp3file="$1"; | |
fi | |
echo "input " $mp3file; | |
echo "output ../$mp3file" | |
ffmpeg -i "$mp3file" -af silencedetect=d=3:n=-45dB -f null - 2>vol.txt | |
while read -r line; do | |
echo "$line" | |
if [ -z "$sEnd" ] && [[ "$line" =~ end:.([0-9]+) ]]; then #only whole seconds are taken | |
sEnd=${BASH_REMATCH[1]}; | |
echo found end $sEnd #first silence end | |
fi | |
if [[ "$line" =~ start:.([0-9.\]+) ]]; then | |
sStart=${BASH_REMATCH[1]} | |
echo found start $sStart #latest silence start | |
fi | |
done < <(grep -o -E "silence_\w*:\s-?[0-9\.]*" vol.txt) | |
sEnd="$(printf '%d' $sEnd 2>/dev/null)" | |
if [ $sEnd -gt 20 ]; then | |
echo silence sStart too big $sEnd | |
$sEnd="" #first silence end is too far away from start, ignoring | |
fi | |
if [ $sEnd -gt 0 ] || [ -n "$sStart" ]; then | |
args=(-i "$mp3file" -vn -acodec copy) | |
if [[ -n "$sEnd" && $sEnd -gt 1 ]]; then args+=(-ss $sEnd); fi | |
if [[ -n "$sStart" ]]; then args+=(-t $sStart); fi | |
args+=("../$mp3file") | |
echo "${args[@]}" | |
ffmpeg "${args[@]}"; | |
echo $sEnd - $sStart | |
else | |
echo nothing to cut | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please feel free to use this script at your own risk.
It takes file name as an argument and tries to detect silence and the begin and at the end using the
ffmpeg -af silencedetect=d=3:n=-45dB
Then it cuts the silence (if it has been found) using the
ffmpeg -vn -acodec copy
command.It does not rebuild the file so quality is not reduced.