
# DOWNLOAD VP9
# spd <url> 2:30 30

# DOWNLOAD MKV
# spda <url> 2:30 30

# SET DEFAULTS FOR OTHER STREAMCLIPS
# spsd 

# TRIM
# spt 15 20

# CROP
# spc iw/4 ih/2 3*iw/4 ih/2

# SCALE
# sps 960 -1

# FRAMERATE
# spr 12

# FINISH AND UPLOAD
# spfu

# FINISH WITH PREVIEW, THEN UPLOAD
# spf  
# spu

soundpost_set_defaults ()
{
  scale="scale=-1:-1"
  crop="crop=in_w:in_h:0:0"
  trim="0"
  fps="fps=fps=source_fps"
  _duration=$(ffprobe -hide_banner -v error -show_format -of:c json streamclip* | jq -r '.format.duration')
  dur=${_duration%.*}
}
alias spsd="soundpost_set_defaults"

soundpost_download ()
{
  yt-dlp -f "(bestvideo+bestaudio/best)" $1 --external-downloader ffmpeg --external-downloader-args "ffmpeg_i:-ss $2 -t $3" -o streamclip
  soundpost_set_defaults
  mpv streamclip*
}
alias spd="soundpost_download"

soundpost_download_alt ()
{
  yt-dlp -f "(bestvideo+bestaudio/best)[protocol!*=dash]" $1 --external-downloader ffmpeg --external-downloader-args "ffmpeg_i:-ss $2 -t $3" -o streamclip
  soundpost_set_defaults
  mpv streamclip*
}
alias spda="soundpost_download_alt"

# soundpost_perform_fast_encoding_with_all_settings soundpost_precrop.webm
soundpost_perform_fast_encoding_with_all_settings () {
  ffmpeg -hide_banner -y -i streamclip* -c:a libopus -compression_level 0 -c:v libvpx -cpu-used -5 -deadline realtime -vf "${fps},${crop},${scale}" -ss ${trim} -t $dur $1

}

_soundpost_crop ()
{
  crop="crop=$1:$2:$3:$4"
  soundpost_perform_fast_encoding_with_all_settings soundpost-preview-crop.webm
  mpv soundpost-preview-crop.webm
}
alias soundpost_crop="noglob _soundpost_crop"
alias spc="noglob _soundpost_crop"

soundpost_trim ()
{
  trim=$1
  dur=$2
  soundpost_perform_fast_encoding_with_all_settings soundpost-preview-trim.webm
  mpv soundpost-preview-trim.webm
}
alias spt="soundpost_trim"

soundpost_scale ()
{
  scale="scale=$1:$2"
  soundpost_perform_fast_encoding_with_all_settings soundpost-preview-scale.webm
  mpv soundpost-preview-scale.webm
}
alias sps="soundpost_scale"

soundpost_framerate ()
{
  fps="fps=fps=$1"
  soundpost_perform_fast_encoding_with_all_settings soundpost-preview-fps.webm
  mpv soundpost-preview-fps.webm
}
alias spr="soundpost_framerate"

soundpost_encode_for_separate_files ()
{
  ffmpeg -hide_banner -y -i streamclip* -vn -c:a libopus -b:a 160K -ss ${trim} -t $dur ${PWD##*/}.ogg
  local target_bitrate=$((24000/$dur))
  local targ_bv=${target_bitrate%.*}k 
  ffmpeg -hide_banner -y -i streamclip* -an -c:v libvpx -deadline best -vf "${fps},${crop},${scale}" -ss ${trim} -t $dur -b:v ${targ_bv} -pass 1 ${PWD##*/}.webm
  ffmpeg -hide_banner -y -i streamclip* -an -c:v libvpx -deadline best -vf "${fps},${crop},${scale}" -ss ${trim} -t $dur -b:v ${targ_bv} -pass 2 ${PWD##*/}.webm
}

soundpost_finalise ()
{
  soundpost_encode_for_separate_files
  ffmpeg -hide_banner -y -i ${PWD##*/}.webm -i ${PWD##*/}.ogg -c copy soundpost-final-preview.webm
  mpv soundpost-final-preview.webm
}
alias spf="soundpost_finalise"

soundpost_upload () 
{
  local catbox=`curl -fL -F "reqtype=fileupload" -F "fileToUpload=@${PWD##*/}.ogg" https://catbox.moe/user/api.php`
  local zzht=`curl -fL -F "files[]=@${PWD##*/}.ogg" https://zz.ht/api/upload | jq -c -r '.files[].url'`
  local sound="[sound=$(urle $catbox)][sound=$(urle $zzht)]"
  mv "${PWD##*/}.webm" "${PWD##*/}${sound}.webm"
  mv "${PWD##*/}${sound}.webm" ../
}
alias spu="soundpost_upload"

soundpost_finalise_and_upload ()
{
  soundpost_encode_for_separate_files
  soundpost_upload
}
alias spfu="soundpost_finalise_and_upload"

# url encode one-liner for soundpost urls
urle () { [[ "${1}" ]] || return 1; local LANG=C i x; for (( i = 0; i < ${#1}; i++ )); do x="${1:$i:1}"; [[ "${x}" == [a-zA-Z0-9.~_-] ]] && echo -n "${x}" || printf '%%%02X' "'${x}"; done; echo; }


# for when pomf is back

#   local pomf=`curl -fL -F "files[]=@${PWD##*/}.ogg" https://pomf.cat/upload.php | jq -c -r --arg base https://a.pomf.cat/ '$base + .files[].url'`
#   local sound="[sound=$(urle $catbox)][sound=$(urle $zzht)][sound=$(urle $pomf)]"
