|
1 | 1 | import spawn from 'nano-spawn'
|
2 | 2 | import fs from 'fs'
|
| 3 | +import { video, subtitles } from './yt-dlp.js' |
3 | 4 |
|
4 | 5 | export const ytUrlRegExp = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/watch\?v=([^&]+).*/
|
5 | 6 |
|
6 | 7 | const fetchYoutubeHeaders = {
|
7 | 8 | 'Accept-Language': 'en',
|
8 | 9 | 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36'
|
9 | 10 | }
|
10 |
| -const subtitlesYTDLPArgs = `--write-subs --write-auto-subs --sub-format vtt --convert-subs srt -k` |
11 |
| -const videoYTDLPArgs = quality => `--concurrent-fragments 5 --newline --progress --progress-delta 1 --merge-output-format webm/mp4 -f bestvideo[height<=${quality}]+bestaudio/best[height<=${quality}] --sponsorblock-remove sponsor` |
12 |
| - |
13 |
| - |
14 |
| -function getOptionalCookiesPath() { |
15 |
| - if (fs.existsSync('/app/cookies.txt')) return '/app/cookies.txt' |
16 |
| - if (fs.existsSync('./cookies.txt')) return './cookies.txt' |
17 |
| - console.log('cookies not set') |
18 |
| - return |
19 |
| -} |
20 | 11 |
|
21 | 12 | export async function downloadVideo(id, repo, callback = () => {}) {
|
22 | 13 | return new Promise(async (resolve, reject) => {
|
23 | 14 | if (isUnsupportedUrl(id)) return reject(new Error('unsupported url'))
|
24 |
| - |
25 | 15 | if (isYouTubeUrl(id)) id = extractIdFromUrl(id)
|
26 | 16 | try {
|
27 |
| - const cookiesPath = getOptionalCookiesPath() |
28 |
| - let cookiesOption = cookiesPath ? `--cookies ${cookiesPath}` : '' |
29 | 17 | const quality = repo.getVideoQuality()
|
30 |
| - const commandArgs = `-o ./data/videos/${id}.%(ext)s ${cookiesOption} ${videoYTDLPArgs(quality)} ${subtitlesYTDLPArgs} -- ${id}`.split(/ +/) |
31 |
| - console.log('running yt-dlp', commandArgs.join(' ')) |
32 | 18 | let location, format
|
33 |
| - for await (const line of spawn('yt-dlp', commandArgs)) { |
| 19 | + for await (const line of video(id, quality)) { |
34 | 20 | console.log(line)
|
35 | 21 | callback(line)
|
36 | 22 | if (line.startsWith('[Merger] Merging formats into')) {
|
@@ -66,13 +52,8 @@ export async function getVideoSubtitles (id, callback = () => {}) {
|
66 | 52 | return new Promise(async (resolve, reject) => {
|
67 | 53 | try {
|
68 | 54 | const transcriptPath = `./data/videos/${id}.en.srt`
|
69 |
| - const cookies = getOptionalCookiesPath() |
70 |
| - let cookiesOption = '' |
71 |
| - if (cookies) cookiesOption = `--cookies ./cookies.txt` |
72 | 55 | if (!fs.existsSync(transcriptPath)) {
|
73 |
| - const commandArgs = `-o ./data/videos/${id} --skip-download ${cookiesOption} ${subtitlesYTDLPArgs} -- ${id}`.split(/ +/) |
74 |
| - console.log('running yt-dlp', commandArgs.join(' ')) |
75 |
| - for await (const line of spawn('yt-dlp', commandArgs)) { |
| 56 | + for await (const line of subtitles(id)) { |
76 | 57 | console.log(line)
|
77 | 58 | callback(line)
|
78 | 59 | }
|
|
0 commit comments