Skip to content

Commit

Permalink
Use streamlink as downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
HitomaruKonpaku committed Dec 25, 2024
1 parent a0096a9 commit 97d2b3f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
9 changes: 5 additions & 4 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ ENV NODE_ENV=production
VOLUME /app/logs
VOLUME /app/download

RUN apk add --no-cache curl
# RUN apk add --no-cache curl
RUN apk add --no-cache ffmpeg
RUN apk add --no-cache python3 py3-pip py3-websockets
# RUN apk add --no-cache python3 py3-pip py3-websockets
RUN apk add --no-cache streamlink

# Install latest yt-dlp
RUN curl -L -o /usr/local/bin/yt-dlp https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp
RUN chmod a+rx /usr/local/bin/yt-dlp
# RUN curl -L -o /usr/local/bin/yt-dlp https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp
# RUN chmod a+rx /usr/local/bin/yt-dlp

COPY --from=dependencies /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
Expand Down
63 changes: 45 additions & 18 deletions src/Downloader.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@
// eslint-disable-next-line camelcase
import child_process, { SpawnOptions } from 'child_process'
import { logger as baseLogger } from './logger'
import { configManager } from './modules/ConfigManager'
import winston from 'winston'
import DailyRotateFile from 'winston-daily-rotate-file'
import { LOGGER_DATE_PATTERN, LOGGER_DIR } from './constants/logger.constant'

const logger = baseLogger.child({ label: '[Downloader]' })
function getFileName() {
return `${process.env.NODE_ENV || 'dev'}.downloader.%DATE%`
}

const logger = winston.createLogger({
transports: [
new DailyRotateFile({
level: 'debug',
datePattern: LOGGER_DATE_PATTERN,
dirname: LOGGER_DIR,
filename: `${getFileName()}.log`,
}),
],
})

export class Downloader {
public static downloadUrl(
url: string,
options?: {
output?: string
formatSort?: string
// formatSort?: string
},
) {
const cmd = 'yt-dlp'
const args = []
const ytdlOptions = Array.from(configManager.config?.ytdlOptions || [])
args.push(...ytdlOptions)
if (options?.output && !['--output', '-o'].some((v) => ytdlOptions.includes(v))) {
args.push('--output', options.output)
}
if (options?.formatSort) {
args.push('--format-sort', options.formatSort)
}
// const cmd = 'yt-dlp'
const cmd = 'streamlink'
const args: string[] = []

// const ytdlOptions: string[] = Array.from(configManager.config?.ytdlOptions || [])
// args.push(...ytdlOptions)

// if (options?.output && !['--output', '-o'].some((v) => ytdlOptions.includes(v))) {
// args.push('--output', options.output)
// }

// if (options?.formatSort) {
// args.push('--format-sort', options.formatSort)
// }

args.push('--loglevel', 'debug')
args.push('--output', options?.output || './{author}/{time:%Y%m%d%H%M%S}-{id}.mp4')
args.push(url)
args.push('best')

logger.verbose(`${cmd} ${args.join(' ')}`)

const spawnOptions: SpawnOptions = {
Expand All @@ -39,16 +62,20 @@ export class Downloader {
if (cp.stdout) {
cp.stdout.setEncoding('utf8')
cp.stdout.on('data', (data) => {
const msg = data.toString()
logger.debug(msg)
const msg = data.toString().trim()
if (msg) {
logger.debug(msg)
}
})
}

if (cp.stderr) {
cp.stderr.setEncoding('utf8')
cp.stderr.on('data', (data) => {
const msg = data.toString()
logger.debug(msg)
const msg = data.toString().trim()
if (msg) {
logger.debug(msg)
}
})
}

Expand Down
11 changes: 10 additions & 1 deletion src/modules/TwitCastingCrawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ export class TwitCastingCrawler extends EventEmitter {
this.logger.warn(`${user.id} live: ${movieUrl}`)
this.sendWebhooks(user, movie)

const fileName = [
`[${user.id.replace(/[:]/g, '_')}]`,
`[${Util.getTimeString()}]`,
' ',
`(${movie.id})`,
'.mp4',
].join('')

const output = path.join(
configManager.getOutDir(),
'twitcasting',
`[%(uploader_id)s][${Util.getTimeString()}] %(title)s (%(id)s).%(ext)s`,
// `[%(uploader_id)s][${Util.getTimeString()}] %(title)s (%(id)s).%(ext)s`,
fileName,
)

const sleepMs = Number(process.env.TWITCASTING_DOWNLOAD_DELAY) || 0
Expand Down

0 comments on commit 97d2b3f

Please sign in to comment.