Add preliminary brtv main script - bitreich-tv - Meme TV encoding and streaming HTML git clone git://bitreich.org/bitreich-tv git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/bitreich-tv DIR Log DIR Files DIR Refs DIR Tags DIR LICENSE --- DIR commit 3a8559a5c96734befbfdb580c7e8a2ab491ea6d4 HTML Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Tue, 11 Aug 2020 17:29:44 +0200 Add preliminary brtv main script Diffstat: A brtv-start.sh | 151 +++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+), 0 deletions(-) --- DIR diff --git a/brtv-start.sh b/brtv-start.sh @@ -0,0 +1,151 @@ +#!/bin/sh +# generate title animations and start streaming server + +pass="$(gpg2 -q -d icecastpass.gpg)" + +if [ $# -ne 1 ]; then + printf 'usage: %s <memecache_path>\n' "$0" >&2 + exit +fi + +### CONFIGURATION START + +# intermediate playlist file +playlist="playlist_$(date +%Y-%m-%d)" + +# memecache source dir +memecache="$1" + +# converted memecache dir +memecache_conv="memecache_conv" + +# title animations dir +title="title" + +# ffmpeg flags for generated videos +video_ext="webm" +ffmpeg_codec="-loglevel error -acodec libopus -b:a 96K -vcodec libvpx -b:v 64k -f webm -vf scale=1280:-1 -r 30 -ac 2" +out_video="brtv_$(date +%Y-%m-%d).${video_ext}" +#out_video_duration="24:00:00" +out_video_duration="00:05:00" + +# target video resolution +video_resolution=1280x720 + +# slide style +bgcolor=magenta +fcolor=white + +# show image memes for this duration [s] +image_display_time=10 + +# show title slides for this duration [s] +title_display_time=5 + +### CONFIGURATION END + + +fit_img_16_9() { + convert -resize "$video_resolution"\> -size "$video_resolution" "$1" \ + xc:"$bgcolor" +swap -gravity center -composite "$2" +} + +video_from_img() { + ffmpeg -y \ + -f lavfi \ + -i anullsrc=r=48000 \ + -i "$1" \ + -t "${image_display_time}" \ + $ffmpeg_codec \ + "$2"; +} + +title_slide() { + img="$(basename "${1%.*}".png)" + convert -size "$video_resolution" "xc:${bgcolor}" \ + -pointsize 48 -fill "$fgcolor" \ + -gravity center -draw "text 0,0 '#${img%.*}'" "$img" + ffmpeg -y \ + -f lavfi \ + -i anullsrc=r=48000 \ + -i "$img" \ + -t "${title_display_time}" \ + $ffmpec_codec \ + "$2" && rm "$img" +} + +video_conv() { + ffmpeg -y -i "$1" $ffmpeg_codec -max_muxing_queue_size 1024 "$2" +} + +mkdir -p "$title" +mkdir -p "$memecache_conv" + +# generate video from each image +for f in "$memecache"/*.{jpg,jpeg,JPG,png,PNG}; do + out="${memecache_conv}/$(basename "${f%.*}.${video_ext}")" + if [ -e "$f" ] && [ ! -e "$out" ]; then + printf 'in: %s, out: %s\n' "$f" "$out" + fit_img_16_9 "$f" "${out%.*}.jpg" + video_from_img "${out%.${video_ext}}.jpg" "${out}" + fi +done + +# make animation for fallback stream +if [ ! -f "bitreich-tv.${video_ext}" ]; then + title_slide bitreich-tv bitreich-tv.${video_ext} +fi + + +# make title slides for video and converted images and encode to common codec +for f in "$memecache"/*.mkv \ + "$memecache"/*.mp4 \ + "$memecache"/*.webm \ + "$memecache_conv"/*."${video_ext}"; do + + title_path="${title}/$(basename "${f%.*}.webm")" + [ ! -e "$title_path" ] && title_slide "$f" "$title_path" + + out="${memecache_conv}/$(basename "${f%.*}.webm")" + [ ! -e "$out" ] && video_conv "$f" "$out" +done + +# generate random playlist from converted memes and add title slides +find "$memecache_conv" -maxdepth 1 -name "*.webm" -type f | \ + sort -R | \ + awk -v memecachedir="$memecache" -v titledir="$title" ' + function add_title(s) { + sub(/memecache/, "title", s); + return s + } + {line=$0; + print "file \""add_title($line)"\""; + print "file \""$0"\""}' | sed "s/\"/'/g" > "$playlist" + #print "file \""$0"\""}' | sed "s/\"/'/g" |grep -v title> "$playlist" + +#printf '\nstarting fallback stream\n' +#ffmpeg -loglevel quiet \ +# -stream_loop -1 -i bitreich-tv.webm +# -f webm -codec copy +# icecast://source:${pass}@localhost:8000/brtvfallback + + +# encode daily video file +ffmpeg -n -f concat -safe 0 -i "$playlist" \ + -f "${video_ext}" -cluster_size_limit 2M -cluster_time_limit 5100 -content_type video/webm \ + -analyzeduration 2M -probesize 2M \ + -codec copy \ + -deadline realtime -threads 8 \ + -to "$out_video_duration" + "$video_out" + +exit 0 + +printf '\nstarting stream\n' + +ffmpeg -f concat -safe 0 -stream_loop -1 -i "$video_out" \ + -f webm -cluster_size_limit 2M -cluster_time_limit 5100 -content_type video/webm \ + -analyzeduration 2M -probesize 2M \ + -codec copy \ + -deadline realtime -threads 8 \ + icecast://source:${pass}@adamsgaard.dk:3232/brtv