Skip to content

Instantly share code, notes, and snippets.

@seb-jones seb-jones/vidterm.sh
Last active Sep 7, 2019

Embed
What would you like to do?
#!/bin/bash
if [[ $# -ne 1 ]]; then
printf "Usage: %s [VIDEO_FILE]\n" $0
exit 1
fi
SOURCE=$1
FRAMERATE=$(ffmpeg -i test.mp4 2>&1 | grep -oE '[0-9]+ fps' | grep -oE '^[0-9]+')
MICROSECONDS_PER_FRAME=$((1000000 / $FRAMERATE))
TEMP_FRAME=/tmp/frame.jpg
TEMP_FRAME_ASCII=/tmp/frame-ascii.txt
SECONDS=0
MINUTES=0
POSITION=0
POSITION_IN_MICROSECONDS=0
function microtime() {
echo $(($(date +%s%N)/1000))
}
function render_frame() {
ffmpeg -y -loglevel quiet -i $SOURCE -r $FRAMERATE -ss "$POSITION" -frames: 1 -s 320x240 $TEMP_FRAME
jp2a -z $TEMP_FRAME > $TEMP_FRAME_ASCII
}
render_frame
# play audio in background
ffplay -hide_banner -loglevel panic -nodisp $SOURCE &
while [[ true ]]; do
cat $TEMP_FRAME_ASCII
START_TIME=$(microtime)
CURRENT_TIME=$(microtime)
ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
render_frame
while [[ ELAPSED_TIME -lt MICROSECONDS_PER_FRAME ]]; do
CURRENT_TIME=$(microtime)
ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
done
POSITION_IN_MICROSECONDS=$((POSITION_IN_MICROSECONDS + ELAPSED_TIME))
POSITION=$( echo "scale=2; ($POSITION_IN_MICROSECONDS) / 1000000.0" | bc -l)
echo $POSITION | grep -q -E "^\."
if [[ $? -eq 0 ]]; then
POSITION="0$POSITION"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.