From 41bb298624796dba6149a771748c829fb4f0e570 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 22 May 2018 19:37:26 +0200 Subject: catch ffmpeg failures --- odin/strm/download_youtube | 241 +++++++++++++++++++++++---------------------- 1 file changed, 124 insertions(+), 117 deletions(-) (limited to 'odin') 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}" # mv -v ${filename} ${newFilename} && filename=${newFilename} # fi + ffmpegResult=0 if [[ -n "${filename}" && -e "${filename}" && ${ytResult} -eq 0 ]]; then - max_vol=$( \ - pv -N "$(trimName "vol:" ${title})" ${filename} | ffmpeg -i pipe:0 -af "volumedetect" -vn -f null /dev/null 2>&1 | \ - grep 'max_volume' | sed -r 's/^.*max_volume: ([-0-9\.]+) dB$/\1/' - ) - printf "Maximum volume: %.2fdB\n" "${max_vol}" - [[ -n "${max_vol}" ]] || max_vol=0 - bare_amp=$(printf "%.2f" $(($max_vol * (-1)))) - amp=$(printf "volume=%sdB" "${bare_amp}") - - typeset -a extensions - extensions=("mkv" "mp3" "mp4" "webm") - - if [[ ${msgTags[(i)tv]} -le $#msgTags ]]; then - extensions=("mp4") - fi + max_vol=$( \ + pv -N "$(trimName "vol:" ${title})" ${filename} | ffmpeg -i pipe:0 -af "volumedetect" -vn -f null /dev/null 2>&1 | \ + grep 'max_volume' | sed -r 's/^.*max_volume: ([-0-9\.]+) dB$/\1/' + ) + ffmpegResult=${pipestatus[2]} + printf "Maximum volume: %.2fdB\n" "${max_vol}" + [[ -n "${max_vol}" ]] || max_vol=0 + bare_amp=$(printf "%.2f" $(($max_vol * (-1)))) + amp=$(printf "volume=%sdB" "${bare_amp}") + + typeset -a extensions + extensions=("mkv" "mp3" "mp4" "webm") + + if [[ ${msgTags[(i)tv]} -le $#msgTags ]]; then + extensions=("mp4") + fi + + typeset -a transcodeExtractors + transcodeExtractors=() + + if [[ "${bare_amp}" -ne 0 + || ${extensions[(i)${filename:e}]} -gt ${#extensions} + || ${transcodeExtractors[(i)${extractor}]} -le ${#transcodeExtractors} + ]]; then + printf "Transcoding ‘%s’" ${title} + printf "%d %d %d/%d(%d)…\n" \ + $([[ $($alwaysTranscode; print $?) -eq 0 ]]; print $?) \ + $([[ "${bare_amp}" -ne 0 ]]; print $?) \ + ${extensions[(i)${filename:e}]} ${#extensions} \ + $([[ ${extensions[(i)${filename:e}]} -gt ${#extensions} ]]; print $?) + tempfile=$(mktemp --tmpdir=${filename:h} .transcode.${filename:t:r}.$$.XXXXXX.${filename:e}) + cleanup rm -v -- "${tempfile}" + mv -vf "${filename}" "${tempfile}" + + typeset -A fileInfo + 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 + fileInfo[${line%=*}]=${(Q)line#*=} + done - typeset -a transcodeExtractors - transcodeExtractors=() - - if [[ "${bare_amp}" -ne 0 - || ${extensions[(i)${filename:e}]} -gt ${#extensions} - || ${transcodeExtractors[(i)${extractor}]} -le ${#transcodeExtractors} - ]]; then - printf "Transcoding ‘%s’" ${title} - printf "%d %d %d/%d(%d)…\n" \ - $([[ $($alwaysTranscode; print $?) -eq 0 ]]; print $?) \ - $([[ "${bare_amp}" -ne 0 ]]; print $?) \ - ${extensions[(i)${filename:e}]} ${#extensions} \ - $([[ ${extensions[(i)${filename:e}]} -gt ${#extensions} ]]; print $?) - tempfile=$(mktemp --tmpdir=${filename:h} .transcode.${filename:t:r}.$$.XXXXXX.${filename:e}) - cleanup rm -v -- "${tempfile}" - mv -vf "${filename}" "${tempfile}" - - typeset -A fileInfo - 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 - fileInfo[${line%=*}]=${(Q)line#*=} - done - - ext=${extensions[1]} - filename=${filename%.*}.${ext} - typeset -a args - args=(-y) - typeset -a prePass - prePass=() - typeset -a cargs - cargs=() - typeset -a p1args - p1args=() - typeset -a p2args - p2args=() - local j=0 - for formatKey (${(k)fileInfo}); do - [[ ${formatKey} =~ "stream.([0-9]+).codec_type" ]] || continue - local i=$match[1] - local cType=${fileInfo[${formatKey}]} - local cName=${fileInfo[stream.${i}.codec_name]} - - local i=$j - j=$((j + 1)) - - printf "Stream %d: %s (%s)\n" ${i} ${cName} ${cType} - - if [[ ${cType} == "video" ]]; then - if [[ ${ext} == "mkv" ]]; then - case ${cName} in - h264|vp9|png) - cargs+=(-c:${i} copy) - ;; - *) - # p1args=(-pass 1 -threads 8 -speed 4 -tile-columns 6 -frame-parallel 1) - # prePass+=(-c:${i} libvpx-vp9 -b:${i} 0 -crf:${i} 33) - # p2args+=(-pass 2 -threads 8 -speed 2 -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25) - # cargs+=(-c:${i} libvpx-vp9 -b:${i} 0 -crf:${i} 33) - cargs+=(-c:${i} libvpx-vp9 -b:${i} 2M -threads 8 -tile-columns 6 -frame-parallel 1) - ;; - esac - elif [[ ${ext} == "mp4" ]]; then - case ${cName} in - mpeg|h264) - cargs+=(-c:${i} copy) - ;; - *) - p2args+=(-strict -2) - cargs+=(-c:${i} libx264) - ;; - esac - fi - elif [[ ${cType} == "audio" ]]; then - if [[ ${ext} == "mkv" ]]; then - if [[ ( ${cName} == opus || ${cName} == flac || ${cName} == vorbis ) && "${bare_amp}" -eq 0 ]]; then + ext=${extensions[1]} + filename=${filename%.*}.${ext} + typeset -a args + args=(-y) + typeset -a prePass + prePass=() + typeset -a cargs + cargs=() + typeset -a p1args + p1args=() + typeset -a p2args + p2args=() + local j=0 + for formatKey (${(k)fileInfo}); do + [[ ${formatKey} =~ "stream.([0-9]+).codec_type" ]] || continue + local i=$match[1] + local cType=${fileInfo[${formatKey}]} + local cName=${fileInfo[stream.${i}.codec_name]} + + local i=$j + j=$((j + 1)) + + printf "Stream %d: %s (%s)\n" ${i} ${cName} ${cType} + + if [[ ${cType} == "video" ]]; then + if [[ ${ext} == "mkv" ]]; then + case ${cName} in + h264|vp9|png) cargs+=(-c:${i} copy) - else - p2args+=(-vbr on -compression_level 10) - cargs+=(-c:${i} libopus -b:${i} 256K) - fi - elif [[ ${ext} == "mp4" ]]; then - if [[ ${cName} == aac && "${bare_amp}" -eq 0 ]]; then + ;; + *) + # p1args=(-pass 1 -threads 8 -speed 4 -tile-columns 6 -frame-parallel 1) + # prePass+=(-c:${i} libvpx-vp9 -b:${i} 0 -crf:${i} 33) + # p2args+=(-pass 2 -threads 8 -speed 2 -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25) + # cargs+=(-c:${i} libvpx-vp9 -b:${i} 0 -crf:${i} 33) + cargs+=(-c:${i} libvpx-vp9 -b:${i} 2M -threads 8 -tile-columns 6 -frame-parallel 1) + ;; + esac + elif [[ ${ext} == "mp4" ]]; then + case ${cName} in + mpeg|h264) cargs+=(-c:${i} copy) - else - cargs+=(-c:${i} aac) - fi + ;; + *) + p2args+=(-strict -2) + cargs+=(-c:${i} libx264) + ;; + esac + fi + elif [[ ${cType} == "audio" ]]; then + if [[ ${ext} == "mkv" ]]; then + if [[ ( ${cName} == opus || ${cName} == flac || ${cName} == vorbis ) && "${bare_amp}" -eq 0 ]]; then + cargs+=(-c:${i} copy) + else + p2args+=(-vbr on -compression_level 10) + cargs+=(-c:${i} libopus -b:${i} 256K) + fi + elif [[ ${ext} == "mp4" ]]; then + if [[ ${cName} == aac && "${bare_amp}" -eq 0 ]]; then + cargs+=(-c:${i} copy) + else + cargs+=(-c:${i} aac) fi fi - done + fi + done - [[ "${bare_amp}" -ne 0 ]] && p2args+=(-af "${amp}") + [[ "${bare_amp}" -ne 0 ]] && p2args+=(-af "${amp}") - if [[ $#prePass -gt 0 ]]; then - args+=(-v info -i ${tempfile}) + if [[ $#prePass -gt 0 ]]; then + args+=(-v info -i ${tempfile}) - oldPwd=${PWD} - cd $(mktemp -d --tmpdir "transcode.${0:t}.$$.XXXXXX") - cleanup rm -rfv -- ${PWD} + oldPwd=${PWD} + cd $(mktemp -d --tmpdir "transcode.${0:t}.$$.XXXXXX") + cleanup rm -rfv -- ${PWD} - p1args+=(-an -f matroska) - print -- ${prePass} ${p1args} - ffmpeg ${args} ${prePass} ${p1args} -- /dev/null + p1args+=(-an -f matroska) + print -- ${prePass} ${p1args} + ffmpeg ${args} ${prePass} ${p1args} -- /dev/null + ffmpegResult=$((ffmpegResult || $?)) - print -- ${cargs} ${p2args} - ffmpeg ${args} ${cargs} ${p2args} -- "${filename}" + print -- ${cargs} ${p2args} + ffmpeg ${args} ${cargs} ${p2args} -- "${filename}" + ffmpegResult=$((ffmpegResult || $?)) - cd ${oldPwd} - else - args+=(-v warning -i pipe:0) + cd ${oldPwd} + else + args+=(-v warning -i pipe:0) - print -- ${cargs} ${p2args} - pv -N "$(trimName "trans:" ${title})" ${tempfile} | ffmpeg ${args} ${cargs} ${p2args} -- "${filename}" - fi + print -- ${cargs} ${p2args} + pv -N "$(trimName "trans:" ${title})" ${tempfile} | ffmpeg ${args} ${cargs} ${p2args} -- "${filename}" + ffmpegResult=$((ffmpegResult || ${pipestatus[2]})) fi + fi + fi + if [[ -n "${filename}" && -e "${filename}" && ${ytResult} -eq 0 && ${ffmpegResult} -eq 0 ]]; then chmod -v 644 "${filename}" tmpFile=$(mktemp --tmpdir=/home/gkleen/rss/tmp .insertUrl.$$.XXXXXX) relUrl=$(realpath --relative-to=/srv/media ${filename}) @@ -331,7 +338,7 @@ printf ">>> %s <<<\n %s\n" "${msgId}" "${message}" print "Message vanished from inbox" >&2 fi else - printf "An error occured while downloading video at ‘%s’ (exitcode: %d)\n" ${url} ${ytResult} >&2 + printf "An error occured while downloading video at ‘%s’ (youtube-dl: %d) (ffmpeg: %d)\n" ${url} ${ytResult} ${ffmpegResult} >&2 # notmuch tag +failed -- ${msgId} # exit $? fi -- cgit v1.2.3