Skip to content

Commit

Permalink
Merge pull request #349 from Lezek123/checkvideo-fix
Browse files Browse the repository at this point in the history
CheckVideo fix + test
  • Loading branch information
Lezek123 authored Dec 6, 2024
2 parents 3d3dd6d + 6ca4f9d commit 5371fa9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
15 changes: 11 additions & 4 deletions src/services/youtube/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,18 +518,21 @@ export class YoutubeClient implements IYoutubeApi {
this.logger.debug(`Checking video`, { proxy, videoUrl })

const { maxVideoSizeMB } = this.config.sync.limits || {}
const response = await create(`proxychains ${YT_DLP_PATH}`)(videoUrl, {
listFormats: true,
const executable = proxy ? `proxychains ${YT_DLP_PATH}` : YT_DLP_PATH
const response = await create(executable)(videoUrl, {
dumpJson: true,
simulate: true,
noWarnings: true,
abortOnError: true,
printJson: true,
format: this.getAcceptedFormats().join('/'),
retries: 0,
forceIpv4: true,
proxy,
maxFilesize: maxVideoSizeMB ? `${maxVideoSizeMB}M` : undefined,
})
if (typeof response === 'string') {
throw new Error(`yt-dlp response was a string: ${(response as string).slice(0, 200)} (...)`)
}
return response
}

Expand All @@ -540,7 +543,8 @@ export class YoutubeClient implements IYoutubeApi {
this.logger.debug(`Downloading video`, { proxy, videoUrl, preDownloadSleepTime })

const { maxVideoSizeMB } = this.config.sync.limits || {}
const response = await create(`proxychains ${YT_DLP_PATH}`)(videoUrl, {
const executable = proxy ? `proxychains ${YT_DLP_PATH}` : YT_DLP_PATH
const response = await create(executable)(videoUrl, {
noWarnings: true,
abortOnError: true,
printJson: true,
Expand All @@ -555,6 +559,9 @@ export class YoutubeClient implements IYoutubeApi {
maxFilesize: maxVideoSizeMB ? `${maxVideoSizeMB}M` : undefined,
// noWaitForVideo: true, // our version of youtube-dl-exec is not aware of this flag
})
if (typeof response === 'string') {
throw new Error(`yt-dlp response was a string: ${(response as string).slice(0, 200)} (...)`)
}
return response
}

Expand Down
44 changes: 29 additions & 15 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import ffmpegInstaller from '@ffmpeg-installer/ffmpeg'
import ytdl from 'youtube-dl-exec'
import assert from 'assert'
import { LoggingService } from './services/logging'
import { YoutubeClient } from './services/youtube/api'
import { ConfigParserService } from './utils/configParser'
import path from 'path'

async function downloadVideo(videoUrl: string, outPath: string): ReturnType<typeof ytdl> {
const response = await ytdl(videoUrl, {
noWarnings: true,
printJson: true,
format: 'bv[height<=1080][ext=mp4]+ba[ext=m4a]/bv[height<=1080][ext=webm]+ba[ext=webm]/best[height<=1080]',
output: `${outPath}/%(id)s.%(ext)s`,
ffmpegLocation: ffmpegInstaller.path,
})
console.log('video downloaded', outPath, videoUrl)
return response
const configParser = new ConfigParserService(path.join(__dirname, '../config.yml'))
const config = configParser.parse()
const logging = LoggingService.withCLIConfig()
const youtubeClient = new YoutubeClient(config, logging)

async function main() {
const { downloadsDir } = config.sync
assert(downloadsDir)
const videos = process.argv.slice(2)
await Promise.all(videos.map(async (v) => {
const checkResp = await youtubeClient.checkVideo(v)
if (typeof checkResp === 'string') {
console.log((checkResp as any).slice(0, 200))
}
assert(typeof checkResp === 'object', `Check: Expected object, got ${typeof checkResp}`)
assert(checkResp.format, 'Check: Missing format')
assert(checkResp.filesize_approx, 'Check: Missing filesize_approx')
const downloadResp = await youtubeClient.downloadVideo(v, downloadsDir)
assert(typeof downloadResp === 'object', `Download: Expected object, got ${typeof downloadResp}`)
assert(checkResp.ext, 'Download: Missing ext')
assert(checkResp.format, 'Download: Missing format')
}))
console.error("All videos successfully downloaded!")
}

downloadVideo('https://youtube.com/watch?v=cU7AAUNACVY', '/home/ubuntu/youtube-synch/')
.then(console.log)
.catch(error => console.error('Error downloading video:', error));
main().catch(e => console.error(e))

0 comments on commit 5371fa9

Please sign in to comment.