scicloj / wolframite

An interface between Clojure and Wolfram Language (the language of Mathematica)
https://scicloj.github.io/wolframite/
Mozilla Public License 2.0
56 stars 2 forks source link

[Fix #118] Display Video output with Kindly #131

Open holyjak opened 1 month ago

holyjak commented 1 month ago

Issue: Wolfram may return (Video <path to file> ...) and we want to be able to display it nicely in a Clay or compatible notebook.

Fix: Add ns with a fn to try add display metadata to all results, call it from eval.

light-matters commented 1 month ago

This is not currently working for me.

light-matters commented 1 month ago

I am first struggling to display any video with kindly and also because VideoTrim returns a slightly different format. I'm working on the first issue first though.

light-matters commented 1 month ago

The format (k/video {:src "./resources/video/Baby.mp4"}) works for me rather than file://...

light-matters commented 1 month ago

We would also need to somehow specify the default path to Wolfram video operations (~/Documents/Wolfram/Video) or add the folder dynamically.

light-matters commented 1 month ago
(defn- kind<-Video
  "Takes a Wolfram-like `(video url ...)` expression and returns an appropriate video kind.

  NOTE: Take care with the capital letter. This is to distinguish a Wolfram `Video` from other types."
  [expr]
  (k/video {:src (second expr)}))

(defn maybe-add-kindly-meta [expr]
  {:pre [expr]}
  (if (instance? IObj expr)
    (cond
      (= (head expr) 'Video)
      (k/fn expr
        {:kindly/f kind<-Video}))

    expr))

Seems to work for me for a 'normal' (Video ...). Not sure why it's not working for the output of VideoTrim though.

(-> "./resources/video/Baby.mp4"
    w/Video
    ;; (w/VideoTrim [6 7])
    wl/eval
    );; => works!, i.e. video is displayed nicely in Clay window

(-> "./resources/video/Baby.mp4"
    w/Video
    (w/VideoTrim [6 7])
    wl/eval);; => doesn't work, i.e. "No video with supported format and MIME type found." is seen in an image in Clay (instead of the video).

And, in case you're wondering, the original video is 22 seconds long.

If I manually take the cider output of the above function though and evaluate it through Clay in the usual way then it works. Is it something to do with the video being examined before it's created or anything like that?

light-matters commented 1 month ago

Interestingly,

(-> (w/do (-> "./resources/video/Baby.mp4"
              w/Video
              (w/VideoTrim [6 7])
              (->> (w/= 'thing)))
          (-> 'thing))
    wl/eval) ;; => doesn't work

(-> "./resources/video/Baby.mp4"
    w/Video
    (w/VideoTrim [6 7])
    (->> (w/= 'thing))
    wl/eval)

(-> 'thing
    wl/eval) ;; => does work!
holyjak commented 1 month ago

I bet ☝️works because the latter returns whatever = returns, which is the RHS, which is the VideoTrim output. But the former could also be simplified to two calls (wl/eval ...) (wl/eval 'thing) and then it is perhaps not surprising it only returns 'thing.