Skip to content

Instantly share code, notes, and snippets.

@gingerbeardman
Last active June 28, 2023 06:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gingerbeardman/59413f3b9d06e0d7a8ccbf3119e7dbdf to your computer and use it in GitHub Desktop.
Save gingerbeardman/59413f3b9d06e0d7a8ccbf3119e7dbdf to your computer and use it in GitHub Desktop.
Synology DSM Task Scheduler custom script to set faststart movflag on all mp4 files
#!/usr/bin/env bash
# by Matt Sephton @gingerbeardman
# requires SynoCommunity ffmpeg to be installed
log="/volume1/video/Scripts/faststart.log"
work="/volume1/video/Scripts/"
start="../"
export log
export bin
function output ()
{
echo "$1"
echo "$1" >> $log
}
export -f output
function outprint ()
{
printf "$1"
printf "$1" >> $log
}
export -f outprint
function notify () # Faststart, status, path, info
{
#echo "Args are \"$@\""
synodsmnotify @administrators "$1 $2" "$3"
msg="$2 = $3"
output $msg
}
export -f notify
cd $work
total=$(find "$start" -type f \( -iname \*.mp4 \) -not -path "../#recycle*" | wc -l)
if [ $total -eq 0 ]; then
output "no MP4 files found"
exit
fi
#notify Faststart found "$total files"
function process_pass ()
{
#notify Faststart OK "$short ($runtime seconds)"
output "OK"
rm "$1"
mv "${1%.*}.fast.mp4" "$1"
}
export -f process_pass
function process_fail ()
{
notify Faststart FAIL "$short" ""
}
export -f process_fail
function process_file ()
{
fullname=$(basename "$1")
short="${fullname%.*}"
outprint "$short = "
start=`date +%s`
/usr/local/ffmpeg/bin/ffmpeg -y -i "$1" -movflags faststart -acodec copy -vcodec copy "${1%.*}.fast.mp4" -hide_banner -loglevel error
end=`date +%s`
runtime=$((end-start))
status=$?
[ $status -eq 0 ] && process_pass "$1" || process_fail "$1"
}
export -f process_file
echo "" > $log
find "$start" -type f \( -iname \*.mp4 \) -not -path "../#recycle*" -print0 | xargs -0 -n1 bash -c 'process_file "$@"' _
wait
total=$(find "$start" -type f \( -iname \*.mp4 \) -not -path "../#recycle*" | wc -l)
notify Faststart complete "$total files processed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment