From ab642d9c51337de4df409c14dfa10bb1d499e9df Mon Sep 17 00:00:00 2001 From: root Date: Sun, 9 Oct 2022 12:24:15 +0700 Subject: [PATCH] Timestamp to log Email on start encode Encode audio 48000 bps Clearly show error or success in Results page Optional savedir --- _vars | 3 +++ encode | 20 +++++++++++++------- stream | 10 ++++++++-- uploadpage/index.php | 10 +++++----- uploadpage/login.php | 2 +- uploadpage/upload.php | 33 ++++++++++++++++++++------------- 6 files changed, 50 insertions(+), 28 deletions(-) diff --git a/_vars b/_vars index 9624eb8..156217a 100644 --- a/_vars +++ b/_vars @@ -11,6 +11,9 @@ interface='' # Email for registering SSL-certificates sslmail='(valid email)' +# Directory for saving completed streams +savedir='' + # Variables for mailer user='(smtp login)' password='(smtp password)' diff --git a/encode b/encode index 30f308a..724af2c 100755 --- a/encode +++ b/encode @@ -1,5 +1,5 @@ #!/usr/bin/env bash - +set +vx # encode - Encode for streaming and schedule cron job # Usage: encode # Called by cron every minute; process oldest .upload file in ./uploadpage/streams) @@ -15,7 +15,7 @@ upload=$(ls -Atr "$dir"/*.upload 2>/dev/null |head -1) [[ $upload ]] || exit 0 Log(){ # 1:message 2:returncode(empty: no exit) I:file - echo -e "$1">>"$log" + echo -e "$(date +'%m/%d %H:%M:%S') $1">>"$log" [[ $2 ]] && exit $2 || return 0 } @@ -36,9 +36,11 @@ Mail(){ # 1:kind(0:done, 1:wrong type, 2:encoding error) 2:logline I:repopath,em sbj[0]="Stream Upload encoding done for ${name##*@}" sbj[1]="Stream Upload file wrong type: $type" sbj[2]="Stream Upload error encoding" + sbj[3]="Stream Upload encoding started" msg[0]="Encoded video with tag '$name'.\nEncoding started on $start and finished on $finish." msg[1]="The file '$name' from $start is of type '$type' and could not be used." msg[2]="The file '$name' started encoding on $start but ran into an error on $finish." + msg[3]="The file '$name' was received and encoding is starting." message="Heya,\n\n${msg[$1]}\n\nStream Upload server\n" if [[ $to && $user && $password && $smtp && $port ]] then # All required ingredients for a mail present @@ -48,10 +50,12 @@ Mail(){ # 1:kind(0:done, 1:wrong type, 2:encoding error) 2:logline I:repopath,em ((err==1)) && Log "== Mail with subject '${sbj[$1]}' could not be sent" ((err==2)) && Log "== Mail with subject '${sbj[$1]}' failed to send to: $to" else # Can't send - Log "== Mail with subject '${sbj[$1]}' cannot be sent" + Log "== Mail with subject '${sbj[$1]}' cannot be sent [$to-$user-$password-$smtp-$port]" fi + Log "${msg[$1]}" - Log "$2" $1 + # No log, no exit when just mailing to say we're starting + (($1==3)) || Log "$2" $1 } # Rename upload and check type @@ -59,17 +63,19 @@ file=${upload%.upload} video=$file.mp4 name=${file##*/} key=${name%%.*} rest=${name#*.} date=${rest:0:15} id=${rest:15:1} _=${rest:16} _=${_%@*} username=${_%%:*} email=${_#$username} mv "$upload" "$file" +start=$(date +'%Y-%m-%d at %H:%M:%S') type=$(file -bL --mime-type "$file") [[ ! ${type:0:5} = video ]] && Mail 1 "File $name is of type $type" # Encode video -start=$(date +'%Y-%m-%d at %H:%M:%S') error=0 +Mail 3 +error=0 ## Single pass #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" |tail -n 20 >"$file.0log" || error=1 # Double pass set -o pipefail # to get ffmpeg's returncode -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)' -vf "format=yuv420p,scale=1920x1080,setdar=16/9,fps=25" -video_track_timescale 18000 -movflags faststart -c:a copy -tune zerolatency -pass 1 -passlogfile "$file" -f mp4 "$video" 2>&1 |tail -n 20 >"$file.1log" && - 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)' -vf "format=yuv420p,scale=1920x1080,setdar=16/9,fps=25" -video_track_timescale 18000 -movflags faststart -c:a copy -tune zerolatency -pass 2 -passlogfile "$file" -f mp4 "$video" 2>&1 |tail -n 20 >"$file.2log" || +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)' -vf "format=yuv420p,scale=1920x1080,setdar=16/9,fps=25" -video_track_timescale 18000 -movflags faststart -ar 48000 -tune zerolatency -pass 1 -passlogfile "$file" -f mp4 "$video" 2>&1 |tail -n 20 >"$file.1log" && + 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)' -vf "format=yuv420p,scale=1920x1080,setdar=16/9,fps=25" -video_track_timescale 18000 -movflags faststart -ar 48000 -tune zerolatency -pass 2 -passlogfile "$file" -f mp4 "$video" 2>&1 |tail -n 20 >"$file.2log" || error=1 # Remove logfiles diff --git a/stream b/stream index d7f2f93..1c5dacb 100755 --- a/stream +++ b/stream @@ -8,6 +8,7 @@ # has to be a dot!) and the "target" at the very end, after the last '@'. # Required: ffmpeg coreutils(tail rm) + _=$(readlink -f -- "${BASH_SOURCE:-$0}") repopath=${_%/*} log=$repopath/process.log in=$repopath/uploadpage/streams/$1.mp4 key=${1%%.*} target=${1##*@} @@ -17,12 +18,17 @@ YouTube) rtmp=rtmp://a.rtmp.youtube.com/live2/$key ;; RestreamSG) rtmp=rtmp://singapore.restream.io/live/$key ;; *) rtmp=rtmp://live.restream.io/live/$key esac +source "$repopath/vars" # I:savedir # Stream, log it, and remove all files if successful set -o pipefail # to get ffmpeg's returncode -ffmpeg -re -y -i "$in" -c:v copy -c:a copy -f flv "$rtmp" 2>&1 |tail -n 20 >"$in.log" && - rm "$in.log" "$in" "${in%.mp4}" || +if ffmpeg -re -y -i "$in" -c:v copy -c:a copy -f flv "$rtmp" 2>&1 |tail -n 20 >"$in.log" +then + rm "$in.log" "${in%.mp4}" + [[ $savedir && -f $savedir ]] && mv "$in" "$savedir" || rm "$in" +else echo "Error while streaming $1" >>"$log" +fi # In any case, comment-out the crontab entry so it's not waiting for next year crontab -l| sed "/$1/ s/^/#/" |crontab - diff --git a/uploadpage/index.php b/uploadpage/index.php index cbf5045..d835c13 100755 --- a/uploadpage/index.php +++ b/uploadpage/index.php @@ -1,6 +1,6 @@

