Fix warning Unused lexical argument `proc' & `event'.
[ffmpeg-utils.git] / ffmpeg-utils.el
blob13dc62d7a03d333eb01ad10cf9078fd9709855ed
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") (transient "0.1.0"))
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 default ffmpeg command process sentinel notification function."
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 (osx-lib-notify2 "Emacs ffmpeg-utils.el" msg))
64 ((fboundp 'ns-do-applescript)
65 (ns-do-applescript
66 (format "display notification \"%s\" with title \"%s\""
67 msg "Emacs ffmpeg-utils.el")))
68 ((executable-find "osascript")
69 (start-process
70 "emacs-timer-notification" nil
71 "osascript" "-e"
72 (format "'display notification \"%s\" with title \"title\"'" msg "Emacs ffmpeg-utils.el")))))
73 (_ (message (format "Emacs ffmpeg-utils.el: %s" msg))))))
75 (defun ffmpeg--run-command (arglist)
76 "Construct ffmpeg command with ARGLIST, SENTINEL-FUNC and BODY."
77 (make-process
78 :name "ffmpeg"
79 :command (append '("ffmpeg") arglist) ; <------------- problem here
80 :buffer "*ffmpeg*"
81 :sentinel ffmpeg-notification-function))
83 (defun ffmpeg-mode-line-running-indicator (msg)
84 "Display a running indicator on mode-line."
85 (setq mode-line-process
86 (concat " "
87 (propertize (or msg "ffmpeg running...")
88 'font-lock-face 'mode-line-highlight)))
89 (force-mode-line-update t))
91 ;;; ffmpeg command option "-t" accept seconds like 57 as value.
92 (defun ffmpeg--subtract-timestamps (start-timestamp end-timestamp)
93 "Subtract END-TIMESTAMP with START-TIMESTAMP."
94 (time-subtract
95 (encode-time
96 (parse-time-string ; (SECOND MINUTE HOUR DAY MONTH YEAR IGNORED DST ZONE)
97 (concat "2020-01-01" " " end-timestamp)))
98 (encode-time
99 (parse-time-string
100 (concat "2020-01-01" " " start-timestamp)))))
102 ;; (ffmpeg--subtract-timestamps "00:11:25" "00:12:12")
104 (defun ffmpeg-cut-clip (input-filename start-timestamp end-timestamp output-filename)
105 "Cut clip of media INPUT-FILENAME between START-TIMESTAMP END-TIMESTAMP and output to OUTPUT-FILENAME.
107 Support read timestamp begin/end range in format like this: 00:17:23 -- 00:21:45."
108 (interactive (if (region-active-p)
109 ;; regexp spec detect "00:17:23 -- 00:21:45"
110 (let* ((time-range (split-string
111 (buffer-substring-no-properties (region-beginning) (region-end))
112 " -- "))
113 (timestamp-begin (car time-range))
114 (timestamp-end (cadr time-range)))
115 (list
116 (expand-file-name
117 (substring-no-properties
118 (read-file-name "FFmpeg input filename: " nil nil 'confirm-after-completion)))
119 timestamp-begin
120 timestamp-end
121 (expand-file-name
122 (substring-no-properties
123 (read-file-name "FFmpeg output filename: ")))))
124 (list
125 (read-file-name "FFmpeg input filename: ")
126 (read-string "FFmpeg start timestamp: ")
127 (read-string "FFmpeg end timestamp: ")
128 (read-file-name "FFmpeg output filename: "))))
129 (when (region-active-p) (deactivate-mark) (forward-line) (newline) (forward-line -1))
130 (ffmpeg-mode-line-running-indicator "ffmpeg cut video clip")
131 (setq ffmpeg--output-filename output-filename)
132 (let ((input-type (file-name-extension input-filename))
133 (output-type (file-name-extension output-filename)))
134 (if output-type
135 ;; output filename specified video file extension.
136 (if (string-equal output-type input-type) ; if output extension is same as input
137 output-filename ; keep output original extension
138 (if (yes-or-no-p "Whether keep output video format extension? ")
139 output-filename
140 (setq output-filename (format "%s.%s" (file-name-sans-extension output-filename) input-type))))
141 ;; output filename has NOT specific video file extension.
142 (setq output-filename (format "%s.%s" output-filename input-type)))
143 ;; "ffmpeg -i input-filename -ss start-timestamp -t time-timestamp -codec copy output-filename"
144 (ffmpeg--run-command
145 `("-i" ,input-filename
146 "-ss" ,start-timestamp
147 "-t" ,(number-to-string (ffmpeg--subtract-timestamps start-timestamp end-timestamp))
148 "-codec" "copy"
149 ,output-filename))))
151 (define-transient-command ffmpeg-transient ()
152 "ffmpeg transient commands"
153 ["Video"
154 ("c" "Cut video clip" ffmpeg-cut-clip)]
155 ["Audio"
156 ("c" "Cut audio clip" ffmpeg-cut-clip)])
160 (provide 'ffmpeg-utils)
162 ;;; ffmpeg-utils.el ends here