diff --git a/encode b/encode index e67cac5..f13b314 100755 --- a/encode +++ b/encode @@ -2,12 +2,12 @@ # encode - Encode for streaming and schedule cron job # Usage: encode -# Called by cron every minute; process oldest .upload file in uploadpage/streams) +# Called by cron every minute; process oldest .upload file in ./uploadpage/streams) # Check for oldest uploaded file -_=$(realpath -- "${BASH_SOURCE:-$0}") repopath=${_%/*} +_=$(readlink -f -- "${BASH_SOURCE:-$0}") repopath=${_%/*} log=$repopath/process.log dir=$repopath/uploadpage/streams -upload=$(/usr/bin/ls -1tr "$dir"/*.upload 2>/dev/null |head -1) +upload=$(ls -tr "$dir"/*.upload 2>/dev/null |head -1) # Finished if no uploadpage/streams/*.upload files found [[ $upload ]] || exit 0 @@ -17,29 +17,35 @@ Log(){ # 1:message 2:returncode(empty: no exit) I:file [[ $2 ]] && exit $2 || return 0 } -Mail(){ # 1:kind(0:done, 1:wrong type, 2:encoding error) 2:logline I:email,name,type,start,finish - local sbj msg from="Stream Upload server" +Mail(){ # 1:kind(0:done, 1:wrong type, 2:encoding error) 2:logline I:repopath,email,name,type,start,finish source "$repopath/mailvars" # I:to,user,password,smtp,port - [[ $email ]] && to=$email - sbj[0]="Stream upload done for ${name##*@}" - sbj[1]="Stream upload wrong type: $type" - sbj[2]="Stream upload error encoding" + local sbj msg from="Stream Upload server" + # Strip the '_' if email set + [[ $email ]] && to=${email:1} + sbj[0]="Stream Upload encoding done for ${name##*@}" + sbj[1]="Stream Upload file wrong type: $type" + sbj[2]="Stream Upload error encoding" msg[0]="Heya,\n\nVideo file with tag $name\nEncoding started on $start and finished on $finish.\n\nStream Upload server" msg[1]="Heya,\n\nThe file $name from $start is of type $type and could not be used.\n\nStream Upload server\n" msg[2]="Heya,\n\nThe file $name started encoding on $start but ran into an error on $finish.\n\nStream Upload server\n" - [[ $to && $user && $password && $smtp && $port ]] && + if [[ $to && $user && $password && $smtp && $port ]] + then mailer -m "$(echo -e "${msg[$1]}")" -t "$to" -s "${sbj[$1]}" -u "$user" -p "$password" -S "$smtp" -P "$port" -f "$from" && - Log "Mail with subject '${sbj[$1]}' sent to $to" || + Log "Mail with subject '${sbj[$1]}' sent to $to" || + Log "Mail with subject '${sbj[$1]}' failed to send" + Log "-- Start encoding on $start, finished on $finish" + else Log "Mail with subject '${sbj[$1]}' could not be sent" + (($1==1)) && Log "-- File of type $type" + (($1==2)) && Log "-- Start encoding on $start, error on $finish" + fi Log "$2" $1 } # Rename upload and check type file=${upload%.upload} video=$file.mp4 name=${file##*/} key=${name%%.*} rest=${name#*.} date=${rest:0:15} _=${rest:15} email=${_%@*} -# If email embedded, strip the '_' -[[ $email ]] && email=${email:1} -/usr/bin/mv "$upload" "$file" +mv "$upload" "$file" type=$(file -bL --mime-type "$file") [[ ! ${type:0:5} = video ]] && Mail 1 "File $name is of type $type" @@ -47,21 +53,23 @@ type=$(file -bL --mime-type "$file") # Encode video start=$(date +'%Y-%m-%d at %H:%M:%S') error=0 ## Single pass -#/usr/bin/ffmpeg -y -i "$file" -c:v libx264 -x264opts no-scenecut -b:v 6M -force_key_frames 'expr:gte(t,n_forced*2)' -c:a copy -tune zerolatency -f mp4 "$video" || +#ffmpeg -y -i "$file" -c:v libx264 -x264opts no-scenecut -b:v 6M -force_key_frames 'expr:gte(t,n_forced*2)' -c:a copy -tune zerolatency -f mp4 "$video" || # Double pass -/usr/bin/ffmpeg -y -i "$file" -c:v libx264 -x264opts no-scenecut -b:v 6M -maxrate 6M -bufsize 12M -force_key_frames 'expr:gte(t,n_forced*2)' -movflags faststart -c:a copy -tune zerolatency -pass 1 -f mp4 "$video" && - /usr/bin/ffmpeg -y -i "$file" -c:v libx264 -x264opts no-scenecut -b:v 6M -maxrate 6M -bufsize 12M -force_key_frames 'expr:gte(t,n_forced*2)' -movflags faststart -c:a copy -tune zerolatency -pass 2 -f mp4 "$video" || +cd $(mktemp -d) # for the pass-1 logfile +ffmpeg -y -i "$file" -c:v libx264 -x264opts no-scenecut -b:v 6M -maxrate 6M -bufsize 12M -force_key_frames 'expr:gte(t,n_forced*2)' -movflags faststart -c:a copy -tune zerolatency -pass 1 -passlogfile "$file" -f mp4 "$video" && + ffmpeg -y -i "$file" -c:v libx264 -x264opts no-scenecut -b:v 6M -maxrate 6M -bufsize 12M -force_key_frames 'expr:gte(t,n_forced*2)' -movflags faststart -c:a copy -tune zerolatency -pass 2 -passlogfile "$file" -f mp4 "$video" || error=1 +# Remove logfiles +rm "$file-*" finish=$(date +'%Y-%m-%d at %H:%M:%S') ((error)) && Mail 2 "Error encoding $name" # Schedule cron job m=${date:13:2} m=${m#0} h=${date:11:2} h=${h#0} D=${date:8:2} D=${D#0} M=${date:5:2} M=${M#0} -/usr/bin/crontab -l || - echo -e "# m h dom mon dow command\n" |/usr/bin/crontab - +crontab -l || echo -e "# m h dom mon dow command\n" |crontab - line="$m $h $D $M "'*'" $repopath/stream '$name'" -echo -e "$(/usr/bin/crontab -l)\n$line" |/usr/bin/crontab - +echo -e "$(crontab -l)\n$line" |crontab - Mail 0 "crontab: '$line'" diff --git a/stream b/stream index d3c49a7..c1e7a6f 100755 --- a/stream +++ b/stream @@ -2,12 +2,12 @@ # stream - Stream video when called from crontab # Usage: stream -# The tag refers to video files in $repopath/uploadpage/streams without the +# The tag refers to video files in ./uploadpage/streams without the # '.mp4', that should have been re-encoded by `encode`. The tag has no path # information, has the "streamkey" at the start before the first dot (there # has to be a dot!) and the "target" at the very end, after the last '@'. -_=$(realpath -- "${BASH_SOURCE:-$0}") repopath=${_%/*} +_=$(readlink -f -- "${BASH_SOURCE:-$0}") repopath=${_%/*} log=$repopath/process.log in=$repopath/uploadpage/streams/$1.mp4 key=${1%%.*} target=${1##*@} case $target in @@ -17,5 +17,5 @@ RestreamSG) rtmp=rtmp://singapore.restream.io/live/$key ;; *) rtmp=rtmp://live.restream.io/live/$key esac -/usr/bin/ffmpeg -re -y -i $in -c:v copy -c:a copy -f flv "$rtmp" || +ffmpeg -re -y -i $in -c:v copy -c:a copy -f flv "$rtmp" || echo "Error while streaming $1" >>"$log"