aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/youtube-dl.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/youtube-dl.ts')
-rw-r--r--server/helpers/youtube-dl.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/server/helpers/youtube-dl.ts b/server/helpers/youtube-dl.ts
index 07c85797a..6d2e6f6d1 100644
--- a/server/helpers/youtube-dl.ts
+++ b/server/helpers/youtube-dl.ts
@@ -20,6 +20,12 @@ export type YoutubeDLInfo = {
20 originallyPublishedAt?: Date 20 originallyPublishedAt?: Date
21} 21}
22 22
23export type YoutubeDLSubs = {
24 language: string
25 filename: string
26 path: string
27}[]
28
23const processOptions = { 29const processOptions = {
24 maxBuffer: 1024 * 1024 * 10 // 10MB 30 maxBuffer: 1024 * 1024 * 10 // 10MB
25} 31}
@@ -45,6 +51,40 @@ function getYoutubeDLInfo (url: string, opts?: string[]): Promise<YoutubeDLInfo>
45 }) 51 })
46} 52}
47 53
54function getYoutubeDLSubs (url: string, opts?: object): Promise<YoutubeDLSubs> {
55 return new Promise<YoutubeDLSubs>((res, rej) => {
56 const cwd = CONFIG.STORAGE.TMP_DIR
57 const options = opts || { all: true, format: 'vtt', cwd }
58
59 safeGetYoutubeDL()
60 .then(youtubeDL => {
61 youtubeDL.getSubs(url, options, (err, files) => {
62 if (err) return rej(err)
63
64 logger.debug('Get subtitles from youtube dl.', { url, files })
65
66 const subtitles = files.reduce((acc, filename) => {
67 const matched = filename.match(/\.([a-z]{2})\.(vtt|ttml)/i)
68
69 if (matched[1]) {
70 return [
71 ...acc,
72 {
73 language: matched[1],
74 path: join(cwd, filename),
75 filename
76 }
77 ]
78 }
79 }, [])
80
81 return res(subtitles)
82 })
83 })
84 .catch(err => rej(err))
85 })
86}
87
48function downloadYoutubeDLVideo (url: string, extension: string, timeout: number) { 88function downloadYoutubeDLVideo (url: string, extension: string, timeout: number) {
49 const path = generateVideoImportTmpPath(url, extension) 89 const path = generateVideoImportTmpPath(url, extension)
50 let timer 90 let timer
@@ -185,6 +225,7 @@ function buildOriginallyPublishedAt (obj: any) {
185export { 225export {
186 updateYoutubeDLBinary, 226 updateYoutubeDLBinary,
187 downloadYoutubeDLVideo, 227 downloadYoutubeDLVideo,
228 getYoutubeDLSubs,
188 getYoutubeDLInfo, 229 getYoutubeDLInfo,
189 safeGetYoutubeDL, 230 safeGetYoutubeDL,
190 buildOriginallyPublishedAt 231 buildOriginallyPublishedAt