User: '.$user.'

-
+
@@ -49,7 +49,7 @@ if($n>0){ print(' '); } @@ -64,8 +64,8 @@ print('
- -

+ '); +print('

 

'); diff --git a/uploadpage/login.php b/uploadpage/login.php index 7b6cec3..bb28600 100755 --- a/uploadpage/login.php +++ b/uploadpage/login.php @@ -1,6 +1,6 @@ '.$msg.'

-
+ if(empty($msg)){ + print('

File successfully uploaded, encoding started

'); + }else{ + print('

ERROR: '.$msg.'

'); + } + print('
'); exit; @@ -23,16 +26,19 @@ foreach($mh as $line){ } } $upload=htmlspecialchars(basename($_FILES['file']['name'])); -$key=htmlspecialchars($_POST['streamkey']); -$date=htmlspecialchars($_POST['date']); -$time=htmlspecialchars($_POST['time']); -$email=htmlspecialchars($_POST['email']); -$target=htmlspecialchars($_POST['target']); -$id=htmlspecialchars($_POST['id']); +$key=htmlspecialchars(@$_POST['streamkey']); +$date=htmlspecialchars(@$_POST['date']); +$time=htmlspecialchars(@$_POST['time']); +$email=htmlspecialchars(@$_POST['email']); +$target=htmlspecialchars(@$_POST['target']); +$id=htmlspecialchars(@$_POST['id']); +if(empty($id)){ + $id='_'; +} if(substr($id, 0, 1)!=='_'){ $countdown=substr($id, 1); + $id=substr($id, 0, 1); } -$id=substr($id, 0, 1); if($email){ $to=$email; $email=':'.$email; @@ -51,7 +57,7 @@ print('

User: '.$user.'

-
+

Encoding

@@ -82,6 +88,7 @@ if(isset($countdown)){ print('

using '.$countdown.''); } print('

-

When done encoding, email '.$to.'

'); -Back('Streaming on '.$date.' at '.$time.'h on '.$target.''); +

When done encoding, email '.$to.'

+

Streaming on '.$date.' at '.$time.'h on '.$target.'

'); +Back(''); ?>