Files
wehub-resource-sync 9194ef5abd
Docs/Test Workflow / Test docs build (push) Failing after 0s
Check links & references / links-check (push) Failing after 1s
Pytest/Test Workflow / Import Test and Pytest Run (ubuntu-latest, 3.10) (push) Failing after 0s
Pytest/Test Workflow / Import Test and Pytest Run (ubuntu-latest, 3.11) (push) Failing after 0s
PR Conflict Labeler / main (push) Failing after 2s
Pytest/Test Workflow / Import Test and Pytest Run (ubuntu-latest, 3.12) (push) Failing after 2s
Pytest/Test Workflow / Import Test and Pytest Run (ubuntu-latest, 3.13) (push) Failing after 0s
Pytest/Test Workflow / Build this Package (push) Failing after 5s
Pytest/Test Workflow / Import Test and Pytest Run (macos-latest, 3.10) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (macos-latest, 3.11) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (macos-latest, 3.12) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (macos-latest, 3.13) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (windows-latest, 3.10) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (windows-latest, 3.11) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (windows-latest, 3.12) (push) Has been cancelled
Pytest/Test Workflow / Import Test and Pytest Run (windows-latest, 3.13) (push) Has been cancelled
Pytest/Test Workflow / testing-guardian (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:06:10 +08:00

62 lines
1.8 KiB
Python

import os
import sys
from typing import Any
import yt_dlp
from jsonargparse import auto_cli
from yt_dlp.utils import DownloadError
def _build_ydl_opts(output_path: str | None, file_name: str | None) -> dict[str, Any]:
out_dir = output_path or "."
if not os.path.exists(out_dir):
os.makedirs(out_dir)
name_template = file_name if file_name else "%(title)s.%(ext)s"
return {
"format": (
"bestvideo[ext=mp4][vcodec!*=av01][height<=2160]+bestaudio[ext=m4a]/"
"best[ext=mp4][vcodec!*=av01][height<=2160]/"
"bestvideo+bestaudio/best"
),
"merge_output_format": "mp4",
"outtmpl": os.path.join(out_dir, name_template),
"quiet": False,
"noplaylist": True,
}
def main(
url: str, output_path: str = "data/source", file_name: str = "video.mp4"
) -> None:
"""
Download a specific YouTube video by providing its URL.
Args:
url: The full URL of the YouTube video you wish to download.
output_path: Specifies the directory where the video will be saved.
file_name: Sets the name of the saved video file.
"""
# ssl._create_default_https_context = ssl._create_unverified_context
ydl_opts = _build_ydl_opts(output_path, file_name)
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
except DownloadError as err:
print(f"Download failed: {err}", file=sys.stderr)
sys.exit(1)
final_name = file_name if file_name else "the video title"
final_path = output_path if output_path else "current directory"
print(f"Download completed! Video saved as '{final_name}' in '{final_path}'.")
if __name__ == "__main__":
from jsonargparse import auto_cli, set_parsing_settings
set_parsing_settings(parse_optionals_as_positionals=True)
auto_cli(main, as_positional=False)