summaryrefslogtreecommitdiff
path: root/odin/strm/download_youtube
diff options
context:
space:
mode:
Diffstat (limited to 'odin/strm/download_youtube')
-rw-r--r--odin/strm/download_youtube241
1 files changed, 124 insertions, 117 deletions
diff --git a/odin/strm/download_youtube b/odin/strm/download_youtube
index 4024155a..4e9eb2de 100644
--- a/odin/strm/download_youtube
+++ b/odin/strm/download_youtube
@@ -174,137 +174,144 @@ printf ">>> %s <<<\n %s\n" "${msgId}" "${message}"
174 # mv -v ${filename} ${newFilename} && filename=${newFilename} 174 # mv -v ${filename} ${newFilename} && filename=${newFilename}
175 # fi 175 # fi
176 176
177 ffmpegResult=0
177 if [[ -n "${filename}" && -e "${filename}" && ${ytResult} -eq 0 ]]; then 178 if [[ -n "${filename}" && -e "${filename}" && ${ytResult} -eq 0 ]]; then
178 max_vol=$( \ 179 max_vol=$( \
179 pv -N "$(trimName "vol:" ${title})" ${filename} | ffmpeg -i pipe:0 -af "volumedetect" -vn -f null /dev/null 2>&1 | \ 180 pv -N "$(trimName "vol:" ${title})" ${filename} | ffmpeg -i pipe:0 -af "volumedetect" -vn -f null /dev/null 2>&1 | \
180 grep 'max_volume' | sed -r 's/^.*max_volume: ([-0-9\.]+) dB$/\1/' 181 grep 'max_volume' | sed -r 's/^.*max_volume: ([-0-9\.]+) dB$/\1/'
181 ) 182 )
182 printf "Maximum volume: %.2fdB\n" "${max_vol}" 183 ffmpegResult=${pipestatus[2]}
183 [[ -n "${max_vol}" ]] || max_vol=0 184 printf "Maximum volume: %.2fdB\n" "${max_vol}"
184 bare_amp=$(printf "%.2f" $(($max_vol * (-1)))) 185 [[ -n "${max_vol}" ]] || max_vol=0
185 amp=$(printf "volume=%sdB" "${bare_amp}") 186 bare_amp=$(printf "%.2f" $(($max_vol * (-1))))
186 187 amp=$(printf "volume=%sdB" "${bare_amp}")
187 typeset -a extensions 188
188 extensions=("mkv" "mp3" "mp4" "webm") 189 typeset -a extensions
189 190 extensions=("mkv" "mp3" "mp4" "webm")
190 if [[ ${msgTags[(i)tv]} -le $#msgTags ]]; then 191
191 extensions=("mp4") 192 if [[ ${msgTags[(i)tv]} -le $#msgTags ]]; then
192 fi 193 extensions=("mp4")
194 fi
195
196 typeset -a transcodeExtractors
197 transcodeExtractors=()
198
199 if [[ "${bare_amp}" -ne 0
200 || ${extensions[(i)${filename:e}]} -gt ${#extensions}
201 || ${transcodeExtractors[(i)${extractor}]} -le ${#transcodeExtractors}
202 ]]; then
203 printf "Transcoding ‘%s’" ${title}
204 printf "%d %d %d/%d(%d)…\n" \
205 $([[ $($alwaysTranscode; print $?) -eq 0 ]]; print $?) \
206 $([[ "${bare_amp}" -ne 0 ]]; print $?) \
207 ${extensions[(i)${filename:e}]} ${#extensions} \
208 $([[ ${extensions[(i)${filename:e}]} -gt ${#extensions} ]]; print $?)
209 tempfile=$(mktemp --tmpdir=${filename:h} .transcode.${filename:t:r}.$$.XXXXXX.${filename:e})
210 cleanup rm -v -- "${tempfile}"
211 mv -vf "${filename}" "${tempfile}"
212
213 typeset -A fileInfo
214 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}; ffmpegResult=$((ffmpegResult || $?)))); do
215 fileInfo[${line%=*}]=${(Q)line#*=}
216 done
193 217
194 typeset -a transcodeExtractors 218 ext=${extensions[1]}
195 transcodeExtractors=() 219 filename=${filename%.*}.${ext}
196 220 typeset -a args
197 if [[ "${bare_amp}" -ne 0 221 args=(-y)
198 || ${extensions[(i)${filename:e}]} -gt ${#extensions} 222 typeset -a prePass
199 || ${transcodeExtractors[(i)${extractor}]} -le ${#transcodeExtractors} 223 prePass=()
200 ]]; then 224 typeset -a cargs
201 printf "Transcoding ‘%s’" ${title} 225 cargs=()
202 printf "%d %d %d/%d(%d)…\n" \ 226 typeset -a p1args
203 $([[ $($alwaysTranscode; print $?) -eq 0 ]]; print $?) \ 227 p1args=()
204 $([[ "${bare_amp}" -ne 0 ]]; print $?) \ 228 typeset -a p2args
205 ${extensions[(i)${filename:e}]} ${#extensions} \ 229 p2args=()
206 $([[ ${extensions[(i)${filename:e}]} -gt ${#extensions} ]]; print $?) 230 local j=0
207 tempfile=$(mktemp --tmpdir=${filename:h} .transcode.${filename:t:r}.$$.XXXXXX.${filename:e}) 231 for formatKey (${(k)fileInfo}); do
208 cleanup rm -v -- "${tempfile}" 232 [[ ${formatKey} =~ "stream.([0-9]+).codec_type" ]] || continue
209 mv -vf "${filename}" "${tempfile}" 233 local i=$match[1]
210 234 local cType=${fileInfo[${formatKey}]}
211 typeset -A fileInfo 235 local cName=${fileInfo[stream.${i}.codec_name]}
212 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 236
213 fileInfo[${line%=*}]=${(Q)line#*=} 237 local i=$j
214 done 238 j=$((j + 1))
215 239
216 ext=${extensions[1]} 240 printf "Stream %d: %s (%s)\n" ${i} ${cName} ${cType}
217 filename=${filename%.*}.${ext} 241
218 typeset -a args 242 if [[ ${cType} == "video" ]]; then
219 args=(-y) 243 if [[ ${ext} == "mkv" ]]; then
220 typeset -a prePass 244 case ${cName} in
221 prePass=() 245 h264|vp9|png)
222 typeset -a cargs
223 cargs=()
224 typeset -a p1args
225 p1args=()
226 typeset -a p2args
227 p2args=()
228 local j=0
229 for formatKey (${(k)fileInfo}); do
230 [[ ${formatKey} =~ "stream.([0-9]+).codec_type" ]] || continue
231 local i=$match[1]
232 local cType=${fileInfo[${formatKey}]}
233 local cName=${fileInfo[stream.${i}.codec_name]}
234
235 local i=$j
236 j=$((j + 1))
237
238 printf "Stream %d: %s (%s)\n" ${i} ${cName} ${cType}
239
240 if [[ ${cType} == "video" ]]; then
241 if [[ ${ext} == "mkv" ]]; then
242 case ${cName} in
243 h264|vp9|png)
244 cargs+=(-c:${i} copy)
245 ;;
246 *)
247 # p1args=(-pass 1 -threads 8 -speed 4 -tile-columns 6 -frame-parallel 1)
248 # prePass+=(-c:${i} libvpx-vp9 -b:${i} 0 -crf:${i} 33)
249 # p2args+=(-pass 2 -threads 8 -speed 2 -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25)
250 # cargs+=(-c:${i} libvpx-vp9 -b:${i} 0 -crf:${i} 33)
251 cargs+=(-c:${i} libvpx-vp9 -b:${i} 2M -threads 8 -tile-columns 6 -frame-parallel 1)
252 ;;
253 esac
254 elif [[ ${ext} == "mp4" ]]; then
255 case ${cName} in
256 mpeg|h264)
257 cargs+=(-c:${i} copy)
258 ;;
259 *)
260 p2args+=(-strict -2)
261 cargs+=(-c:${i} libx264)
262 ;;
263 esac
264 fi
265 elif [[ ${cType} == "audio" ]]; then
266 if [[ ${ext} == "mkv" ]]; then
267 if [[ ( ${cName} == opus || ${cName} == flac || ${cName} == vorbis ) && "${bare_amp}" -eq 0 ]]; then
268 cargs+=(-c:${i} copy) 246 cargs+=(-c:${i} copy)
269 else 247 ;;
270 p2args+=(-vbr on -compression_level 10) 248 *)
271 cargs+=(-c:${i} libopus -b:${i} 256K) 249 # p1args=(-pass 1 -threads 8 -speed 4 -tile-columns 6 -frame-parallel 1)
272 fi 250 # prePass+=(-c:${i} libvpx-vp9 -b:${i} 0 -crf:${i} 33)
273 elif [[ ${ext} == "mp4" ]]; then 251 # p2args+=(-pass 2 -threads 8 -speed 2 -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25)
274 if [[ ${cName} == aac && "${bare_amp}" -eq 0 ]]; then 252 # cargs+=(-c:${i} libvpx-vp9 -b:${i} 0 -crf:${i} 33)
253 cargs+=(-c:${i} libvpx-vp9 -b:${i} 2M -threads 8 -tile-columns 6 -frame-parallel 1)
254 ;;
255 esac
256 elif [[ ${ext} == "mp4" ]]; then
257 case ${cName} in
258 mpeg|h264)
275 cargs+=(-c:${i} copy) 259 cargs+=(-c:${i} copy)
276 else 260 ;;
277 cargs+=(-c:${i} aac) 261 *)
278 fi 262 p2args+=(-strict -2)
263 cargs+=(-c:${i} libx264)
264 ;;
265 esac
266 fi
267 elif [[ ${cType} == "audio" ]]; then
268 if [[ ${ext} == "mkv" ]]; then
269 if [[ ( ${cName} == opus || ${cName} == flac || ${cName} == vorbis ) && "${bare_amp}" -eq 0 ]]; then
270 cargs+=(-c:${i} copy)
271 else
272 p2args+=(-vbr on -compression_level 10)
273 cargs+=(-c:${i} libopus -b:${i} 256K)
274 fi
275 elif [[ ${ext} == "mp4" ]]; then
276 if [[ ${cName} == aac && "${bare_amp}" -eq 0 ]]; then
277 cargs+=(-c:${i} copy)
278 else
279 cargs+=(-c:${i} aac)
279 fi 280 fi
280 fi 281 fi
281 done 282 fi
283 done
282 284
283 [[ "${bare_amp}" -ne 0 ]] && p2args+=(-af "${amp}") 285 [[ "${bare_amp}" -ne 0 ]] && p2args+=(-af "${amp}")
284 286
285 if [[ $#prePass -gt 0 ]]; then 287 if [[ $#prePass -gt 0 ]]; then
286 args+=(-v info -i ${tempfile}) 288 args+=(-v info -i ${tempfile})
287 289
288 oldPwd=${PWD} 290 oldPwd=${PWD}
289 cd $(mktemp -d --tmpdir "transcode.${0:t}.$$.XXXXXX") 291 cd $(mktemp -d --tmpdir "transcode.${0:t}.$$.XXXXXX")
290 cleanup rm -rfv -- ${PWD} 292 cleanup rm -rfv -- ${PWD}
291 293
292 p1args+=(-an -f matroska) 294 p1args+=(-an -f matroska)
293 print -- ${prePass} ${p1args} 295 print -- ${prePass} ${p1args}
294 ffmpeg ${args} ${prePass} ${p1args} -- /dev/null 296 ffmpeg ${args} ${prePass} ${p1args} -- /dev/null
297 ffmpegResult=$((ffmpegResult || $?))
295 298
296 print -- ${cargs} ${p2args} 299 print -- ${cargs} ${p2args}
297 ffmpeg ${args} ${cargs} ${p2args} -- "${filename}" 300 ffmpeg ${args} ${cargs} ${p2args} -- "${filename}"
301 ffmpegResult=$((ffmpegResult || $?))
298 302
299 cd ${oldPwd} 303 cd ${oldPwd}
300 else 304 else
301 args+=(-v warning -i pipe:0) 305 args+=(-v warning -i pipe:0)
302 306
303 print -- ${cargs} ${p2args} 307 print -- ${cargs} ${p2args}
304 pv -N "$(trimName "trans:" ${title})" ${tempfile} | ffmpeg ${args} ${cargs} ${p2args} -- "${filename}" 308 pv -N "$(trimName "trans:" ${title})" ${tempfile} | ffmpeg ${args} ${cargs} ${p2args} -- "${filename}"
305 fi 309 ffmpegResult=$((ffmpegResult || ${pipestatus[2]}))
306 fi 310 fi
311 fi
312 fi
307 313
314 if [[ -n "${filename}" && -e "${filename}" && ${ytResult} -eq 0 && ${ffmpegResult} -eq 0 ]]; then
308 chmod -v 644 "${filename}" 315 chmod -v 644 "${filename}"
309 tmpFile=$(mktemp --tmpdir=/home/gkleen/rss/tmp .insertUrl.$$.XXXXXX) 316 tmpFile=$(mktemp --tmpdir=/home/gkleen/rss/tmp .insertUrl.$$.XXXXXX)
310 relUrl=$(realpath --relative-to=/srv/media ${filename}) 317 relUrl=$(realpath --relative-to=/srv/media ${filename})
@@ -331,7 +338,7 @@ printf ">>> %s <<<\n %s\n" "${msgId}" "${message}"
331 print "Message vanished from inbox" >&2 338 print "Message vanished from inbox" >&2
332 fi 339 fi
333 else 340 else
334 printf "An error occured while downloading video at ‘%s’ (exitcode: %d)\n" ${url} ${ytResult} >&2 341 printf "An error occured while downloading video at ‘%s’ (youtube-dl: %d) (ffmpeg: %d)\n" ${url} ${ytResult} ${ffmpegResult} >&2
335 # notmuch tag +failed -- ${msgId} 342 # notmuch tag +failed -- ${msgId}
336 # exit $? 343 # exit $?
337 fi 344 fi