#!/bin/sh
# $Id: mmp,v 1.28 2024/07/06 07:01:28 grog Exp grog $
# From: mmp,v 1.14 2018/03/13 10:53:47 grog Exp $
#
# Play a video stream
# usage:  mmp -opts mpeg-image [subtitles]
#
MPLAYER=/usr/local/bin/mpv
# Be cleverer about this name
SEENLIST=/spool/stats/seenlist
if [ "$1" = "-i" ]; then
  # Set aspect ratio to "wide"
  INTERLACE=--deinterlace=yes
  shift
fi
if [ "$1" = "-W" ]; then
  # Set aspect ratio to "wide"
  ASPECT="--video-aspect-override=16:9"
  shift
fi
if [ "$1" = "-N" ]; then
  # Set aspect ratio to "narrow"
  ASPECT="--video-aspect-override=4:3"
  shift
fi
if [ "$1" = "-x" ]; then
  # Use x11 output for remote displays.  Otherwise VO is empty
  VO='-vo x11'
  shift
fi
SIZE="--sub-font-size=30 --osd-font-size=20"
xtset mplayer: $1
STARTLOG=`date  +"%a %F %H:%M:%S"`
echo $MPLAYER $VO --write-filename-in-watch-later-config $SIZE $INTERLACE $ASPECT $*
$MPLAYER $VO --write-filename-in-watch-later-config $SIZE $INTERLACE $ASPECT $*
echo -n "Remove $1? "
read reply
if [ "$reply" = "y" ]; then
  base=`basename $1 .mp4`
  rm -f $1 $base.sub  $base.txt sub  $base.srt sub $base.*ttml $base.*vtt
  echo Removed $1
  STATUS=x
  ls -l $base*
elif [ "$reply" = "a" ]; then
  base=`basename $1 .mp4`
  ALREADY=`pwd | sed 's:spool:spool/Already:'`
  mkdir -p $ALREADY
  for i in $1 $base.sub  $base.txt sub  $base.srt sub $base.*ttml $base.*vtt; do
    if [ -f $i ]; then
      mv $i $ALREADY
    fi
  done
  echo Saved and removed $1
  STATUS=x
  ls -l $base*
elif [ "$reply" = "t" ]; then
  truncate -s `cat $1.fpos` $1
  echo Truncating $1 to current position
  STATUS=t
else
  echo Keeping $1
  STATUS=k
fi
PATHNAME=`pwd`/$1
PATHNAME=`echo $PATHNAME |sed  's:.*//':/:`
echo $STARTLOG "->" `date +%H:%M:%S` $STATUS $PATHNAME >> $SEENLIST
echo
xtset `pwd`: Empty slot
echo Finished with $PATHNAME
# build lists
seelist.php > /spool/stats/seenrecently &
