From 8d2ef40cd102d34e83a23f2dd663cd04bda45acb Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sun, 29 Jun 2025 18:02:16 +1000 Subject: [emacs] emms get duration using ffprobe --- emacs/.emacs.d/lisp/my/my-emms.el | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'emacs/.emacs.d/lisp') diff --git a/emacs/.emacs.d/lisp/my/my-emms.el b/emacs/.emacs.d/lisp/my/my-emms.el index 50c1310..c2d2eaa 100644 --- a/emacs/.emacs.d/lisp/my/my-emms.el +++ b/emacs/.emacs.d/lisp/my/my-emms.el @@ -469,6 +469,7 @@ under /zzz-seren/." An up arrow at the first played in the current group, and a down arrow at the end of the track group." + (remove-overlays) (overlay-put (make-overlay (point) (point)) 'before-string (propertize @@ -485,7 +486,6 @@ arrow at the end of the track group." (defun my-emms-playlist-random-group () (interactive) (with-current-emms-playlist - (remove-overlays) (let ((random-line (1+ (random (count-lines (point-min) (point-max)))))) (goto-line random-line) (pcase-let ((`(,group-start . ,group-end) (my-emms-playlist-group-bounds))) @@ -613,5 +613,31 @@ If the last command is `emms-playlist-mode-play-smart', then set (cl-second (gethash k2 emms-score-hash))))) (message "Top 10: %s" (string-join (take 10 keys) "\n")))) +(defun my-emms-maybe-get-duration-for-current-track () + "Get duration for the current track. + +Can be used as a `emms-player-started-hook'" + (unless (emms-track-get (emms-playlist-current-selected-track) + 'info-playing-time) + (my-emms-info-ffprobe (emms-playlist-current-selected-track)))) + +(defun my-emms-info-ffprobe (track) + "Use ffprobe for urls to get duration. + +Call + +ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 + +on the url" + (when (eq (emms-track-type track) 'url) + (with-temp-buffer + (call-process "ffprobe" nil t nil "-v" "error" "-show_entries" + "format=duration" "-of" "default=noprint_wrappers=1:nokey=1" + (emms-track-name track)) + (let ((duration (string-trim (buffer-string)))) + (when (string-match-p "[0-9.]+" duration) + (emms-track-set track 'info-playing-time + (floor (string-to-number duration)))))))) + (provide 'my-emms) ;;; my-emms.el ends here -- cgit v1.2.3