diff options
author | Sacha Chua <sacha@sachachua.com> | 2023-10-17 11:59:28 -0400 |
---|---|---|
committer | Sacha Chua <sacha@sachachua.com> | 2023-10-17 11:59:28 -0400 |
commit | 0963815d6bb6e9cf01b95aa05c3b34a3d6fcaa22 (patch) | |
tree | 4fa0feaa28cd551bc2e2e9a98a92abb6895a25ce | |
parent | d51d86133dbd93bbf6203fdb1683a23aa40beae6 (diff) | |
download | emacsconf-ansible-0963815d6bb6e9cf01b95aa05c3b34a3d6fcaa22.tar.xz emacsconf-ansible-0963815d6bb6e9cf01b95aa05c3b34a3d6fcaa22.zip |
add prerec scripts
-rw-r--r-- | roles/prerec/tasks/main.yml | 10 | ||||
-rw-r--r-- | roles/prerec/templates/Makefile | 13 | ||||
-rwxr-xr-x | roles/prerec/templates/mux-subs.sh | 24 | ||||
-rwxr-xr-x | roles/prerec/templates/remux.sh | 36 | ||||
-rwxr-xr-x | roles/prerec/templates/rename-original.sh | 15 | ||||
-rwxr-xr-x | roles/prerec/templates/verify-main.sh | 115 |
6 files changed, 211 insertions, 2 deletions
diff --git a/roles/prerec/tasks/main.yml b/roles/prerec/tasks/main.yml index da62340..8144e28 100644 --- a/roles/prerec/tasks/main.yml +++ b/roles/prerec/tasks/main.yml @@ -59,3 +59,13 @@ - reencode.sh - run-aeneas.sh - rename-original.sh + - mux-subs.sh + - verify-main.sh + - remux.sh +- name: Copy Makefile + template: + src: Makefile + dest: "{{ emacsconf_caption_dir }}/cache/Makefile" + owner: "{{ emacsconf_user }}" + group: "{{ emacsconf_group }}" + diff --git a/roles/prerec/templates/Makefile b/roles/prerec/templates/Makefile new file mode 100644 index 0000000..53b49be --- /dev/null +++ b/roles/prerec/templates/Makefile @@ -0,0 +1,13 @@ +# {{ ansible_managed }} + +PRERECS_FINAL := $(wildcard {{ emacsconf_id }}-*--final.webm) +PRERECS_MAIN := $(patsubst %--final.webm, %--main.webm, $(PRERECS_FINAL)) + +.PHONY: all +all: $(PRERECS_MAIN) $(PRERECS_FINAL) + +emacsconf-%--main.webm: {{ emacsconf_id }}-%--final.webm + cp "$<" "$@" + +emacsconf-%--final.webm: {{ emacsconf_id }}-%--main.vtt + mux-subs.sh "$@" "$<" diff --git a/roles/prerec/templates/mux-subs.sh b/roles/prerec/templates/mux-subs.sh new file mode 100755 index 0000000..87702ad --- /dev/null +++ b/roles/prerec/templates/mux-subs.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# {{ ansible_managed }} +# Mixes in captions +# Usage: scripts/mux-subs.sh $video $vtt + +set -euo pipefail + +cleanup() { + if [[ -v tmp_file ]]; then + rm -f -- "$tmp_file" + fi +} +trap cleanup EXIT + +vid="$1" +sub="$2" + +if head -1 "$sub" | grep -q -- captioned; then + tmp_file="$(basename "$vid" ".webm")-new.webm" + ffmpeg -y -i "$vid" -i "$sub" -c copy -metadata:s:s:0 language=eng -disposition:s:s:0 default "$tmp_file" && \ + mv "$tmp_file" "$vid" +else + touch -m "$vid" +fi diff --git a/roles/prerec/templates/remux.sh b/roles/prerec/templates/remux.sh new file mode 100755 index 0000000..c378133 --- /dev/null +++ b/roles/prerec/templates/remux.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# {{ ansible_managed }} +# Mix in the normalized audio +# Usage: remux.sh $input_video + +with_suffix() { + echo "$input_video" | sed "s/--\(reencoded\|original\).webm\$/--$1/" +} + +input_video="$1" +input_audio="$(with_suffix "normalized.opus")" +output_video="$(with_suffix "final.webm")" +main_video="$(with_suffix "main.webm")" +main_subs="$(with_suffix "main.vtt")" + +command="$(cat<<EOF +ffmpeg -i "$input_video" -i "$input_audio" -c:v copy -c:a copy -map 0:v:0 -map 1:a:0 "$output_video" && + cp "$output_video" "$main_video" && + if [[ -f "$main_subs" ]]; then touch -m "$main_subs"; fi +EOF +)" + +printf "input: %s\ncomputed output: %s\nrelated main: %s\n\n" "$input_video" "$output_video" "$main_video" +printf "Produced incantation:\n%s\n\n" "$command" + +while true; +do + read -r -p "Run it? y/n " -n 1 -r response + if [[ $response =~ ^([yY])$ ]]; then + eval "$command" + exit 0 + else + printf "\nExiting\n" + exit 3 + fi +done diff --git a/roles/prerec/templates/rename-original.sh b/roles/prerec/templates/rename-original.sh index 110d39a..0499ae8 100755 --- a/roles/prerec/templates/rename-original.sh +++ b/roles/prerec/templates/rename-original.sh @@ -1,13 +1,24 @@ #!/bin/bash # {{ ansible_managed }} +# Usage: rename-original.sh $slug $file [$extra] [$talks-json] SLUG=$1 FILE=$2 -TALKS_JSON=${3:-~/current/talks.json} +TALKS_JSON=${4:-~/current/talks.json} +EXTRA="" +if [ -z ${3-unset} ]; then + EXTRA="" +elif [ -n "$3" ]; then + EXTRA="--$3" +elif echo "$FILE" | grep -e '\(webm\|mp4\|mov\)'; then + EXTRA="--original" +fi filename=$(basename -- "$FILE") extension="${filename##*.}" filename="${filename%.*}" FILE_PREFIX=$(jq -r '.talks[] | select(.slug=="'$SLUG'")["file-prefix"]' < $TALKS_JSON) -mv "$FILE" $FILE_PREFIX--original.$extension +mv "$FILE" $FILE_PREFIX$EXTRA.$extension +echo $FILE_PREFIX$EXTRA.$extension +# Copy to original if needed if [ -f $FILE_PREFIX--original.webm ] && [ ! -f $FILE_PREFIX--main.$extension ]; then cp $FILE_PREFIX--original.$extension $FILE_PREFIX--main.webm fi diff --git a/roles/prerec/templates/verify-main.sh b/roles/prerec/templates/verify-main.sh new file mode 100755 index 0000000..541e0b6 --- /dev/null +++ b/roles/prerec/templates/verify-main.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env bash +# {{ ansible_managed }} +# Checks that files have the most up-to-date --main.webm. +# Usage: scripts/verify-main.sh + +set -euo pipefail + +log() { + printf "%b\n" "$1" +} + +color_red="\e[31m" +color_green="\e[32m" +color_white="\e[0m" + +log2() { + log "${color_green}$1${color_white}" +} + +log3() { + log "${color_red}$1${color_white}" +} + +plural () { + word="$1" + number="$2" + if [ "$number" = 1 ] || [ "$number" = -1 ]; then + echo "${word}" + else + case $1 in + "has" ) + echo "have" + ;; + "needs" ) + echo "need" + ;; + * ) + echo "${word}s" + ;; + esac + fi +} + +updated=0 +not_updated=0 + +with_suffix() { + echo "$file" | sed "s/--main.webm/--$1.webm/" +} + +verify() { + type="$1" + candidate="$(with_suffix "$type")" + if [ -f "$candidate" ]; then + if [[ "$hash" == "$(md5 "$candidate")" ]]; then + # Nothing to fix for this file + return 0 + else + log2 "${type^} version available for $id" + command="rm \"$file\"; cp \"$candidate\" \"$file\" " + fix "$command" + fi + else + if [[ "$type" == "final" ]]; then + log3 "${type^} version missing for $id" + fi + # Stop looking at this file + return 1 + fi +} + +fix() { + command="$1" + # printf "Command to fix it:\n%s\n" "$command" + while true; + do + read -r -p "Update? y/n " -n 1 -r response + if [[ $response =~ ^([yY])$ ]]; then + eval "$command" + printf "\n" + updated=$((updated+1)) + return 0 + else + printf "\n" + not_updated=$((not_updated+1)) + return 0 + fi + done +} + +md5() { + md5sum "$1" | awk '{ print $1 };' +} + +log "Looking for prerecs to update..." + +for file in *--main.webm; do + id="$(echo "$file" | awk -F"-" '{ print $3 };')" + hash="$(md5 "$file")" + + if verify "final" || verify "reencoded" || verify "original"; then + continue + fi +done + +if [[ "$updated" -gt 0 ]] || [[ "$not_updated" -gt 0 ]]; then + if [[ "$updated" -gt 0 ]]; then + log "$updated $(plural "prerec" $updated) $(plural "has" $updated) been updated" + fi + if [[ "$not_updated" -gt 0 ]]; then + log "$not_updated $(plural "prerec" $not_updated) $(plural "needs" $not_updated) updating" + fi +else + log "Nothing to update!" +fi |