Put image->video processing into separate 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 469296142ed571c3e38749a234a9eb31205645ac
DIR parent 9712aec3da3a8dd8e17f8626c138ae12c26e9c8c
HTML Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Sun, 16 Aug 2020 21:33:26 +0200
Put image->video processing into separate script
Diffstat:
A brtv-imgs-to-video.sh | 78 +++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+), 0 deletions(-)
---
DIR diff --git a/brtv-imgs-to-video.sh b/brtv-imgs-to-video.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+# read hashtags.txt as stdin, download all images, and convert them to videos
+# requirements: hurl(1), ffmpeg(1), convert(1)
+
+
+### CONFIGURATION START
+
+# dir to contain images as videos
+out_dir="img2vid"
+
+# 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"
+
+# target video resolution
+video_resolution=1280x720
+
+# slide style
+bgcolor=magenta
+
+# show image memes for this duration [s]
+image_display_time=10
+
+### CONFIGURATION END
+
+
+die() {
+ printf '%s: error: %s\n' "${0##*/}" "$1" >&2
+ exit 1
+}
+
+regeximatch() {
+ printf '%s' "$1" | grep -iEq "$2"
+}
+
+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" < /dev/null
+}
+
+mkdir -p "$out_dir"
+
+# generate video from each image
+# TODO: deal with .gif
+while read -r tag url; do
+ if ! regeximatch "$url" '\.(jpg|jpeg|png)$'; then
+ continue
+ fi
+
+ imgfile="${out_dir}/${url##*/}"
+ out="${imgfile%.*}.${video_ext}"
+
+ if [ ! -f "$out" ]; then
+
+ if [ ! -f "$imgfile" ]; then
+ if ! hurl "$url" > "$imgfile"; then
+ die "hurl could not download $url"
+ fi
+ fi
+
+ if ! regeximatch "$(file -ib "$imgfile")" "^image\/"; then
+ die "input image $imgfile is invalid ($(file -b "$imgfile"))"
+ fi
+ fit_img_16_9 "$imgfile" "${out%.*}_16-9.jpg"
+ video_from_img "${out%.${video_ext}}_16-9.jpg" "${out}"
+ printf '%s\n' "$out"
+ fi
+done