Last active
February 26, 2023 16:36
-
-
Save xcsrz/c45928dd7e7b47a9308bcec76f84746a to your computer and use it in GitHub Desktop.
Add a gentle fade-in from black to the beginning and a matching fade-out to the end of a video clip
This file contains hidden or 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 | |
destfile() { | |
dir=$(dirname "$1") | |
filename=$(basename -- "$1"); | |
extension="${filename##*.}"; | |
basename="${filename%.*}"; | |
filepath="${dir}/${basename}-faded.${extension}"; | |
c=0 | |
while [ -f "$filepath" ]; do | |
c=$((c+1)) | |
filepath="${dir}/${basename}-faded-${c}.${extension}"; | |
done | |
echo "${filepath}"; | |
} | |
SRCFILE=$1 | |
FADEIN=${2:-'1.5'} | |
FADEOUT=${3:-$FADEIN} | |
if [ "${SRCFILE}" == "" ]; then | |
echo "You must provide a source file"; | |
exit; | |
fi | |
if [ ! -f "${SRCFILE}" ]; then | |
echo "${SRCFILE} does not exists."; | |
exit; | |
fi | |
echo "reading from ${SRCFILE}"; | |
OUTFILE=$(destfile "${SRCFILE}"); | |
echo "writing to ${OUTFILE}" | |
LENGTH=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${SRCFILE}") | |
STARTFADEOUT=$(echo "${LENGTH} ${FADEOUT}" | awk '{ print $1-$2 }') | |
ffmpeg -i "${SRCFILE}" -vf "fade=t=in:st=0:d=${FADEIN},fade=t=out:st=${STARTFADEOUT}:d=${FADEOUT}" "${OUTFILE}" | |
echo "done writing ${OUTFILE}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment