Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/bin/ari.sh
1677 views
1
#!/bin/bash
2
set -eo pipefail
3
4
GTN_CACHE="$(pwd)/.jekyll-cache/aws-polly/"
5
mkdir -p "$GTN_CACHE"
6
7
# Setup our Engine
8
mozilla_port=$(ss -lptn 'sport = :5002' | wc -l)
9
if [[ -n "$AWS_ACCESS_KEY_ID" ]]; then
10
engine="aws"
11
echo "Using AWS for speech (this will cost money!)"
12
elif (( mozilla_port > 1 )); then
13
engine="mozilla"
14
echo "Using MozillaTTS for speech"
15
else
16
echo "Cannot run, no speech engine setup. "
17
echo "If you're testing, try: docker run -it -p 5002:5002 synesthesiam/mozillatts"
18
exit 1
19
fi
20
21
# Setup a temporary directory for building our project.
22
build_dir=$(mktemp -d /tmp/gtn-ari.XXXXXXXXXX)
23
echo "Building in $build_dir"
24
25
# Setup inputs
26
if [ "$#" -ne 3 ]; then
27
echo "Error, expecting 3 parameters: slides_PDF slides_source mp4_output"
28
exit 1
29
fi
30
31
slides=$1 # e.g. _site/training-material/topic/admin/tutorials/ansible/slides.pdf
32
source=$2 # e.g. topic/admin/tutorials/ansible/slides.html
33
output=$3 # e.g. _site/training-material/topic/admin/tutorials/ansible/slides.mp4
34
slidesbase=$(basename "$source" .html)
35
if [[ "$slidesbase" == *"_ES" ]]; then
36
lang=es
37
language=esp
38
else
39
lang=en
40
language=eng
41
fi
42
43
subtitles="$(dirname "$output")"/"$(basename "$output" .mp4)".${lang}.vtt
44
cover="$output".png
45
srcdir="$(dirname "$source")"
46
47
# Metadata
48
ruby bin/extract-frontmatter.rb "${source}" | jq '.contributors | join(", ")' > /dev/null || ec=$?
49
if (( ec == 0 )); then
50
meta_authors="$(ruby bin/extract-frontmatter.rb "${source}" | jq '.contributors | join(", ")' -r)"
51
else
52
meta_authors="$(ruby bin/extract-frontmatter.rb "${source}" | jq '.contributions.authorship | join(", ")' -r)"
53
fi
54
55
meta_title="$(ruby bin/extract-frontmatter.rb "${source}" | jq .title -r)"
56
REVISION="$(git log -1 --format=%H)"
57
58
# This is digusting, but the safest way to get a fresh ffmpeg.
59
FFMPEG_PATH=$(echo "const ffmpeg = require('ffmpeg-static');console.log(ffmpeg.split('/').slice(0, -1).join('/'));" | node -)
60
echo "Located FFMPEG at $FFMPEG_PATH"
61
export PATH="$FFMPEG_PATH:$PATH"
62
which ffmpeg
63
64
# We'll cache audio locally.
65
ffmpeglog=warning
66
67
# Setup output files
68
script="${build_dir}/script.json"
69
70
# Generate our script
71
echo " Building Script, Subtitles"
72
echo ruby bin/ari-extract-script.rb "$source"
73
ruby bin/ari-extract-script.rb "$source" > "$script"
74
voice=$(cat "$script" | jq .voice.id)
75
76
# Now explode that into individual lines, synthesize, and re-assemble them into
77
# our editly.json5 script
78
# This will read the environment variable EDITLY_FAST=true and set fast in the script, if requested.
79
ruby bin/ari-prep-script.rb "${build_dir}" "${engine}"
80
81
# Generate images for use.
82
echo " Extracting slides"
83
convert "${slides}" -resize 1920x1080 "${build_dir}/slides.%03d.png"
84
85
echo " Building Video | ./node_modules/.bin/editly --json ${build_dir}/editly.json5"
86
$FFMPEG_PATH/ffmpeg -version
87
./node_modules/.bin/editly --json "${build_dir}/editly.json5"
88
89
# Mux it together
90
echo " Muxing"
91
ffmpeg -loglevel $ffmpeglog -i "${build_dir}/tmp.mp4" -i "${build_dir}/out.srt" \
92
-movflags +faststart \
93
-metadata comment="build-tag:$(date --rfc-3339=seconds)/$REVISION/$USER/$engine/$voice" \
94
-metadata network="Galaxy Training Network"\
95
-metadata artist="$meta_authors, AWS Polly $voice" \
96
-metadata title="$meta_title" \
97
-c:v copy -c:a copy -c:s mov_text \
98
-map 0 -map 1 \
99
-metadata:s:a:0 language=$language \
100
-metadata:s:v:0 language=$language \
101
-metadata:s:s:0 language=$language \
102
-disposition:s:s:0 forced "${build_dir}/out.mp4"
103
104
# Check if output dir needs to be created
105
mkdir -p "$(dirname "$output")"
106
107
# Copy our files over
108
cp "${build_dir}/out.mp4" "$output"
109
cp "${build_dir}/out.vtt" "$subtitles"
110
convert -resize 480x270 "${build_dir}/slides.000.png" "$cover"
111
ffprobe -loglevel warning -show_format -show_private_data -show_streams -print_format json -i "$output" > "$output".json
112
113