diff options
author | Chocobozzz <me@florianbigard.com> | 2018-02-12 11:02:14 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-02-12 11:11:38 +0100 |
commit | e78720386f9a3187f30c0dc74a27ced837977ca9 (patch) | |
tree | f6f375221ac4914088ab0ad580099d904e829ffd /server/tools | |
parent | 64586951deda01365c14451f463a0a6c41e76213 (diff) | |
download | PeerTube-e78720386f9a3187f30c0dc74a27ced837977ca9.tar.gz PeerTube-e78720386f9a3187f30c0dc74a27ced837977ca9.tar.zst PeerTube-e78720386f9a3187f30c0dc74a27ced837977ca9.zip |
Improve youtube import script
Diffstat (limited to 'server/tools')
-rw-r--r-- | server/tools/import-youtube.ts | 73 |
1 files changed, 39 insertions, 34 deletions
diff --git a/server/tools/import-youtube.ts b/server/tools/import-youtube.ts index b4405c452..d57798d5b 100644 --- a/server/tools/import-youtube.ts +++ b/server/tools/import-youtube.ts | |||
@@ -11,7 +11,7 @@ program | |||
11 | .option('-u, --url <url>', 'Server url') | 11 | .option('-u, --url <url>', 'Server url') |
12 | .option('-U, --username <username>', 'Username') | 12 | .option('-U, --username <username>', 'Username') |
13 | .option('-p, --password <token>', 'Password') | 13 | .option('-p, --password <token>', 'Password') |
14 | .option('-y, --youtube-url <directory>', 'Youtube URL') | 14 | .option('-y, --youtube-url <youtubeUrl>', 'Youtube URL') |
15 | .parse(process.argv) | 15 | .parse(process.argv) |
16 | 16 | ||
17 | if ( | 17 | if ( |
@@ -45,6 +45,9 @@ async function run () { | |||
45 | youtubeDL.getInfo(program['youtubeUrl'], [ '-j', '--flat-playlist' ], async (err, info) => { | 45 | youtubeDL.getInfo(program['youtubeUrl'], [ '-j', '--flat-playlist' ], async (err, info) => { |
46 | if (err) throw err | 46 | if (err) throw err |
47 | 47 | ||
48 | // Normalize utf8 fields | ||
49 | info = info.map(i => normalizeObject(i)) | ||
50 | |||
48 | const videos = info.map(i => { | 51 | const videos = info.map(i => { |
49 | return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title } | 52 | return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title } |
50 | }) | 53 | }) |
@@ -60,54 +63,37 @@ async function run () { | |||
60 | }) | 63 | }) |
61 | } | 64 | } |
62 | 65 | ||
63 | function processVideo (videoUrlName: { name: string, url: string }) { | 66 | function processVideo (video: { name: string, url: string }) { |
64 | return new Promise(async res => { | 67 | return new Promise(async res => { |
65 | const result = await searchVideo(program['url'], videoUrlName.name) | 68 | const result = await searchVideo(program['url'], video.name) |
69 | |||
70 | console.log('############################################################\n') | ||
71 | |||
66 | if (result.body.total !== 0) { | 72 | if (result.body.total !== 0) { |
67 | console.log('Video "%s" already exist, don\'t reupload it.\n', videoUrlName.name) | 73 | console.log('Video "%s" already exists, don\'t reupload it.\n', video.name) |
68 | return res() | 74 | return res() |
69 | } | 75 | } |
70 | 76 | ||
71 | const video = youtubeDL(videoUrlName.url) | 77 | const path = join(__dirname, new Date().getTime() + '.mp4') |
72 | let videoInfo | ||
73 | let videoPath: string | ||
74 | 78 | ||
75 | video.on('error', err => console.error(err)) | 79 | console.log('Downloading video "%s"...', video.name) |
76 | 80 | ||
77 | let size = 0 | 81 | youtubeDL.exec(video.url, [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]', '-o', path ], {}, async (err, output) => { |
78 | video.on('info', info => { | 82 | if (err) return console.error(err) |
79 | videoInfo = info | ||
80 | size = info.size | ||
81 | 83 | ||
82 | videoPath = join(__dirname, size + '.mp4') | 84 | console.log(output.join('\n')) |
83 | console.log('Creating "%s" of video "%s".', videoPath, videoInfo.title) | ||
84 | 85 | ||
85 | video.pipe(createWriteStream(videoPath)) | 86 | youtubeDL.getInfo(video.url, async (err, videoInfo) => { |
86 | }) | 87 | if (err) return console.error(err) |
87 | 88 | ||
88 | let pos = 0 | 89 | await uploadVideoOnPeerTube(normalizeObject(videoInfo), path) |
89 | video.on('data', chunk => { | ||
90 | pos += chunk.length | ||
91 | // `size` should not be 0 here. | ||
92 | if (size) { | ||
93 | const percent = (pos / size * 100).toFixed(2) | ||
94 | writeWaitingPercent(percent) | ||
95 | } | ||
96 | }) | ||
97 | 90 | ||
98 | video.on('end', async () => { | 91 | return res() |
99 | await uploadVideoOnPeerTube(videoInfo, videoPath) | 92 | }) |
100 | |||
101 | return res() | ||
102 | }) | 93 | }) |
103 | }) | 94 | }) |
104 | } | 95 | } |
105 | 96 | ||
106 | function writeWaitingPercent (p: string) { | ||
107 | cursorTo(process.stdout, 0) | ||
108 | process.stdout.write(`waiting ... ${p}%`) | ||
109 | } | ||
110 | |||
111 | async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string) { | 97 | async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string) { |
112 | const category = await getCategory(videoInfo.categories) | 98 | const category = await getCategory(videoInfo.categories) |
113 | const licence = getLicence(videoInfo.license) | 99 | const licence = getLicence(videoInfo.license) |
@@ -153,3 +139,22 @@ function getLicence (licence: string) { | |||
153 | 139 | ||
154 | return undefined | 140 | return undefined |
155 | } | 141 | } |
142 | |||
143 | function normalizeObject (obj: any) { | ||
144 | const newObj: any = {} | ||
145 | |||
146 | for (const key of Object.keys(obj)) { | ||
147 | // Deprecated key | ||
148 | if (key === 'resolution') continue | ||
149 | |||
150 | const value = obj[key] | ||
151 | |||
152 | if (typeof value === 'string') { | ||
153 | newObj[key] = value.normalize() | ||
154 | } else { | ||
155 | newObj[key] = value | ||
156 | } | ||
157 | } | ||
158 | |||
159 | return newObj | ||
160 | } | ||