Files
.emacs.d/lisp/fix-dirvish-preview.el

45 lines
2.0 KiB
EmacsLisp

;;; fix-dirvish-preview.el --- fix dirvish preview on windows -*- lexical-binding: t -*-
;;; Commentary:
;; Fix video-mtn preview issue on Windows
;; The problem: mtn generates thumbnails with filename like "video.mp4.jpg",
;; but dirvish expects MD5-based filename like "a1b2c3d4.jpg"
;;; Code:
(with-eval-after-load 'dirvish
;; Function to find mtn output file for a given video file in cache directory
(defun my-dirvish-find-mtn-output (video-file cache-dir ext)
"Find mtn-generated thumbnail for VIDEO-FILE in CACHE-DIR with extension EXT."
(let* ((file-base (file-name-base video-file))
;; mtn generates: [filename].[ext].jpg
(mtn-pattern (format "%s.%s.jpg" (regexp-quote file-base) ext))
(files (directory-files cache-dir t mtn-pattern)))
(car files)))
;; Advice to fix the sentinel after mtn generates the thumbnail
(defun my-dirvish-media--cache-sentinel-advice (orig-fun proc exit-code)
"Advice to rename mtn output to MD5 format after generation."
(let* ((video-path (process-get proc 'path))
(ext (downcase (or (file-name-extension video-path) "")))
(dv (dirvish-curr)))
(when (and (memq system-type '(ms-dos windows-nt))
(member ext dirvish-video-exts)
dv)
;; Try to find and rename mtn output to MD5 format
(let* ((width (dirvish-media--img-size (dv-preview-window dv)))
(cache (dirvish--img-thumb-name video-path width ".jpg"))
(cache-dir (dirvish--get-parent-path cache))
(mtn-file (my-dirvish-find-mtn-output video-path cache-dir ext)))
(when (and mtn-file (file-exists-p mtn-file) (not (file-exists-p cache)))
(rename-file mtn-file cache)))))
;; Call original function
(funcall orig-fun proc exit-code))
;; Apply advice
(advice-add 'dirvish-media--cache-sentinel :around #'my-dirvish-media--cache-sentinel-advice))
(provide 'fix-dirvish-preview)
;;; fix-dirvish-preview.el ends here