Re-encode on reboot-interruption

This commit is contained in:
pepa65 2023-05-07 20:41:15 +07:00
parent 890cf28a09
commit cc37a1b89a
3 changed files with 15 additions and 12 deletions

21
encode
View File

@ -9,6 +9,7 @@ set +vx
# Check for oldest uploaded file # Check for oldest uploaded file
_=$(readlink -f -- "${BASH_SOURCE:-$0}") repopath=${_%/*} _=$(readlink -f -- "${BASH_SOURCE:-$0}") repopath=${_%/*}
log=$repopath/process.log dir=$repopath/web/streams log=$repopath/process.log dir=$repopath/web/streams
shopt -s dotglob # Also get files starting with '.'
upload=$(ls -Atr "$dir"/*.upload 2>/dev/null |head -1) upload=$(ls -Atr "$dir"/*.upload 2>/dev/null |head -1)
# Finished if no web/streams/*.upload files found # Finished if no web/streams/*.upload files found
@ -59,27 +60,27 @@ Mail(){ # 1:kind(0:done, 1:wrong type, 2:encoding error) 2:logline I:repopath,em
} }
# Rename upload and check type # Rename upload and check type
file=${upload%.upload} video=$file.mp4 name=${file##*/} key=${name%%.*} file=${upload%.upload} proc=$file.processing video=$file.mp4 name=${file##*/} key=${name%%.*}
rest=${name#*.} date=${rest:0:15} id=${rest:15:1} _=${rest:16} _=${_%@*} username=${_%%:*} email=${_#$username} rest=${name#*.} date=${rest:0:15} id=${rest:15:1} _=${rest:16} _=${_%@*} username=${_%%:*} email=${_#$username}
mv "$upload" "$file" mv "$upload" "$proc"
start=$(date +'%Y-%m-%d at %H:%M:%S') start=$(date +'%Y-%m-%d at %H:%M:%S')
type=$(file -bL --mime-type "$file") type=$(file -bL --mime-type "$proc")
[[ ! ${type:0:5} = video ]] && Mail 1 "File $name is of type $type" [[ ! ${type:0:5} = video ]] && Mail 1 "File $name is of type $type"
# Encode video # Encode video
Mail 3 Mail 3
error=0 error=0
## Single pass ## 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 #ffmpeg -y -i "$proc" -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 # Double pass
set -o pipefail # to get ffmpeg's returncode 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=30" -video_track_timescale 18000 -movflags faststart -tune zerolatency -pass 1 -passlogfile "$file" -f mp4 "$video" 2>&1 |tail -n 20 >"$file.1log" && ffmpeg -y -i "$proc" -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=30" -video_track_timescale 18000 -movflags faststart -tune zerolatency -pass 1 -passlogfile "$proc" -f mp4 "$video" 2>&1 |tail -n 20 >"$proc.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=30" -video_track_timescale 18000 -movflags faststart -tune zerolatency -pass 2 -passlogfile "$file" -f mp4 "$video" 2>&1 |tail -n 20 >"$file.2log" || ffmpeg -y -i "$proc" -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=30" -video_track_timescale 18000 -movflags faststart -tune zerolatency -pass 2 -passlogfile "$proc" -f mp4 "$video" 2>&1 |tail -n 20 >"$proc.2log" ||
error=1 error=1
# Remove logfiles # Remove logfiles
rm "$file"-* rm "$proc"-*
finish=$(date +'%Y-%m-%d at %H:%M:%S') finish=$(date +'%Y-%m-%d at %H:%M:%S')
((error)) && Mail 2 "Error encoding $name" ((error)) && Mail 2 "Error encoding $name"
@ -87,12 +88,14 @@ finish=$(date +'%Y-%m-%d at %H:%M:%S')
if [[ ! $id = _ ]] if [[ ! $id = _ ]]
then then
countdown=$(ls -tr "$dir/.$id"*.mp4 2>/dev/null |head -1) countdown=$(ls -tr "$dir/.$id"*.mp4 2>/dev/null |head -1)
ffmpeg -f concat -safe 0 -i <(echo -e "file '$countdown'\nfile '$video'") -c copy "${video%.mp4}_.mp4" 2>&1 |tail -n 20 >"$file.3log" ffmpeg -f concat -safe 0 -i <(echo -e "file '$countdown'\nfile '$video'") -c copy "${video%.mp4}_.mp4" 2>&1 |tail -n 20 >"$proc.3log"
mv "${video%.mp4}_.mp4" "$video" mv "${video%.mp4}_.mp4" "$video"
fi fi
# Remove tailfiles # Remove tailfiles
rm -- "$file".?log rm -- "$proc".?log
# Rename
mv -- "$proc" "$file"
# Schedule cron job # Schedule cron job
m=${date:13:2} m=${m#0} h=${date:11:2} h=${h#0} m=${date:13:2} m=${m#0} h=${date:11:2} h=${h#0}

View File

@ -34,7 +34,7 @@ function filename(){
<input class="abs" id="fakeinput" tabindex="-1"> <input class="abs" id="fakeinput" tabindex="-1">
<p class="abs" id="name">Click to select the video</p> <p class="abs" id="name">Click to select the video</p>
</div>'); </div>');
if($user!=='library' && $user!=='pp'){ /////////////////////////////// if($user!=='library'){ ///////////////////////////////
// Check countdown options // Check countdown options
$cf=file(__DIR__.'/../countdown',FILE_IGNORE_NEW_LINES & FILE_SKIP_EMPTY_LINES); $cf=file(__DIR__.'/../countdown',FILE_IGNORE_NEW_LINES & FILE_SKIP_EMPTY_LINES);
$n=0; $n=0;

View File

@ -53,7 +53,7 @@ $min=substr($time, 3, 2);
$tme=$hour.$min; $tme=$hour.$min;
$dir='streams/'; $dir='streams/';
$name=$key.'.'.$date.'_'.$tme.$id.$user.$email.'@'.$target; $name=$key.'.'.$date.'_'.$tme.$id.$user.$email.'@'.$target;
if($user=='library' || $user=='pp'){ //////////////////////// if($user=='library'){ ////////////////////////
move_uploaded_file($_FILES['file']['tmp_name'], $dir.'video.'.$user); move_uploaded_file($_FILES['file']['tmp_name'], $dir.'video.'.$user);
sleep(15); sleep(15);
session_unset(); session_unset();
@ -101,7 +101,7 @@ if(isset($countdown)){
} }
print(' print('
</p> </p>
<p>When done encoding, email <b>'.$to.'</b></p> <p>After encoding, an email is sent to <b>'.$to.'</b></p>
<p>Streaming on <b>'.$date.'</b> at <b>'.$time.'</b>h on <b>'.$target.'</b></p>'); <p>Streaming on <b>'.$date.'</b> at <b>'.$time.'</b>h on <b>'.$target.'</b></p>');
Back(''); Back('');
?> ?>