Skip to content

Instantly share code, notes, and snippets.

@crazoter
Created June 19, 2020 15:43
Show Gist options
  • Save crazoter/0b62b71d7dc43cbd105a8ed36f82e60b to your computer and use it in GitHub Desktop.
Save crazoter/0b62b71d7dc43cbd105a8ed36f82e60b to your computer and use it in GitHub Desktop.
#!/bin/bash
# silence_trim.sh - an old script I used a while back to trim silences from videos
# Used on Ubuntu 18.04 LTS
# Can trim videos 1-2hrs long within 10-20 minutes
# Requires ffmpeg, ffprobe (ffprobe comes with ffmpeg)
# Place the video you want to shorten named vid.mp4 into same folder as this script
# Script will split video into several parts with sound and then merge said videos
# It will then clean up and produce a "shortened-vid.mp4"
#
# BUG: There is a setting that makes ffmpeg directly remove silence trimming without re-encoding it;
# this means that although it's faster, the video will appear choppy. Audio is good so it's not bad
# for presentations / lectures but for actual video post-processing this is a no go
#
# Modify silencedetect params if it's not working well
ffmpeg -i vid.mp4 -filter_complex "[0:a]silencedetect=n=-40dB:d=0.3[outa]" -map [outa] -f s16le -y /dev/null |& F='-aq 30 -c copy -v warning' perl -ne 'INIT { $ss=0; $se=0; } if (/silence_start: (\S+)/) { $ss=$1; $ctr+=1; printf "ffmpeg -nostdin -i vid.mp4 -ss %f -t %f $ENV{F} -c copy -y %05d.mkv\n", $se, ($ss-$se), $ctr; } if (/silence_end: (\S+)/) { $se=$1; } END { printf "ffmpeg -nostdin -i vid.mp4 -ss %f $ENV{F} -c copy -y %05d.mkv\n", $se, $ctr+1; }' | bash -x
for f in *.mkv ; do if ! ffprobe -loglevel error "$f" 2>&1 | grep "End of file"; then echo file \'"$f"\' >> list.txt; fi; done
ffmpeg -f concat -safe 0 -i list.txt -c copy shortened-vid.mp4
# Commented out as there were some issues with deleting thousands of files on LTS
# rm ./*mkv
# rm ./list.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment