49 lines
1.7 KiB
Bash
Executable File
49 lines
1.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set +vx
|
|
# convert - Encode for Library display
|
|
# Usage: convert
|
|
# Called by cron every minute; process oldest .library file in ./web/streams
|
|
# and upload it to the Library display server
|
|
# Required: file coreutils(readlink ls head mv tail rm) ffmpeg
|
|
|
|
# Check for oldest uploaded file
|
|
_=$(readlink -f -- "${BASH_SOURCE:-$0}") path=${_%/*}
|
|
log=$path/library.log dir=$path/web/streams
|
|
upload=$(ls -Atr "$dir"/*.library 2>/dev/null |head -1)
|
|
|
|
# Finished if no videos/*.library videos found
|
|
[[ $upload ]] || exit 0
|
|
|
|
Log(){ # 1:message 2:returncode(empty: no exit) I:log
|
|
echo -e "$(date +'%Y-%m-%d at %H:%M:%S') $1">>"$log"
|
|
[[ $2 ]] && exit $2 || return 0
|
|
}
|
|
|
|
# Rename upload and check type
|
|
file=${upload%.library} video=$file.mp4 name=${file##*/}
|
|
mv "$upload" "$file"
|
|
|
|
type=$(file -bL --mime-type "$file")
|
|
[[ ! ${type:0:5} = video ]] && Log "File $name is of type $type" 1
|
|
|
|
# Encode video
|
|
b= F= s= d= f= t=
|
|
[[ $bitrate ]] && bi="-b:v $bitrate -maxrate $bitrate -bufsize $((bitrate*2))"
|
|
[[ $format ]] && fo="format=$format,"
|
|
[[ $scale ]] && sc="scale=${scale//:/x},"
|
|
[[ $dar ]] && da="setdar=${dar//://},"
|
|
[[ $fps ]] && fp="fps=$fps,"
|
|
[[ $fo || $sc || $da || $fp ]] && vf="-vf '$fo$sc$da$fp" vf="${vf%,}'"
|
|
[[ $tbn ]] && tb="-video_track_timescale $tbn"
|
|
cmd="ffmpeg -y -i $file -c:v libx264 -x264opts no-scenecut $bi $vf $tb -c:a copy $video"
|
|
set -o pipefail # to get ffmpeg's returncode
|
|
ffmpeg -y -i "$file" -c:v libx264 -x264opts no-scenecut -vf "scale=1920x980,fps=15" "$video" 2>&1 |tail -n 20 >"$log" ||
|
|
Log "Error encoding $name" 2
|
|
|
|
Log "Encoding $video done"
|
|
|
|
curl -v -F video=@$dir/$name.mp4 -F type=play http://192.168.7.21:8888/process.php &&
|
|
Log "Dispatching video done" 0 ||
|
|
Log "Error dispatching video" 3
|
|
|