-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubtitle_Generator.ny
107 lines (93 loc) · 3.27 KB
/
Subtitle_Generator.ny
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
;nyquist plug-in
;version 4
;type tool analyze
;name "Subtitle Generator"
;manpage "Subtitle_Generator"
;debugbutton enabled
;author "Cheng Huaiyu"
;release 1.0.0
;copyright "Released under terms of the GNU General Public License version 2"
$control filename "Export Label Track to File:" file "Select a file" "*default*/subtitles" "SRT file|*.srt;*.SRT|LRC files|*.lrc;*.LRC" "save,overwrite"
;; Return number as string with at least 2 digits
(defun pad (num)
(format nil "~a~a" (if (< num 10) "0" "") num)
)
;; Return number as string with at least 3 digits
(defun pad3 (num)
(format nil "~a~a" (if (< num 100) "00" (if (< num 100) "0" "")) num)
)
;; Format time (seconds) as hh:mm:ss,xxx
(defun srt-time-format (sec)
(let* ((seconds (truncate sec))
(hh (truncate (/ seconds 3600)))
(mm (truncate (/ (rem seconds 3600) 60)))
(ss (rem seconds 60))
(xxx (round (* (- sec seconds) 1000))))
(format nil "~a:~a:~a,~a" (pad hh) (pad mm) (pad ss) (pad3 xxx))
)
)
;; Format time (seconds) as mm:ss.xx
(defun lrc-time-format (sec)
(let* ((seconds (truncate sec))
(mm (truncate (/ seconds 60)))
(ss (rem seconds 60))
(xx (round (* (- sec seconds) 100))))
(format nil "~a:~a.~a" (pad mm) (pad ss) (pad xx)))
)
; generate srt format subtitle
(defun label-to-srt (labels)
;; subtitle index
(let ((srt "")
(ind 0))
(dolist (label labels)
(setq ind (1+ ind))
(setf timeS (srt-time-format (first label)))
(setf timeE (srt-time-format (second label)))
(string-append srt (format nil "~a~%~a --> ~a~%~a~%~%" ind timeS timeE (third label)))
)
(format nil srt)
)
)
;; generate mp3 lyric
(defun label-to-lrc (labels)
(setf lrc "")
(string-append lrc "[ar:Lyrics artist]\n"
"[al:Album where the song is from]\n"
"[ti:Lyrics (song) title]\n"
"[au:Creator of the Songtext]\n"
"[length:How long the song is]\n"
"[by:Creator of the LRC file]\n"
"[offset:+/- Overall timestamp adjustment in milliseconds, + shifts time up, - shifts down]\n"
"[re:The player or editor that created the LRC file]\n"
"[ve:version of program]\n\n"
)
(dolist (label labels)
(setf timeS (lrc-time-format (first label)))
(string-append lrc (format nil "[~a] ~s~%" timeS (third label)))
)
(format nil lrc)
)
;; Return file extension or empty string
(defun get-file-extension (fname)
(let ((n (1- (length fname)))
(ext ""))
(do ((i n (1- i)))
((= i 0) ext)
(when (char= (char fname i) #\.)
(setf ext (subseq fname i))
(return ext)
)
)
)
)
;; Get labels from first label track
(setf labels (second (first (aud-get-info "labels"))))
(setf file-ext (string-upcase (get-file-extension filename)))
;; detect file extension to determine which format to export
(setf txt (if (string= ".LRC" file-ext)
(label-to-lrc labels)
(label-to-srt labels)))
(setf fp (open filename :direction :output))
(format fp "~a" txt)
(close fp)
(format nil "File exported successfully to: ~a" filename)