yb.download.ranges¶
Download just a time range of a video: parse start/end, build yt-dlp’s options.
Sourcing a sound effect or a clip from a long video should not mean downloading
the whole thing. start= / end= on yb.download.download_youtube_video()
and yb.download.download_youtube_audio() take seconds (7.9) or
timestamps ("0:07.9", "1:02:03"); this module turns them into
yt-dlp’s download_ranges option.
>>> parse_timestamp("1:02.5")
62.5
>>> TimeRange.of(start="0:07.9", end=8.75).label
'7.9-8.75'
>>> TimeRange.of(start=30).label
'30-end'
yt-dlp cuts a range by handing the stream to ffmpeg, so the range path
needs ffmpeg on PATH (or extra_opts={"ffmpeg_location": ...}). Cuts land
on keyframes unless precise_cuts=True (re-encodes, slower, frame-accurate).
When the system ffmpeg is a build that lacks what yt-dlp asks of it, point
ffmpeg_location at another one (moviepy’s bundled ffmpeg, for example).
Module Attributes
seconds, or a |
|
yt-dlp option names this module sets. |
|
Key under which a ranged download's span is recorded on |
|
beyond it a typo, not a video. |
Functions
yt-dlp's |
|
|
Seconds from a number or a |
|
The yt-dlp options that download only |
|
|
Classes
|
A span of a video, in seconds. |
- yb.download.ranges.DOWNLOAD_RANGES_OPT = 'download_ranges'¶
yt-dlp option names this module sets.
- yb.download.ranges.INFO_RANGE_KEY = 'download_range'¶
Key under which a ranged download’s span is recorded on
DownloadResult.info.
- yb.download.ranges.MAX_TIMESTAMP_SECONDS = 864000¶
beyond it a typo, not a video.
- Type:
Longest timestamp accepted (10 days)
- class yb.download.ranges.TimeRange(start=0.0, end=None)[source]¶
Bases:
objectA span of a video, in seconds.
end=Nonemeans “to the end”.- classmethod of(*, start=None, end=None)[source]¶
Build from user input;
Nonewhen neither bound is given.- Raises:
ValueError – On a bad timestamp, or
endnot afterstart.- Return type:
- yb.download.ranges.Timestamp = int | float | str¶
seconds, or a
[[h:]m:]s[.fraction]string.- Type:
A time as callers give it
- yb.download.ranges.default_range_func()[source]¶
yt-dlp’s
download_range_func(imported lazily: yt-dlp is optional).
- yb.download.ranges.parse_timestamp(value)[source]¶
Seconds from a number or a
"[[h:]m:]s[.fraction]"string.- Raises:
ValueError – If
valueis negative, not finite, or not a timestamp.- Return type:
- yb.download.ranges.range_opts(time_range, *, precise_cuts=False, range_func=None)[source]¶
The yt-dlp options that download only
time_range.
- yb.download.ranges.ranged_filename_template(template, time_range, *, precise_cuts=False)[source]¶
templatewith the range before the extension:"T (id) [7.9-8.75].mp4".Without it a clip would share the full video’s filename, and yt-dlp would take one for the other (“already downloaded”). A precise cut is named apart from a keyframe cut for the same reason. Only templates ending in
.%(ext)s(the default does) are changed; one written by hand is the caller’s to keep distinct.- Return type: