diff options
author | Sacha Chua <sacha@sachachua.com> | 2022-10-25 11:13:23 -0400 |
---|---|---|
committer | Sacha Chua <sacha@sachachua.com> | 2022-10-25 11:13:23 -0400 |
commit | e42e761dca58557799084ceafdf2088d85fe40c5 (patch) | |
tree | 7a88d305b303b763b7ad8c849b0902b3068634bd /roles/caption/templates | |
parent | 765036da2fc05fd45693bec60ce911b01636e9be (diff) | |
download | emacsconf-ansible-e42e761dca58557799084ceafdf2088d85fe40c5.tar.xz emacsconf-ansible-e42e761dca58557799084ceafdf2088d85fe40c5.zip |
Caption daemon
Diffstat (limited to '')
-rwxr-xr-x | roles/caption/templates/captions.init.d | 78 | ||||
-rwxr-xr-x | roles/caption/templates/inotify-process-captions.sh | 15 | ||||
-rwxr-xr-x | roles/caption/templates/process-captions.py | 46 |
3 files changed, 118 insertions, 21 deletions
diff --git a/roles/caption/templates/captions.init.d b/roles/caption/templates/captions.init.d new file mode 100755 index 0000000..5e1c9f2 --- /dev/null +++ b/roles/caption/templates/captions.init.d @@ -0,0 +1,78 @@ +#!/bin/sh +# {{ ansible_managed }} + +### BEGIN INIT INFO +# Provides: captions +# Required-Start: $local_fs $remote_fs $network $syslog +# Required-Stop: $local_fs $remote_fs $network $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: starts captioning monitor +# Description: starts captioning monitor using start-stop-daemon +### END INIT INFO + +PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin" +USER="{{ emacsconf_user }}" +GROUP="{{ emacsconf_group }}" +DESC="EmacsConf captions" +NAME="captions" +WORKDIR="{{ emacsconf_caption_dir }}" + +set -e + +. /lib/lsb/init-functions + +start() { + echo "Starting $DESC... " + + start-stop-daemon --start --chuid "$USER:$GROUP" -d $WORKDIR --background --make-pidfile --pidfile /var/run/$NAME.pid --exec "$WORKDIR/inotify-process-captions.sh > /var/log/$NAME.log" || true + echo "done" +} + +#We need this function to ensure the whole process tree will be killed +killtree() { + local _pid=$1 + local _sig=${2-TERM} + for _child in $(ps -o pid --no-headers --ppid ${_pid}); do + killtree ${_child} ${_sig} + done + kill -${_sig} ${_pid} +} + +stop() { + echo "Stopping $DESC... " + if test -f /var/run/$NAME.pid; then + while test -d /proc/$(cat /var/run/$NAME.pid); do + killtree $(cat /var/run/$NAME.pid) 15 + sleep 0.5 + done + rm /var/run/$NAME.pid + fi + echo "done" +} + +status() { + status_of_proc -p /var/run/$NAME.pid "" "$NAME" && exit 0 || exit $? +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + stop + start + ;; + status) + status + ;; + *) + echo "Usage: $NAME {start|stop|restart|status}" >&2 + exit 1 + ;; +esac + +exit 0 diff --git a/roles/caption/templates/inotify-process-captions.sh b/roles/caption/templates/inotify-process-captions.sh new file mode 100755 index 0000000..ce5f416 --- /dev/null +++ b/roles/caption/templates/inotify-process-captions.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# {{ ansible_managed }} + +# This script waits for new webm and mov files +# in the current directory (recursively) and then calls +# process-captions to process all the new files that need work. +inotifywait -r -m "{{ emacsconf_caption_dir }}" -e create -e moved_to | + while read directory action file; do + if [[ "$file" =~ .*(webm|mov)$ ]]; then + "{{ emacsconf_caption_dir }}"/process-captions.py | tee -a "{{ emacsconf_caption_dir }}/captions.log" + elif [[ "$file" =~ .*(vtt|srv2|ogg|opus)$ ]]; then + # Copy to backstage area + rsync --ignore-existing $directory/$file orga@media.emacsconf.org:/var/www/media.emacsconf.org/{{ emacsconf_year }}/backstage/ + fi + done diff --git a/roles/caption/templates/process-captions.py b/roles/caption/templates/process-captions.py index 6ad890a..72e9ad2 100755 --- a/roles/caption/templates/process-captions.py +++ b/roles/caption/templates/process-captions.py @@ -46,7 +46,8 @@ AUDIO_REGEXP = '\.(ogg|opus)$' ALWAYS = False TRIM_AUDIO = False MODEL = os.environ.get('MODEL', 'large') # Set to tiny for testing -JSON_FILE = '/data/emacsconf/2022/talks.json' +WORK_DIR = "{{ emacsconf_caption_dir }}" +JSON_FILE = os.path.join(WORK_DIR, 'talks.json') def get_slug_from_filename(filename): m = re.search('emacsconf-[0-9]+-([a-z]+)--', filename) @@ -100,14 +101,14 @@ def get_files_to_work_on(directory): return needs_work def extract_audio(work): - output = subprocess.check_output(['ffprobe', video_file], stderr=subprocess.STDOUT) + output = subprocess.check_output(['ffprobe', work['video']], stderr=subprocess.STDOUT) extension = 'opus' if 'Audio: vorbis' in output.decode(): extension = 'ogg' - new_file = os.path.join(os.path.dirname(video_file), base_name(video_file) + '.' + extension) - acodec = 'copy' if re.search('webm$', video_file) else 'libopus' - log("Extracting audio from %s acodec %s" % (video_file, acodec)) - output = subprocess.check_output(['ffmpeg', '-y', '-i', video_file, '-acodec', acodec, '-vn', new_file], stderr=subprocess.STDOUT) + new_file = work['base'] + '.' + extension + acodec = 'copy' if re.search('webm$', work['video']) else 'libopus' + log("Extracting audio from %s acodec %s" % (work['video'], acodec)) + output = subprocess.check_output(['ffmpeg', '-y', '-i', work['video'], '-acodec', acodec, '-vn', new_file], stderr=subprocess.STDOUT) work['audio'] = new_file return work @@ -213,19 +214,22 @@ def base_name(s): # assert(base_name('/home/sachac/current/sqlite/emacsconf-2022-sqlite--using-sqlite-as-a-data-source-a-framework-and-an-example--andrew-hyatt--normalized.webm.vtt') == 'emacsconf-2022-sqlite--using-sqlite-as-a-data-source-a-framework-and-an-example--andrew-hyatt--main') log(f"MODEL {MODEL} ALWAYS {ALWAYS} TRIM_AUDIO {TRIM_AUDIO}") -directory = sys.argv[1] if len(sys.argv) > 1 else "~/current" +directory = sys.argv[1] if len(sys.argv) > 1 else WORK_DIR + needs_work = get_files_to_work_on(directory) -if THREADS > 0: - torch.set_num_threads(THREADS) -for work in needs_work: - log("Started processing %s" % work['base']) - if work['audio']: - if ALWAYS or not 'vtt' in work: - work = generate_captions(work) - if ALWAYS or not 'srv2' in work: - work = generate_srv2(work) -# print("Aligning words", audio_file, datetime.datetime.now()) -# word_cuts = align_words(cuts) -# convert_cuts_to_word_timing(audio_file, word_cuts) - log("Done %s" % str(work['base'])) - +if len(needs_work) > 0: + if THREADS > 0: + torch.set_num_threads(THREADS) + for work in needs_work: + log("Started processing %s" % work['base']) + if work['audio']: + if ALWAYS or not 'vtt' in work: + work = generate_captions(work) + if ALWAYS or not 'srv2' in work: + work = generate_srv2(work) + # print("Aligning words", audio_file, datetime.datetime.now()) + # word_cuts = align_words(cuts) + # convert_cuts_to_word_timing(audio_file, word_cuts) + log("Done %s" % str(work['base'])) +else: + log("No work needed.") |