Add alert package as requirement
[ffmpeg-utils.git] / ffmpeg-utils.el
blob717d45a76a2c1dbb4b56ff41b3492a544c2c372f
1 ;;; ffmpeg-utils.el --- FFmpeg command utilities wrappers -*- lexical-binding: t; -*-
3 ;;; Time-stamp: <2020-10-29 11:08:29 stardiviner>
5 ;; Authors: stardiviner <numbchild@gmail.com>
6 ;; Package-Requires: ((emacs "25.1") (alert "1.2") (transient "0.1.0") (osx-lib "0.1"))
7 ;; Package-Version: 0.1
8 ;; Keywords: multimedia
9 ;; homepage: https://repo.or.cz/ffmpeg-utils.git
11 ;; ffmpeg-utils.el is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; ffmpeg-utils.el is distributed in the hope that it will be useful, but WITHOUT
17 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 ;; License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Usage:
28 ;;; - [M-x ffmpeg-cut-clip]
30 ;;; Code:
32 (require 'notifications)
33 (require 'alert)
34 (require 'transient)
36 (defvar ffmpeg--output-filename nil
37 "A variable to store the command output filename which used in notification.")
39 (defcustom ffmpeg-notification-function 'ffmpeg-notification-default
40 "Specify the ffmpeg command process sentinel function."
41 :type 'function
42 :safe #'functionp
43 :group 'ffmpeg)
45 (defun ffmpeg-notification-default (&optional proc event)
46 "The ffmpeg command process sentinel notification function with args PROC, EVENT."
47 (setq mode-line-process nil) ; remove mode-line-process indicator.
48 (let ((msg (format "ffmpeg cut %s finished" (file-name-nondirectory ffmpeg--output-filename))))
49 (pcase system-type
50 ('gnu/linux
51 (cond
52 ((and (featurep 'dbus) (fboundp 'notifications-notify))
53 (notifications-notify :title "Emacs ffmpeg-utils.el" :body msg))))
54 ('darwin
55 (cond
56 ((featurep 'alert)
57 (let ((msg "Emacs ffmpeg-utils.el process finished."))
58 (cl-case system-type
59 (darwin
60 (ns-do-applescript (format "say \"%s\"" msg))))
61 (alert msg :title "Emacs ffmpeg-utils.el")))
62 ((and (featurep 'osx-lib) (bound-and-true-p osx-lib-start-terminal))
63 (require 'osx-lib)
64 (osx-lib-notify2 "Emacs ffmpeg-utils.el" msg))
65 ((fboundp 'ns-do-applescript)
66 (ns-do-applescript
67 (format "display notification \"%s\" with title \"%s\""
68 msg "Emacs ffmpeg-utils.el")))
69 ((executable-find "osascript")
70 ;; (start-process
71 ;; "emacs-timer-notification" nil
72 ;; "osascript"
73 ;; (format "-e 'display notification \"%s\" with title \"%s\"'" msg "Emacs ffmpeg-utils.el"))
74 (shell-command
75 (format "osascript -e 'display notification \"%s\" with title \"%s\"'"
76 "yes" "Emacs ffmpeg-utils.el")))))
77 (_ (message (format "Emacs ffmpeg-utils.el: %s" msg))))))
79 (defun ffmpeg--run-command (arglist)
80 "Construct ffmpeg command with ARGLIST, SENTINEL-FUNC and BODY."
81 (make-process
82 :name "ffmpeg"
83 :command (append '("ffmpeg") arglist) ; <------------- problem here
84 :buffer "*ffmpeg*"
85 :sentinel ffmpeg-notification-function))
87 (defun ffmpeg-mode-line-running-indicator (msg)
88 "Display a running indicator on mode-line."
89 (setq mode-line-process
90 (concat " "
91 (propertize (or msg "ffmpeg running...")
92 'font-lock-face 'mode-line-highlight)))
93 (force-mode-line-update t))
95 ;;; ffmpeg command option "-t" accept seconds like 57 as value.
96 (defun ffmpeg--subtract-timestamps (start-timestamp end-timestamp)
97 "Subtract END-TIMESTAMP with START-TIMESTAMP."
98 (time-subtract
99 (encode-time
100 (parse-time-string ; (SECOND MINUTE HOUR DAY MONTH YEAR IGNORED DST ZONE)
101 (concat "2020-01-01" " " end-timestamp)))
102 (encode-time
103 (parse-time-string
104 (concat "2020-01-01" " " start-timestamp)))))
106 ;; (ffmpeg--subtract-timestamps "00:11:25" "00:12:12")
108 (defun ffmpeg-cut-clip (input-filename start-timestamp end-timestamp output-filename)
109 "Cut clip of media INPUT-FILENAME between START-TIMESTAMP END-TIMESTAMP and output to OUTPUT-FILENAME.
111 Support read timestamp begin/end range in format like this: 00:17:23 -- 00:21:45."
112 (interactive (if (region-active-p)
113 ;; regexp spec detect "00:17:23 -- 00:21:45"
114 (let* ((time-range (split-string
115 (buffer-substring-no-properties (region-beginning) (region-end))
116 " -- "))
117 (timestamp-begin (car time-range))
118 (timestamp-end (cadr time-range)))
119 (list
120 (expand-file-name
121 (substring-no-properties
122 (read-file-name "FFmpeg input filename: " nil nil 'confirm-after-completion)))
123 timestamp-begin
124 timestamp-end
125 (expand-file-name
126 (substring-no-properties
127 (read-file-name "FFmpeg output filename: ")))))
128 (list
129 (read-file-name "FFmpeg input filename: ")
130 (read-string "FFmpeg start timestamp: ")
131 (read-string "FFmpeg end timestamp: ")
132 (read-file-name "FFmpeg output filename: "))))
133 (when (region-active-p) (deactivate-mark) (forward-line) (newline) (forward-line -1))
134 (ffmpeg-mode-line-running-indicator "ffmpeg cut video clip")
135 (setq ffmpeg--output-filename output-filename)
136 (let ((input-type (file-name-extension input-filename))
137 (output-type (file-name-extension output-filename)))
138 (if output-type
139 ;; output filename specified video file extension.
140 (if (string-equal output-type input-type) ; if output extension is same as input
141 output-filename ; keep output original extension
142 (if (yes-or-no-p "Whether keep output video format extension? ")
143 output-filename
144 (setq output-filename (format "%s.%s" (file-name-sans-extension output-filename) input-type))))
145 ;; output filename has NOT specific video file extension.
146 (setq output-filename (format "%s.%s" output-filename input-type)))
147 ;; "ffmpeg -i input-filename -ss start-timestamp -t time-timestamp -codec copy output-filename"
148 (ffmpeg--run-command
149 `("-i" ,input-filename
150 "-ss" ,start-timestamp
151 "-t" ,(number-to-string (ffmpeg--subtract-timestamps start-timestamp end-timestamp))
152 "-codec" "copy"
153 ,output-filename))))
155 ;; (define-transient-command ffmpeg-transient ()
156 ;; "ffmpeg transient commands"
157 ;; ["Video"
158 ;; ("c" "Cut video clip" ffmpeg-cut-clip)]
159 ;; ["Audio"
160 ;; ("c" "Cut audio clip" ffmpeg-cut-clip)])
164 (provide 'ffmpeg-utils)
166 ;;; ffmpeg-utils.el ends here