Skip to content

Instantly share code, notes, and snippets.

@bojidar-bg
Last active March 15, 2024 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bojidar-bg/a73df27ce96d34242a95173a54a022fe to your computer and use it in GitHub Desktop.
Save bojidar-bg/a73df27ce96d34242a95173a54a022fe to your computer and use it in GitHub Desktop.
Unsilence w/ MLT
#!/usr/bin/env bash
set -e
if [ "$#" -ne 4 ]; then
echo "Usage: $0 source-file silencedetect-args speedup-factor output-file"
echo " E.g.: $0 file.mkv n=-35dB:d=0.25 2.5 file.mlt"
exit 1;
fi
TIME_REMAP=$(ffmpeg -i "$1" -vn -af silencedetect="$2" -nostats -f null - 2>&1 | awk 'BEGIN {o=i=0;s=0;m='"$3"'} $4 == "silence_start:" {o+=$5-i;i=$5;printf ":%f=%f;",o,i} $4 == "silence_end:" {o+=($5-i)/m;i=$5;printf ":%f=%f;",o,i}')
OUT=$(echo $TIME_REMAP | sed -Ee 's/.+;.//;s/=.+//')
#melt "$1" -consumer xml | sed -E 's|out=".+"|out=":'"$OUT"'"|;s|<producer|<chain|;s|</producer>|<link id="link0"><property name="mlt_service">timeremap</property><property name="pitch">1</property><property name="image_mode">nearest</property><property name="time_map">'"$TIME_REMAP"'</property></link></chain>|' > "$4"
melt -chain "$1" out="$OUT" -link timeremap pitch=1 image_mode=nearest time_map="$TIME_REMAP" -consumer xml:"$4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment