Skip to content

Instantly share code, notes, and snippets.

@seb-jones seb-jones/vidterm.sh

Last active Jul 26, 2020
Embed
What would you like to do?
#!/bin/bash
if [[ $# -ne 1 ]]; then
printf "Usage: %s VIDEO_FILE\n" $0
exit 1
fi
source_file=$1
framerate=$(ffmpeg -i "$source_file" 2>&1 | grep -oE '[0-9]+ fps' | grep -oE '^[0-9]+')
temp_dir=$(mktemp --directory)
microseconds_per_frame=$((1000000 / $framerate))
temp_frame="$temp_dir/frame.jpg"
temp_frame_ascii="$temp_dir/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_file" -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_file" &
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.