Menu

Tree [8c312c] master /
 History

HTTPS access


File Date Author Commit
 .vscode 2024-08-07 dragomerlin dragomerlin [ac2812] Initial commit: sync from DmitryScaletta:d9efc7...
 images 2024-08-07 dragomerlin dragomerlin [ac2812] Initial commit: sync from DmitryScaletta:d9efc7...
 .editorconfig 2024-08-07 dragomerlin dragomerlin [ac2812] Initial commit: sync from DmitryScaletta:d9efc7...
 .gitignore 2024-08-07 dragomerlin dragomerlin [ac2812] Initial commit: sync from DmitryScaletta:d9efc7...
 .prettierrc.yml 2024-08-07 dragomerlin dragomerlin [ac2812] Initial commit: sync from DmitryScaletta:d9efc7...
 DOWNLOAD_PRIVATE_VIDEOS.md 2024-08-09 dragomerlin dragomerlin [6fd28e] Update string to match twitch-dlp-2 and use onl...
 README.md 2025-02-02 dragomerlin dragomerlin [8c312c] Make it work much better
 package.json 2024-08-09 dragomerlin dragomerlin [6fd28e] Update string to match twitch-dlp-2 and use onl...
 twitch-dlp-2.js 2025-02-02 dragomerlin dragomerlin [8c312c] Make it work much better

Read Me

twitch-dlp-2

Simple script for downloading twitch VODs from start during live broadcast.
Can also download some other VODs that are not live.

NOTICE

This is a fork of twitch-dlp aiming for some improvements:

  • Save all fragments in a subdirectory in current working directory with the format 'v{ID}'. That's for VODs only, if it's only live then saves to output file directly to where the script is.
  • Uses temporary files (fragments still ending in .part) to avoid regarding amid downloaded segments as complete.
  • Save fragments with pattern 0.ts_Frag1 where 0.ts are the names as appear in the m3u8. If there's init file will start like init.mp4_Frag0.
  • The output file is always overwritten if exists.
  • Does not use ffmpeg, since segments/fragments in .m3u8 are designed to be concatenated "as is".
  • Download fragments with extension either .mp4 or .ts, otherwise warn error.
  • All fragments must have the same extension, otherwise warn error.
  • Save the ouptut file with the same extension as the segments, not not assume it's always ts.
  • Handle M3U8 playlists with init file (for MP4 as header), by detecting the tag #EXT-X-MAP:URI=.
  • Avoid downloading or concatenating duplicate segments.
  • If new segments appear for the same position of already downloaded ones, probably they are muted ones, so it download them as _ALT but are not used for raw concatenation.
  • The list of files to be concatenated is saved to segments.txt in the download dir, using in each index (position) the first segment that appears there in the playlist.
  • For the timestamp createdAt, it tries also with +/- 1 second besides the obtained one, because sometimes the timestamp in the playlist URL and the one obtained from the API don't match by the second. In some cases there can be lots of seconds of difference.
  • Add some DEBUG messages to know what's going on.
  • The following Upload type IDs were used for testing:
  • 637343547: multiple duplicate segments with .ts extension in h264 codec.
  • 637388605: multiple duplicate segments with .mp4 extension in av1 codec, with init file.

twitch-batch-downloader is more feature rich for VODs that are not still live stream. Otherwise this is better.

Features

  • Download live VODs from start (--live-from-start)
  • Download private VODs (or if they were hidden during the broadcast)
  • Download sub-only VODs (ongoing streams)
  • Download hidden or finished sub-only VODs (see the instructions)
  • Watch channel status. If it becomes live, start downloading (--retry-streams DELAY)
  • Supports VOD links and channel links
  • Similar to yt-dlp (youtube-dl) syntax

Usage

Install Node.js v20 or newer.

node twitch-dlp-2.js 'LINK'

Examples

# Download a VOD from start using channel link, continue until stream ends
node twitch-dlp-2 https://www.twitch.tv/xqc --live-from-start

# Download a VOD. If it's live, continue until stream ends
node twitch-dlp-2 https://www.twitch.tv/videos/2022789761

# Download live stream from the current time using streamlink
node twitch-dlp-2 https://www.twitch.tv/xqc

# Download sub-only or hidden VOD
# Follow this instructions first:
# https://github.com/DmitryScaletta/twitch-dlp/blob/master/DOWNLOAD_PRIVATE_VIDEOS.md
node twitch-dlp-2 video:xqc_51582913581_1721686515

# Check every 60 seconds is channel live
# If it's live, start to download it using streamlink
node twitch-dlp-2 https://www.twitch.tv/xqc --retry-streams 60

# Check every 60 seconds is channel live
# If it's live, start to download it's VOD from start
node twitch-dlp-2 https://www.twitch.tv/xqc --retry-streams 60 --live-from-start

# Display available formats
node twitch-dlp-2 https://www.twitch.tv/videos/2022789761 -F

# Download specified format
node twitch-dlp-2 https://www.twitch.tv/videos/2022789761 -f 480p30

# Change output template
node twitch-dlp-2 https://www.twitch.tv/videos/2022789761 -o "%(title)s [%(id)s].%(ext)s"

# Limit download rate
node twitch-dlp-2 https://www.twitch.tv/videos/2022789761 -r 720k

Options

-h, --help                  Show this help message and exit
-f, --format FORMAT         Select format to download
                            Available formats:
                            - best: best quality (default)
                            - FORMAT: select format by format_id
-F, --list-formats          List available formats and exit
-o, --output OUTPUT         Output filename template
                            Available template variables:
                            - %(title)s
                            - %(id)s
                            - %(ext)s
                            - %(description)s
                            - %(duration)s
                            - %(uploader)s
                            - %(uploader_id)s
                            - %(upload_date)s
                            - %(release_date)s
                            - %(view_count)s
--live-from-start           Download live streams from the start
--retry-streams DELAY       Retry fetching the list of available
                            streams until streams are found
                            while waiting DELAY second(s)
                            between each attempt.
-r, --limit-rate RATE       Limit download rate to RATE
--keep-fragments            Keep fragments after downloading

Formats example

For VODs

┌─────────┬──────────────┬────────┬────────┐
│ (index) │ format_id    │ width  │ height │
├─────────┼──────────────┼────────┼────────┤
│ 0       │ 'Source'     │ '1920' │ '1080' │
│ 1       │ '720p60'     │ '1280' │ '720'  │
│ 2       │ '720p30'     │ '1280' │ '720'  │
│ 3       │ '480p30'     │ '852'  │ '480'  │
│ 4       │ '360p30'     │ '640'  │ '360'  │
│ 5       │ '160p30'     │ '284'  │ '160'  │
│ 6       │ 'Audio_Only' │ null   │ null   │
└─────────┴──────────────┴────────┴────────┘

For live streams (streamlink)

Available streams: audio_only, 160p (worst), 360p, 480p, 720p, 720p60, 1080p60 (best)
Available streams: audio_only, 160p (worst), 360p, 480p, 720p, 720p60_alt, 720p60 (best)

Requires

  • ffmpeg
  • curl (if using --limit-rate option)
  • streamlink (if downloading by channel link without --live-from-start)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.