Skip to content

Instantly share code, notes, and snippets.

@weaver299
Created March 26, 2020 03:43
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 weaver299/a773df126383d472eac398f2a7577a15 to your computer and use it in GitHub Desktop.
Save weaver299/a773df126383d472eac398f2a7577a15 to your computer and use it in GitHub Desktop.
ffmpeg bash functions
function ffclip {
# some links about calculating durations in bash
# https://stackoverflow.com/questions/8903239/how-to-calculate-time-difference-in-bash-script
# http://giantdorks.org/alain/script-to-calculate-clip-duration-from-startstop-times/
# http://giantdorks.org/alain/bash-script-to-calculate-duration-between-two-time-stamps-and-also-report-minutes-hours-days-weeks-months-and-years/
# http://www.daveeddy.com/2014/06/29/human-readable-duration-in-bash/
# https://gist.github.com/bezhermoso/2d61cf44646c2f07c0820d81c644c1de
if [ "$#" -lt 3 ]; then
echo "Usage: ffclip <file> <start> <finish>";
return;
fi
colons1="${2//[^:]}"; # count colons
colons2="${3//[^:]}";
START_TIME=$2;
if [ "${#colons1}" -gt 1 ]; then
SEC1=`date +%s -ud $START_TIME`;
else
SEC1=`date +%s -ud 00:$START_TIME`;
fi
#echo "SEC1: $SEC1";
if [ "${#colons2}" -gt 1 ]; then
SEC2=`date +%s -ud $3`;
else
SEC2=`date +%s -ud 00:$3`;
fi
#echo "SEC2: $SEC2";
#echo "expr ${SEC2} - ${SEC1}";
DIFF=`expr ${SEC2} - ${SEC1}`;
#echo "DIFF: $DIFF";
DURATION=`date +%H:%M:%S -ud @${DIFF}`;
#echo "DURATION: $DURATION";
BASE=${1%.*};
EXTN=${1##*.};
NEW_NAME=$BASE-edit.$EXTN;
echo "ffmpeg -ss $START_TIME -i \"$1\" -t $DURATION -c copy \"$NEW_NAME\"";
ffmpeg -hide_banner -ss $START_TIME -i "$1" -t $DURATION -c copy "$NEW_NAME"
}
ffclip_h264() {
if [ "$#" -lt 3 ]; then
echo "Usage: ffclip <file> <start> <finish>";
return;
fi;
colons1="${2//[^:]}";
colons2="${3//[^:]}";
START_TIME=$2;
if [ "${#colons1}" -gt 1 ]; then
SEC1=`gdate +%s -ud $START_TIME`;
else
SEC1=`gdate +%s -ud 00:$START_TIME`;
fi;
if [ "${#colons2}" -gt 1 ]; then
SEC2=`gdate +%s -ud $3`;
else
SEC2=`gdate +%s -ud 00:$3`;
fi;
DIFF=`expr ${SEC2} - ${SEC1}`;
DURATION=`gdate +%H:%M:%S -ud @${DIFF}`;
BASE=${1%.*};
EXTN=${1##*.};
NEW_NAME=$BASE-edit.mp4;
echo "ffmpeg -ss $START_TIME -i \"$1\" -t $DURATION -c copy \"$NEW_NAME\"";
ffmpeg -hide_banner -ss $START_TIME -i "$1" -t $DURATION -c:v libx264 -preset fast -f mp4 "$NEW_NAME"
}
h265() {
if [[ -z $1 ]]; then
echo; echo "Usage: $0 <file> [crf#]";
echo; echo;
exit 1;
fi;
if [[ -z $2 ]]; then
CRF='25';
else
CRF=$2;
fi;
BASE=${1%.*};
EXTN=${1##*.};
NEW_NAME="$BASE-h265-crf${CRF}.mp4";
ffmpeg -hide_banner -i "$1" -vcodec libx265 -crf $CRF -max_muxing_queue_size 1024 -f mp4 "$NEW_NAME";
touch -r "$1" "$NEW_NAME";
}
h264() {
if [[ -z $1 ]]; then
echo; echo "Usage: $0 <file> [preset]";
echo; echo;
exit 1;
fi;
if [[ -z $2 ]]; then
PRESET='fast';
else
PRESET=$2;
fi;
BASE=${1%.*};
EXTN=${1##*.};
NEW_NAME="$BASE-h264-$PRESET.mp4";
ffmpeg -hide_banner -i $1 -c:v libx264 -preset $PRESET -f mp4 "$NEW_NAME";
touch -r "$1" "$NEW_NAME";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment