bitreich-tv

meme tv encoding and streaming
git clone git://src.adamsgaard.dk/bitreich-tv
Log | Files | Refs Back to index

brtv-imgs-to-video.sh (1630B)


      1 #!/bin/sh
      2 # read hashtags.txt as stdin, download all images, and convert them to videos
      3 # requirements: hurl(1), ffmpeg(1), convert(1)
      4 
      5 
      6 ### CONFIGURATION START
      7 
      8 # dir to contain images as videos
      9 out_dir="img2vid"
     10 
     11 # ffmpeg flags for generated videos
     12 video_ext="webm"
     13 ffmpeg_codec="-loglevel error -acodec libopus -b:a 96K -vcodec libvpx -b:v 64k -f webm -vf scale=1280:-1 -r 30 -ac 2"
     14 
     15 # target video resolution
     16 video_resolution=1280x720
     17 
     18 # slide style
     19 bgcolor=magenta
     20 
     21 # show image memes for this duration [s]
     22 image_display_time=10
     23 
     24 ### CONFIGURATION END
     25 
     26 
     27 die() {
     28 	printf '%s: error: %s\n' "${0##*/}" "$1" >&2
     29 	exit 1
     30 }
     31 
     32 regeximatch() {
     33 	printf '%s' "$1" | grep -iEq "$2"
     34 }
     35 
     36 fit_img_16_9() {
     37 	convert -resize "$video_resolution"\> -size "$video_resolution" "$1" \
     38 		xc:"$bgcolor" +swap -gravity center -composite "$2"
     39 }
     40 
     41 video_from_img() {
     42 	ffmpeg -y \
     43 		-f lavfi \
     44 		-i anullsrc=r=48000 \
     45 		-i "$1" \
     46 		-t "${image_display_time}" \
     47 		$ffmpeg_codec \
     48 		"$2" < /dev/null
     49 }
     50 
     51 mkdir -p "$out_dir"
     52 
     53 # generate video from each image
     54 # TODO: deal with .gif
     55 while read -r tag url; do
     56 	if ! regeximatch "$url" '\.(jpg|jpeg|png)$'; then
     57 		continue
     58 	fi
     59 
     60 	imgfile="${out_dir}/${url##*/}"
     61 	out="${imgfile%.*}.${video_ext}"
     62 
     63 	if [ ! -f "$out" ]; then
     64 
     65 		if [ ! -f "$imgfile" ]; then
     66 			if ! hurl "$url" > "$imgfile"; then
     67 				die "hurl could not download $url"
     68 			fi
     69 		fi
     70 
     71 		if ! regeximatch "$(file -ib "$imgfile")" "^image\/"; then
     72 			die "input image $imgfile is invalid ($(file -b "$imgfile"))"
     73 		fi
     74 		fit_img_16_9 "$imgfile" "${out%.*}_16-9.jpg"
     75 		video_from_img "${out%.${video_ext}}_16-9.jpg" "${out}"
     76 		printf '%s\n' "$out"
     77 	fi
     78 done