aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/youtube-dl.ts
diff options
context:
space:
mode:
authorkimsible <kimsible@users.noreply.github.com>2020-04-11 04:24:42 +0200
committerkimsible <kimsible@users.noreply.github.com>2020-04-14 14:39:30 +0200
commit50ad0a1c1699fb1799c9ba2a99bf888894f88df4 (patch)
tree52279eaa419c94b96b1bf9169d396270e6146aff /server/helpers/youtube-dl.ts
parent5def04e17f1df09a708622afe634ccac256eab86 (diff)
downloadPeerTube-50ad0a1c1699fb1799c9ba2a99bf888894f88df4.tar.gz
PeerTube-50ad0a1c1699fb1799c9ba2a99bf888894f88df4.tar.zst
PeerTube-50ad0a1c1699fb1799c9ba2a99bf888894f88df4.zip
Add getSubs to YoutubeDL video import
Diffstat (limited to 'server/helpers/youtube-dl.ts')
-rw-r--r--server/helpers/youtube-dl.ts36
1 files changed, 36 insertions, 0 deletions
diff --git a/server/helpers/youtube-dl.ts b/server/helpers/youtube-dl.ts
index 07c85797a..277422645 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,35 @@ 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 const subtitles = files.reduce((acc, filename) => {
65 const matched = filename.match(/\.([a-z]{2})\.(vtt|ttml)/i)
66
67 if (matched[1]) {
68 return [...acc, {
69 language: matched[1],
70 path: join(cwd, filename),
71 filename
72 }]
73 }
74 }, [])
75
76 return res(subtitles)
77 })
78 })
79 .catch(err => rej(err))
80 })
81}
82
48function downloadYoutubeDLVideo (url: string, extension: string, timeout: number) { 83function downloadYoutubeDLVideo (url: string, extension: string, timeout: number) {
49 const path = generateVideoImportTmpPath(url, extension) 84 const path = generateVideoImportTmpPath(url, extension)
50 let timer 85 let timer
@@ -185,6 +220,7 @@ function buildOriginallyPublishedAt (obj: any) {
185export { 220export {
186 updateYoutubeDLBinary, 221 updateYoutubeDLBinary,
187 downloadYoutubeDLVideo, 222 downloadYoutubeDLVideo,
223 getYoutubeDLSubs,
188 getYoutubeDLInfo, 224 getYoutubeDLInfo,
189 safeGetYoutubeDL, 225 safeGetYoutubeDL,
190 buildOriginallyPublishedAt 226 buildOriginallyPublishedAt