#!/usr/bin/env bash # encode - Encode for streaming and schedule cron job # Usage: encode # Called by cron every minute; process oldest .upload file in uploadpage/streams) # Check for oldest uploaded file _=$(realpath -- "${BASH_SOURCE:-$0}") repopath=${_%/*} log=$repopath/process.log dir=$repopath/uploadpage/streams upload=$(/usr/bin/ls -1tr "$dir"/*.upload 2>/dev/null |head -1) # Finished if no uploadpage/streams/*.upload files found [[ $upload ]] || exit 0 Log(){ # 1:message 2:returncode(empty: no exit) I:file echo "$1">>"$log" [[ $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" 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" 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 ]] && 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]}' could not be sent" 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" 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') 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" || # Double pass /usr/bin/ffmpeg -y -i "$file" -c:v libx264 -x264opts no-scenecut -b:v 6M -maxrate 6M -bufsize 1M -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 1M -force_key_frames 'expr:gte(t,n_forced*2)' -movflags faststart -c:a copy -tune zerolatency -pass 2 -f mp4 "$video" || error=1 finish=$(date +'%Y-%m-%d at %H:%M') ((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 - line="$m $h $D $M "'*'" $repopath/stream '$name'" echo -e "$(/usr/bin/crontab -l)\n$line" |/usr/bin/crontab - Mail 0 "crontab: '$line'"