#!/bin/sh -e
#
# yt-play - yt-dlp with clipboard input
# dependencies: yt-dlp | ffmpeg

if command -v gum >/dev/null 2>&1; then
  INPUT=$(gum input --placeholder "Video URL" --prompt.foreground 240)
  TIME=$(gum input --placeholder "input time in seconds " --prompt.foreground 240)
  RUNTIME=$(gum input --placeholder "Enter how many seconds to download: " \
    --prompt.foreground 240)
  printf '%s' "Input = " "${INPUT}" "${TIME}"

  gum spin --title "Downloading clip..." -- \
    yt-dlp -f "(bestvideo+bestaudio/best)[protocol!*=dash]" \
    --external-downloader ffmpeg \
    --external-downloader-args "ffmpeg_i:-ss ${TIME} -t ${RUNTIME}" "${INPUT}"
else
  # shellcheck disable=SC2312
  yt-dlp "$(pbpaste)" -o - | ffplay -y 720 -loop 0 - -loglevel quiet
fi
