#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
ROOT_DIR="${SCRIPT_DIR}/.."
VIDEO_PATH="${ROOT_DIR}/data/akiyo_cif.y4m"
FFMPEG_BIN_PATH="${ROOT_DIR}/projects/FFmpeg/build/bin/ffmpeg"
TIME_STAMP=$(date '+%Y-%m-%d_%H-%M-%S')
LOG_DIR="${ROOT_DIR}/logs"
mkdir -p "$LOG_DIR"
if [ ! -x $FFMPEG_BIN_PATH ]; then
echo "FFMPEG binary does not exist at path: $FFMPEG_BIN_PATH"
exit 1
fi
ffmpegCmd=$FFMPEG_BIN_PATH
maxIterations=5
breakLoop=0
runFFMPEGCommand() {
local startInstant=$(date '+%s')
echo -n "Execution Start Time=$startInstant "
$ffmpegCmd -i "$VIDEO_PATH" -c:v libx264 -preset veryslow -b:v 5M -maxrate 5M -bufsize 15M -threads 4 -x264opts bframes=12:keyint=24:min-keyint=24:nal-hrd=cbr:force-cfr=1:bframes=8:no-scenecut -f null - -benchmark -stats
}
executeMainLoop() {
iterationCount=0
while true; do
[[ $breakLoop -eq 1 ]] && break
runFFMPEGCommand
((iterationCount++))
[[ $iterationCount -ge $maxIterations ]] && breakLoop=1
done
}
export ffmpegCmd
export maxIterations
LOG_FILE="${LOG_DIR}/${BASH_SOURCE[0]##*/}-${TIME_STAMP}.log"
exec > >(tee -ia "$LOG_FILE")
exec 2> >(tee -ia "$LOG_FILE" >&2)
startTime=$(date +%s%3N)
executeMainLoop
endTime=$(date +%s%3N)
executionDuration=$((endTime - startTime))
echo "Script executed in $executionDuration milliseconds."