From ed08970e11f73b6ebcfc0a8cf39a831c50fe01ea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Florian=20Mei=C3=9Fner?= <florian.meissner@mailbox.org>
Date: Wed, 18 Oct 2023 23:50:52 +0200
Subject: [PATCH] fixes :D

---
 ..._fn.function.fish => _defer.function.fish} | 23 ++++---
 vmaf.fish                                     | 63 ++++++++++++-------
 2 files changed, 56 insertions(+), 30 deletions(-)
 rename helpers/{_fish_gen_cleanup_fn.function.fish => _defer.function.fish} (55%)

diff --git a/helpers/_fish_gen_cleanup_fn.function.fish b/helpers/_defer.function.fish
similarity index 55%
rename from helpers/_fish_gen_cleanup_fn.function.fish
rename to helpers/_defer.function.fish
index f8a300d..9831815 100644
--- a/helpers/_fish_gen_cleanup_fn.function.fish
+++ b/helpers/_defer.function.fish
@@ -1,17 +1,22 @@
-function _fish_gen_cleanup_fn --argument name body
+. (status dirname)/_error.function.fish
+
+# workaround for `status filename` not returning the script that sourced
+[ (count $argv) -eq 1 ] || _error 'Need to supply script name to source command'
+set current_filename $argv[1]
+
+if [ "$current_filename" = "Standard input" ]
+  set -e current_filename
+else
+  set current_filename (systemd-escape --path (basename $current_filename))
+end
+
+function _defer --argument name body
   if [ (count $argv) -ne 2 ] || [ -z "$argv[1]" ] || [ -z "$argv[2]" ]
     echo Invalid params '(expected 2, name and body, both non-empty)'
     return 1
   end
 
-  set current_filename (status filename)
-  if [ "$current_filename" = "Standard input" ]
-    set -e current_filename
-  else
-    set current_filename (systemd-escape --path (basename $current_filename))
-
-  set name _cleanup_"$(string escape --style=var {$current_filename}_ | string collect
-      )"(string escape --style=var $name)_(uuidgen)
+  set name __cleanup__"$current_filename"__(string escape --style=var "$name")__(uuidgen)
 # need \; and \<newline> for line break escaping and command delimiting
   echo                                                                                          \
   function $name --on-event fish_exit                                                         \;\
diff --git a/vmaf.fish b/vmaf.fish
index eb38d32..925517e 100755
--- a/vmaf.fish
+++ b/vmaf.fish
@@ -1,43 +1,64 @@
 #!/usr/bin/env fish
 
+set REPORT_EXT              '_vmaf.txt'
+set FZF_PROMPT              'Select video files> '
+set FFMPEG_ARGS             (string trim '
 
-set FZF_PROMPT              'Select original file> '
-set FZF_STARTING_QUERY      'orig'
-set FFMPEG_ARGS (string trim '
-    -i $vid -i $ORIG -filter_complex \
-        "[0:v]setpts=PTS-STARTPTS[distorted]; \
-         [1:v]setpts=PTS-STARTPTS[ref]; \
-         [distorted][ref]libvmaf=feature='name=psnr':n_threads=8:log_fmt=json"
+#!/usr/bin/env fish
+set vid $argv[1]
+set ORIG $argv[2]
+ffmpeg -i $vid -i $ORIG -filter_complex \
+    "[0:v]setpts=PTS-STARTPTS[distorted]; \
+     [1:v]setpts=PTS-STARTPTS[ref]; \
+     [distorted][ref]libvmaf=feature=\'name=psnr\':n_threads=10:log_fmt=json" \
+    -f null - &| tee $vid"'$REPORT_EXT'"
 ' | string collect)
 
-
-. (dirname (status --current-filename))/helpers/_error.function.fish
+set HELPERS (dirname (status --current-filename))/helpers/
+. $HELPERS/_error.function.fish
+. $HELPERS/_defer.function.fish (status filename)
 
 switch (count $argv)
 case 1
-  set ORIG $argv
+  set ORIG (path normalize $argv)
 case \*
   _error 'need original video' argv[3..]
 end
 
-set ORIG (realpath (fzf --prompt $FZF_PROMPT --query $FZF_STARTING_QUERY))
 set -q MAXDEPTH || set MAXDEPTH 1
 
-set VIDEO_VILES (find . -maxdepth $MAXDEPTH -type f | filter_by_video | string match )
+set VIDEO_FILES_PRE (find . -maxdepth $MAXDEPTH -type f | filter_by_video | string match -v $ORIG)
+
+for f in $VIDEO_FILES_PRE
+  if [ (path resolve $f) != (path resolve $ORIG) ] && [ ! -e "$f""$REPORT_EXT" ]
+    set VIDEO_FILES_NEW $VIDEO_FILES_NEW $f
+  end
+end
+
+set VIDS_SELECTED (string split \n $VIDEO_FILES_NEW | fzf --prompt $FZF_PROMPT --multi || _error 'canceled by Ctrl+C' VIDEO_FILES_PRE VIDEO_FILES_NEW)
+
+set args_tmp (mktemp --suffix .fish); _defer ffmpeg_args "rm -rf $args_tmp"
+chmod +x $args_tmp
+echo $FFMPEG_ARGS >$args_tmp
+nano $args_tmp </dev/stdin
+[ ! -s $args_tmp ] && _error "ffmpeg args were empty"
+set ffmpeg_args (string collect <$args_tmp)
+echo $ffmpeg_args
 
-for vid in (find . -maxdepth $MAXDEPTH -type f | filter_by_video)
+
+for vid in $VIDS_SELECTED
   set vid (realpath $vid)
   [ -r "$vid" ] || _error "sanity check failed: video '$vid' not readable" ORIG PWD MAXDEPTH
 
-  set output_file {$vid}_vmaf.txt
-  echo touch $output_file
-  #TODO comment in [ -w "$output_file" ] || _error "cannot write to '$output_file'" vid
+  set output_file "$vid""$REPORT_EXT"
+
+# touch is annoying in error case
+#  touch $output_file
+#  [ -w "$output_file" ] || _error "cannot write to '$output_file'" vid
 
-  set args_tmp (mktemp --suffix .fish); function ___cleanup_args_tmp --on-event fish_exit; rm -rf $args_tmp; end
-  echo $FFMPEG_ARGS >$args_tmp
-  nano $args_tmp
-  [ ! -s $args_tmp ] && _error 
+  set --local
 
-  echo $ffmpeg_args
+#  set ffmpeg_args (echo $ffmpeg_args | string replace --all \$vid (string escape $vid) | string replace --all \$ORIG (string escape $ORIG) | string collect)
 
+  $args_tmp $vid $ORIG
 end
-- 
GitLab