Version 2: encoding separate

This commit is contained in:
pepa65 2022-08-28 20:34:38 +07:00
parent 08a25f62af
commit eeeb46dd07
5 changed files with 46 additions and 34 deletions

4
.gitignore vendored
View File

@ -1,2 +1,2 @@
upload.log
uploadpage/uploads/*
encoding.log
uploadpage/streams/*

49
prep
View File

@ -1,31 +1,44 @@
#!/usr/bin/env bash
# prep - Encode for streaming and schedule cron job
# Usage: prep <upload>
# (called from upload.php in /var/www/uploadpage)
# <upload>: "re_[0-9a-f_]*.YYYY-MM-DD_hh:mm" in /var/www/uploadpage/uploads/
# Usage: prep
# Called by cron every minute; process oldest .upload file in /var/www/uploadpage/streams)
log=/var/www/encoding.log
dir=/var/www/uploadpage/streams
# Check for oldest uploaded file
upload=$(/usr/bin/ls -1tr "$dir"/*.upload 2>/dev/null |head -1)
# Finished if no /var/www/uploadpage/streams/*.upload files found
[[ $upload ]] || exit 0
Log(){ # 1:message 2:returncode
echo "$1">>"$log"
# Remove source (if returncode not zero)
/usr/bin/sync
/usr/bin/sleep 1
(($2)) && /usr/bin/rm "$file"
exit $2
}
# Rename upload and check type
file=${upload%.upload} name=${file##*/} key=${name%%.*} date=${name##*.} video=$file.mp4
/usr/bin/mv "$upload" "$file"
log=/var/www/upload.log
dir=/var/www/uploadpage/uploads
name=$1 file=$dir/$name key=${name%%.*} date=${name##*.}
type=$(file -bL --mime-type "$file")
[[ ! ${type:0:5} = video ]] &&
echo "$type" && exit 1
[[ ! ${type:0:5} = video ]] && Log "File $name is of type $type" 1
# Convert videofile
/usr/bin/ffmpeg -y -i "$file" \
-vcodec libx264 -force_key_frames 'expr:gte(t,n_forced*2)' -b:v 5m -acodec copy "$file.mp4" &&
sync &&
sleep 1 &&
/usr/bin/rm "$file" ||
exit 2
# Encode video
/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" ||
Log "Error encoding $name" 2
# Schedule cron job
m=${date:14: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 "'*'" /var/www/stream $name"
line="$m $h $D $M "'*'" /var/www/stream '${video##*/}'"
echo -e "$(/usr/bin/crontab -l)\n$line" |/usr/bin/crontab -
echo "crontab: '$line'" >>"$log"
exit 0
Log "crontab: '$line'" 0

11
stream
View File

@ -1,6 +1,13 @@
#!/usr/bin/env bash
# stream - Stream video when called from crontab
in=/var/www/uploadpage/uploads/$1.mp4 key=${1%%.*}
# stream - Stream video when called from crontab
# Usage: stream <video>
# The video should have been re-encoded by `prep` first,
# and have the streamkey at the start of the filename.
in=/var/www/uploadpage/streams/$1 key=${1%%.*}
/usr/bin/ffmpeg -re -y -i $in -c:v copy -c:a copy \
-f flv "rtmp://singapore.restream.io/live/$key"
# -f flv "rtmp://live.restream.io/live/$key"
# -f flv "rtmp://a.rtmp.youtube.com/live2/$key"
# -f flv "rtmps://live-api-s.facebook.com:443/rtmp/$key"

View File

@ -13,7 +13,7 @@ function respond(){
<tr><td colspan=2 align=center><h1>Video Upload Page</h1></td></tr>
<tr>
<td>Streamkey:</td>
<td><input type="text" name="streamkey" id="streamkey" required title="re_ followed by a string of 0-9, a-f or underscore characters (20 or longer)" pattern="re_[a-f0-9_]{20,}"></td></tr>
<td><input type="text" name="streamkey" id="streamkey" required title="string of 0-9, a-z or underscore characters" pattern="[a-z0-9_]+"></td></tr>
<tr>
<td>Date:</td>
<td><input type="date" name="date" id="date" required></td></tr>

View File

@ -21,9 +21,9 @@ $upload=htmlspecialchars(basename($_FILES['fileToUpload']['name']));
$key=$_POST['streamkey'];
$date=$_POST['date'];
$time=$_POST['time'];
$dir='uploads/';
$dir='streams/';
$name=$key.'.'.$date.'_'.$time;
$file=$dir.$name;
$file=$dir.$name.'.upload';
print('<!DOCTYPE html>
<meta charset="utf-8">
<title>Uploading</title>
@ -46,14 +46,6 @@ if(!move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $file)){
back('Error moving the file');
}
$res=exec('../prep '.$name, $output, $ret);
if($ret==1){
back('Not a video file, but of type: '.$res);
}
if($ret==2){
back('Error with the encoding');
}
print('File uploaded as "'.$name.'"<br>
back('File is now being encoded to "'.$name.'.mp4"<br>
Scheduled for '.$date.' at '.$time);
?>