Skip to content

Instantly share code, notes, and snippets.

@davidrichards
Last active December 7, 2021 02:00
Show Gist options
  • Save davidrichards/938ce56d208f1c2bbf487c338903661b to your computer and use it in GitHub Desktop.
Save davidrichards/938ce56d208f1c2bbf487c338903661b to your computer and use it in GitHub Desktop.
Collecting ffmpeg snippets that work for me and converting them to reusable scripts.
#!/usr/bin/env bash
set -e
gain=${3-"1"}
if [ "$#" -lt 2 ]; then
echo "Usage: $0 input.aifc output.aifc [gain=2]"
exit 1
else
ffmpeg -i $1 -filter_complex acompressor=level_in=$gain $2 -y
exit 0
fi
#!/usr/bin/env bash
set -e
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
-h|--help)
helping=YES
shift # past argument
;;
-x|--suffix)
suffix="$2"
shift # past argument
;;
-s|--searchpath)
search_path="$2"
shift # past argument
;;
-g|--gain)
gain="$2"
shift # past argument
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
# if [[ "YES" -eq $helping ]]; then
# echo "Usage: $0 [-x|--suffix _c] [-s|--searchpath ./*] [-g|--gain 2] [-h|--help]"
# exit 1
# else
suffix=${suffix-"_c"}
search_path=${search_path-"./*"}
gain=${gain-"2"}
echo
echo "Converting files in search_path: ${search_path}, appending suffix: ${suffix}, with gain: ${gain}."
echo
for fullfile in $search_path; do
# filename=$(basename "$fullfile")
extension="${fullfile##*.}"
filename="${fullfile%.*}"
target_filename="$filename$suffix.$extension"
command="ffmpeg -i $fullfile -filter_complex acompressor=level_in=$gain $target_filename -y"
echo "Executing: $command"
echo
$command
# echo $target_filename
done
exit 0
# fi
#!/usr/bin/env bash
set -e
prefix=${3-"img_"}
suffix=${4-"jpg"}
if [ "$#" -lt 2 ]; then
echo "Usage: $0 frame_rate output.mp4 [prefix=img_] [suffix=jpg]"
exit 1
else
ffmpeg -framerate $1 -i $prefix%03d.$suffix -c:v libx264 -r 30 -pix_fmt yuv420p $2
exit 0
fi
#!/usr/bin/env bash
set -e
if [ "$#" -ne 3 ]; then
echo "Usage: $0 input.mp4 output_directory n"
exit 1
else
mkdir $2
ffmpeg -i $1 -vf "select=not(mod(n\,$3))" -vsync vfr -q:v 2 $2/img_%03d.jpg
exit 0
fi
#!/usr/bin/env bash
set -e
if [ "$#" -ne 2 ]; then
echo "Usage: $0 input output.mp3"
exit 1
else
ffmpeg -i $1 -acodec libmp3lame -ab 128k $2
exit 0
fi
#!/usr/bin/env bash
set -e
if [ "$#" -ne 2 ]; then
echo "Usage: $0 input.gif output.mp4"
exit 1
else
ffmpeg -i $1 -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" $2
exit 0
fi
#!/bin/sh
# Control the start and duration
# start_time=2:00
# duration=45
palette="/tmp/palette.png"
# Larger format
filters="fps=15,scale=640:-1:flags=lanczos"
# Test version
# filters="fps=15,scale=320:-1:flags=lanczos"
# Higher quality with the trim filter
# filters="trim=start_frame=12:end_frame=431,fps=15,scale=320:-1:flags=lanczos"
ffmpeg -v warning -i $1 -vf "$filters,palettegen" -y $palette
ffmpeg -v warning -i $1 -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y $2
# Use the start and duration
# ffmpeg -v warning -ss $start_time -t $duration -i $1 -vf "$filters,palettegen" -y $palette
# ffmpeg -v warning -ss $start_time -t $duration -i $1 -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y $2
#!/usr/bin/env bash
set -e
if [ "$#" -ne 2 ]; then
echo "Usage: $0 input.m4a output.mp3"
exit 1
else
ffmpeg -i $1 -acodec libmp3lame -ab 128k $2
exit 0
fi
#!/usr/bin/env bash
set -e
if [ "$#" -ne 2 ]; then
echo "Usage: $0 input.mkv output.mp3"
exit 1
else
ffmpeg -i $1 -acodec mp3 -vcodec mpeg4 -qscale 0 /tmp/mkv2mp3.avi -y
ffmpeg -i /tmp/mkv2mp3.avi $2
rm /tmp/mkv2mp3.avi
exit 0
fi
#!/usr/bin/env bash
set -e
if [ "$#" -lt 2 ]; then
echo "Usage: $0 input_filename output_filename"
echo " input file is best if compressed with gain=1"
exit 1
else
# Use ffmpeg to get the max volume
max_volume=`ffmpeg -i $1 -af "volumedetect" -f null /dev/null 2>&1 | grep max_volume | awk '{print $5}'`
# Get the amount to increase the volume
gain=`python -c "print($max_volume * -1)"`"dB"
# Normalize the audio
ffmpeg -i $1 -af "volume=$gain" $2
exit 0
fi
#!/usr/bin/env bash
set -e
prefix=${1-"img_"}
suffix=${2-"jpg"}
if [ "-h" == "$1" ]; then
echo "Usage: $0 [prefix=img_] [suffix=jpg]"
exit 1
else
for i in *.$suffix; do
new=$(printf "$1%03d.$suffix" "$a")
mv -- "$i" "$new"
let a=a+1
done
exit 0
fi
#!/usr/bin/env bash
set -e
if [ "$#" -ne 2 ]; then
echo "Usage: $0 input.webm output.mp4"
exit 1
else
ffmpeg -fflags +genpts -i $1 -r 24 $2
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment