User Tools

Site Tools


public:ffmpeg-based_interval_timer_for_sport_and_exercise

This is an old revision of the document!


FFmpeg-based Interval Timer for Sport and Exercise

I unfortunately am the one of the cycling captain of UEA Triathlon Club for the academic year of 2017-2018. Possibly due to having low IQ, I have problem with keeping track of the set in a turbo session. I used to use a Matlab-based countdown timer 1). However it always crashes at 42 seconds, possibly due to the fact that it is “The Almighty Answer to the Meaning of Life, the Universe, and Everything” 2). I cannot be bothered to fix the code in that Matlab countdown timer.

After digging through FFmpeg documentation 3) and the Internet 4), I figured out how to generate a text-based video which can be played on any video player that is compatible with H.264 5).

Here I present the program listing, and an example output.

Program listing

#!/bin/bash
 
# The file number counter
let counter=0
 
function cleanup {
    rm seg-*.mp4 filelist.txt
}
 
# calling convention:duration text
function segment {
    let min=$1/60
    let sec=$1-${min}*60
    ffmpeg -f lavfi -i color=c=white:size=1280x720:rate=30 \
    -vf "drawtext=text='${2}': fontsize=80: \
    r=30: x=(w-tw)/2: y=h-h*0.75: fontcolor=black:,\
    drawtext=text='Duration: ${min} min ${sec} sec': fontsize=80: \
    r=30: x=(w-tw)/2: y=h-h*0.5: fontcolor=black:,
    drawtext=timecode='00\:00\:00\:00': fontsize=80: \
    r=30: x=(w-tw)/2: y=h-h*0.25: fontcolor=black:" \
    -t ${1} -preset slow -tune stillimage -y \
    seg-${counter}.mp4
    echo "file seg-${counter}.mp4" >> filelist.txt
    let counter++
}
 
function output {
    ffmpeg -f concat -i filelist.txt -c copy ${1}
    cleanup
}
 
cleanup
 
segment 300 "Easy, gradually build up"
segment 600 "10 mile TT effort"
segment 15  "Sprint"
segment 600 "Easy"
segment 600 "10 mile TT effort"
segment 20  "Sprint"
segment 600 "Easy"
segment 600 "10 mile TT effort"
segment 25  "Sprint"
segment 300 "Cool down"
 
output UEA-Triathlon-2018-02-07.mp4

Example output

public/ffmpeg-based_interval_timer_for_sport_and_exercise.1517968029.txt.gz · Last modified: 2018/03/31 00:38 (external edit)