diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2018-04-25 18:24:53 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2018-04-25 18:24:53 +0200 |
commit | a4d3e987097959b2d29eda1623e726a065c09a31 (patch) | |
tree | da0df1658dcf2b6855ca2777d41a4976e1ad4bb1 /odin/strm | |
parent | 8d43d3eb524e2cffc1c32bc1bc375f5a794e3455 (diff) | |
download | nixos-a4d3e987097959b2d29eda1623e726a065c09a31.tar nixos-a4d3e987097959b2d29eda1623e726a065c09a31.tar.gz nixos-a4d3e987097959b2d29eda1623e726a065c09a31.tar.bz2 nixos-a4d3e987097959b2d29eda1623e726a065c09a31.tar.xz nixos-a4d3e987097959b2d29eda1623e726a065c09a31.zip |
transcode twitch & logging
Diffstat (limited to 'odin/strm')
-rw-r--r-- | odin/strm/download_youtube | 54 |
1 files changed, 24 insertions, 30 deletions
diff --git a/odin/strm/download_youtube b/odin/strm/download_youtube index 1d729fee..357988d4 100644 --- a/odin/strm/download_youtube +++ b/odin/strm/download_youtube | |||
@@ -17,21 +17,13 @@ function notmuch { | |||
17 | done | 17 | done |
18 | } | 18 | } |
19 | 19 | ||
20 | logTag=${0:t} | ||
21 | |||
22 | exec 1> >(logger -t "$logTag" -p news.notice) | ||
23 | exec 2> >(logger -t "$logTag" -p news.error) | ||
24 | |||
25 | debug() { logger -t "$logTag" -p news.debug } | ||
26 | warn() { logger -t "$logTag" -p news.warn } | ||
27 | |||
28 | typeset -a cleanupCmds | 20 | typeset -a cleanupCmds |
29 | cleanupCmds=() | 21 | cleanupCmds=() |
30 | 22 | ||
31 | function doCleanup { | 23 | function doCleanup { |
32 | for cmd (${cleanupCmds}); do | 24 | for cmd (${cleanupCmds}); do |
33 | # print -- ${cmd} | debug | 25 | # print -- ${cmd} |
34 | eval $cmd | debug | 26 | eval $cmd |
35 | done | 27 | done |
36 | } | 28 | } |
37 | 29 | ||
@@ -74,13 +66,13 @@ printf ">>> %s <<<\n %s\n" "${msgId}" "${message}" | |||
74 | msgTags=($(notmuch search --output=tags $msgId )) | 66 | msgTags=($(notmuch search --output=tags $msgId )) |
75 | 67 | ||
76 | if [[ ${msgTags[(i)cached]} -le $#msgTags ]]; then | 68 | if [[ ${msgTags[(i)cached]} -le $#msgTags ]]; then |
77 | print "Message in Cache" | warn | 69 | print "Message in Cache" >&2 |
78 | exit 0 | 70 | exit 0 |
79 | fi | 71 | fi |
80 | 72 | ||
81 | if [[ 'base64' == $(sed '/^Content-Transfer-Encoding: */!d; s///;q' ${message}) ]]; then | 73 | if [[ 'base64' == $(sed '/^Content-Transfer-Encoding: */!d; s///;q' ${message}) ]]; then |
82 | tmpFile=$(mktemp --tmpdir=/home/gkleen/rss/tmp .writeOut.XXXXXX) | 74 | tmpFile=$(mktemp --tmpdir=/home/gkleen/rss/tmp .writeOut.XXXXXX) |
83 | printf "Decoding base64 message content for ‘%s’" ${message} | debug | 75 | printf "Decoding base64 message content for ‘%s’" ${message} |
84 | sed '/^Content-Transfer-Encoding: */d; /^$/q' ${message} >! $tmpFile | 76 | sed '/^Content-Transfer-Encoding: */d; /^$/q' ${message} >! $tmpFile |
85 | sed '1,/^$/d' ${message} | base64 -d >> $tmpFile | 77 | sed '1,/^$/d' ${message} | base64 -d >> $tmpFile |
86 | mv -v $tmpFile ${message} | 78 | mv -v $tmpFile ${message} |
@@ -96,10 +88,10 @@ printf ">>> %s <<<\n %s\n" "${msgId}" "${message}" | |||
96 | sld=$(cut -d '/' -f 3 <<<${url} | rev | cut -d '.' -f 1-2 | rev) | 88 | sld=$(cut -d '/' -f 3 <<<${url} | rev | cut -d '.' -f 1-2 | rev) |
97 | 89 | ||
98 | if [[ -e ${HOME}/.dl-backup ]] && grep -q ${sld} ${HOME}/.dl-backup; then | 90 | if [[ -e ${HOME}/.dl-backup ]] && grep -q ${sld} ${HOME}/.dl-backup; then |
99 | printf "Was told to back up off %s\n" ${sld} | warn | 91 | printf "Was told to back up off %s\n" ${sld} >&2 |
100 | exit 0 | 92 | exit 0 |
101 | else | 93 | else |
102 | printf "Proceeding on %s\n" ${sld} | debug | 94 | printf "Proceeding on %s\n" ${sld} |
103 | fi | 95 | fi |
104 | 96 | ||
105 | dir="/srv/media/youtube" | 97 | dir="/srv/media/youtube" |
@@ -144,15 +136,15 @@ printf ">>> %s <<<\n %s\n" "${msgId}" "${message}" | |||
144 | printf "%s\n%s via %s: %s\nExpecting ‘%s’\n" ${title} ${videoId} ${extractor} ${format} ${filename:t} | 136 | printf "%s\n%s via %s: %s\nExpecting ‘%s’\n" ${title} ${videoId} ${extractor} ${format} ${filename:t} |
145 | 137 | ||
146 | if [[ -n ${extractor} && -n ${videoId} ]]; then | 138 | if [[ -n ${extractor} && -n ${videoId} ]]; then |
147 | printf "Searching for %s on %s in cache...\n" ${videoId} ${extractor} | debug | 139 | printf "Searching for %s on %s in cache...\n" ${videoId} ${extractor} |
148 | 140 | ||
149 | typeset -a messages | 141 | typeset -a messages |
150 | messages=($(notmuch search --output=messages -- from:${extractor} and ${videoId} and is:cached)) | 142 | messages=($(notmuch search --output=messages -- from:${extractor} and ${videoId} and is:cached)) |
151 | 143 | ||
152 | if [[ ${#messages} -ne 0 ]]; then | 144 | if [[ ${#messages} -ne 0 ]]; then |
153 | printf "Video already cached: %s\n" "${messages[*]}" | warn | 145 | printf "Video already cached: %s\n" "${messages[*]}" >&2 |
154 | else | 146 | else |
155 | printf "Video not yet cached\n" | debug | 147 | printf "Video not yet cached\n" |
156 | fi | 148 | fi |
157 | fi | 149 | fi |
158 | 150 | ||
@@ -175,7 +167,7 @@ printf ">>> %s <<<\n %s\n" "${msgId}" "${message}" | |||
175 | done | 167 | done |
176 | fi | 168 | fi |
177 | 169 | ||
178 | [[ -n ${filename} ]] && printf "Found ‘%s’\n" ${filename:t} || debug | 170 | [[ -n ${filename} ]] && printf "Found ‘%s’\n" ${filename:t} |
179 | 171 | ||
180 | # newFilename=${filename:h}/$(mungefilename ${filename:t}) | 172 | # newFilename=${filename:h}/$(mungefilename ${filename:t}) |
181 | # if [[ "${filename}" != "${newFilename}" ]]; then | 173 | # if [[ "${filename}" != "${newFilename}" ]]; then |
@@ -199,17 +191,19 @@ printf ">>> %s <<<\n %s\n" "${msgId}" "${message}" | |||
199 | extensions=("mp4") | 191 | extensions=("mp4") |
200 | fi | 192 | fi |
201 | 193 | ||
202 | if [[ "${bare_amp}" -ne 0 || ${extensions[(i)${filename:e}]} -gt ${#extensions} ]]; then | 194 | typeset -a transcodeExtractors |
203 | printf "Transcoding ‘%s’" ${title} | debug | 195 | transcodeExtractors=("twitch -vod") |
196 | |||
197 | if [[ "${bare_amp}" -ne 0 || ${extensions[(i)${filename:e}]} -gt ${#extensions} || ${transcodeExtractors[(i)${extractor}]} -le ${#transcodeExtractors} ]]; then | ||
198 | printf "Transcoding ‘%s’" ${title} | ||
204 | printf "%d %d %d/%d(%d)…\n" \ | 199 | printf "%d %d %d/%d(%d)…\n" \ |
205 | $([[ $($alwaysTranscode; print $?) -eq 0 ]]; print $?) \ | 200 | $([[ $($alwaysTranscode; print $?) -eq 0 ]]; print $?) \ |
206 | $([[ "${bare_amp}" -ne 0 ]]; print $?) \ | 201 | $([[ "${bare_amp}" -ne 0 ]]; print $?) \ |
207 | ${extensions[(i)${filename:e}]} ${#extensions} \ | 202 | ${extensions[(i)${filename:e}]} ${#extensions} \ |
208 | $([[ ${extensions[(i)${filename:e}]} -gt ${#extensions} ]]; print $?) \ | 203 | $([[ ${extensions[(i)${filename:e}]} -gt ${#extensions} ]]; print $?) |
209 | | debug | ||
210 | tempfile=$(mktemp --tmpdir=${filename:h} .transcode.${filename:t:r}.$$.XXXXXX.${filename:e}) | 204 | tempfile=$(mktemp --tmpdir=${filename:h} .transcode.${filename:t:r}.$$.XXXXXX.${filename:e}) |
211 | cleanup rm -v -- "${tempfile}" | 205 | cleanup rm -v -- "${tempfile}" |
212 | mv -vf "${filename}" "${tempfile}" | debug | 206 | mv -vf "${filename}" "${tempfile}" |
213 | 207 | ||
214 | typeset -A fileInfo | 208 | typeset -A fileInfo |
215 | for line ($(@ffmpeg@/bin/ffprobe -v error -show_format -show_streams -show_entries stream=codec_name,codec_type:format=:stream_tags=:stream_disposition=:format_tags= -of flat=h=0 -- ${tempfile})); do | 209 | for line ($(@ffmpeg@/bin/ffprobe -v error -show_format -show_streams -show_entries stream=codec_name,codec_type:format=:stream_tags=:stream_disposition=:format_tags= -of flat=h=0 -- ${tempfile})); do |
@@ -234,7 +228,7 @@ printf ">>> %s <<<\n %s\n" "${msgId}" "${message}" | |||
234 | local cType=${fileInfo[${formatKey}]} | 228 | local cType=${fileInfo[${formatKey}]} |
235 | local cName=${fileInfo[stream.${i}.codec_name]} | 229 | local cName=${fileInfo[stream.${i}.codec_name]} |
236 | 230 | ||
237 | printf "Stream %d: %s (%s)\n" ${i} ${cName} ${cType} | debug | 231 | printf "Stream %d: %s (%s)\n" ${i} ${cName} ${cType} |
238 | 232 | ||
239 | if [[ ${cType} == "video" ]]; then | 233 | if [[ ${cType} == "video" ]]; then |
240 | if [[ ${ext} == "mkv" ]]; then | 234 | if [[ ${ext} == "mkv" ]]; then |
@@ -289,22 +283,22 @@ printf ">>> %s <<<\n %s\n" "${msgId}" "${message}" | |||
289 | cleanup rm -rfv -- ${PWD} | 283 | cleanup rm -rfv -- ${PWD} |
290 | 284 | ||
291 | p1args+=(-an -f matroska) | 285 | p1args+=(-an -f matroska) |
292 | print -- ${prePass} ${p1args} | debug | 286 | print -- ${prePass} ${p1args} |
293 | ffmpeg ${args} ${prePass} ${p1args} -- /dev/null | 287 | ffmpeg ${args} ${prePass} ${p1args} -- /dev/null |
294 | 288 | ||
295 | print -- ${cargs} ${p2args} | debug | 289 | print -- ${cargs} ${p2args} |
296 | ffmpeg ${args} ${cargs} ${p2args} -- "${filename}" | 290 | ffmpeg ${args} ${cargs} ${p2args} -- "${filename}" |
297 | 291 | ||
298 | cd ${oldPwd} | 292 | cd ${oldPwd} |
299 | else | 293 | else |
300 | args+=(-v warning -i pipe:0) | 294 | args+=(-v warning -i pipe:0) |
301 | 295 | ||
302 | print -- ${cargs} ${p2args} | debug | 296 | print -- ${cargs} ${p2args} |
303 | pv -N "$(trimName "trans:" ${title})" ${tempfile} | ffmpeg ${args} ${cargs} ${p2args} -- "${filename}" | 297 | pv -N "$(trimName "trans:" ${title})" ${tempfile} | ffmpeg ${args} ${cargs} ${p2args} -- "${filename}" |
304 | fi | 298 | fi |
305 | fi | 299 | fi |
306 | 300 | ||
307 | chmod -v 644 "${filename}" | debug | 301 | chmod -v 644 "${filename}" |
308 | tmpFile=$(mktemp --tmpdir=/home/gkleen/rss/tmp .insertUrl.$$.XXXXXX) | 302 | tmpFile=$(mktemp --tmpdir=/home/gkleen/rss/tmp .insertUrl.$$.XXXXXX) |
309 | relUrl=$(realpath --relative-to=/srv/media ${filename}) | 303 | relUrl=$(realpath --relative-to=/srv/media ${filename}) |
310 | typeset -a relUrlComponents | 304 | typeset -a relUrlComponents |
@@ -315,7 +309,7 @@ printf ">>> %s <<<\n %s\n" "${msgId}" "${message}" | |||
315 | relUrl+=$(sed -f ${HOME}/url_escape.sed <<<${urlPiece}) | 309 | relUrl+=$(sed -f ${HOME}/url_escape.sed <<<${urlPiece}) |
316 | done | 310 | done |
317 | awk -v "link=http://odin.asgard.yggdrasil/${relUrl}" '{ if (r == 0) { r = gsub("href=\"[^\"]+\"", "href=\"" link "\""); }; print; }' "${message}" >! $tmpFile | 311 | awk -v "link=http://odin.asgard.yggdrasil/${relUrl}" '{ if (r == 0) { r = gsub("href=\"[^\"]+\"", "href=\"" link "\""); }; print; }' "${message}" >! $tmpFile |
318 | mv -v $tmpFile $message | debug | 312 | mv -v $tmpFile $message |
319 | notmuch tag '+cached' -- $msgId && printf "Tagged ‘%s’ as 'cached'" ${msgId} | 313 | notmuch tag '+cached' -- $msgId && printf "Tagged ‘%s’ as 'cached'" ${msgId} |
320 | 314 | ||
321 | if [[ -n "$(notmuch search "tag:inbox AND $msgId")" ]]; then | 315 | if [[ -n "$(notmuch search "tag:inbox AND $msgId")" ]]; then |
@@ -328,7 +322,7 @@ printf ">>> %s <<<\n %s\n" "${msgId}" "${message}" | |||
328 | fi | 322 | fi |
329 | systemctl --no-block start uucico@hel.service | 323 | systemctl --no-block start uucico@hel.service |
330 | else | 324 | else |
331 | print "Message vanished from inbox" | warn | 325 | print "Message vanished from inbox" >&2 |
332 | fi | 326 | fi |
333 | else | 327 | else |
334 | printf "An error occured while downloading video at ‘%s’ (exitcode: %d)\n" ${url} ${ytResult} >&2 | 328 | printf "An error occured while downloading video at ‘%s’ (exitcode: %d)\n" ${url} ${ytResult} >&2 |