Skip to content

Instantly share code, notes, and snippets.

@yukkerike
Last active September 6, 2024 09:47
Show Gist options
  • Save yukkerike/b5e8a5da656ff17526bb2e9dbb3080de to your computer and use it in GitHub Desktop.
Save yukkerike/b5e8a5da656ff17526bb2e9dbb3080de to your computer and use it in GitHub Desktop.
Trim silent parts in video using ffmpeg and fish shell.
### ffmpeg helpers for fish shell
### ffmpeg хелперы для оболочки fish
## source this file in your fish config file
## добавьте `source trim.fish` куда-то в ваш fish.config
# compress video and convert to mp4
# сжать видео и конвертировать в mp4
function ffshrink
for target in $argv
ffmpeg -hide_banner -y -i "$target" -preset medium -crf 26 -vf format=yuv420p -c:v h264 "$target.out.mp4"
if string match -q "*.mp4" "$target"
mv "$target.out.mp4" "$target"
end
end
end
# remove silent parts from video
# удалить тихие части из видео
# credits: https://github.com/bambax/Remsi
function fftrim
for file in $argv
echo "Finding silence in file: $file"
set -l ffmpeg_output (ffmpeg -i "$file" -af silencedetect=n=-30dB:d=1 -f null - 2>&1 | string collect)
string match -raq "silence_end: ((?<starts>[\.0-9]*))" $ffmpeg_output
string match -raq "silence_start: ((?<ends>[\.0-9]*))" $ffmpeg_output
set -p starts 0
set -l filter
for i in (seq (count $ends))
set -a filter "between(t,$starts[$i],$ends[$i])"
end
set -l filter (string join + $filter)
echo "This filter will be applied: $filter"
ffmpeg -y -i "$file" -vf "select='$filter',setpts=N/FRAME_RATE/TB,format=yuv420p" -af "aselect='$filter',asetpts=N/SR/TB" -preset medium -crf 26 "$file.trim.mp4"
end
end
# convert audio to mp3 that's compatible with vk.com audio messages
# сконвертировать аудио в mp3, совместимый с аудиосообщениями вконтакте
function ffvk
for target in $argv
ffmpeg -hide_banner -y -i "$target" -q:a 4 -ac 1 "$target.mp3"
if string match -q "*.mp3" "$target"
mv "$target.mp3" "$target"
end
end
end
# convert audio to opus ogg that's compatible with telegram audio messages
# сконвертировать аудио в opus ogg, совместимый с аудиосообщениями telegram
# credits: https://github.com/vysheng/tg/issues/420#issuecomment-1272116366
function fftg
for target in $argv
ffmpeg -hide_banner -y -i "$target" -vn -c:a libopus "$target.ogg"
if string match -q "*.ogg" "$target"
mv "$target.ogg" "$target"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment