from urllib.parse import urljoin

视频下载

B站

B站视频下载,可以使用you-get和ffmpeg配合下载

一些老视频可以直接you-get音视频一起下载,但新视频大概率是图像和声音分离的,需要使用ffmpeg处理

处理方式

1.手工

you-get

source .venv_study_notes/bin/activate
pip install you-get -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
usage: you-get [OPTION]... URL...

A tiny downloader that scrapes the web

options:
  -V, --version         Print version and exit
  -h, --help            Print this help message and exit

Dry-run options:
  (no actual downloading)

  -i, --info            Print extracted information
  -u, --url             Print extracted information with URLs
  --json                Print extracted URLs in JSON format

Download options:
  -n, --no-merge        Do not merge video parts
  --no-caption          Do not download captions (subtitles, lyrics, danmaku, ...)
  --post, --postfix     Postfix downloaded files with unique identifiers
  --pre, --prefix PREFIX
                        Prefix downloaded files with string
  -f, --force           Force overwriting existing files
  --skip-existing-file-size-check
                        Skip existing file without checking file size
  -F, --format STREAM_ID
                        Set video format to STREAM_ID
  -O, --output-filename FILE
                        Set output filename
  -o, --output-dir DIR  Set output directory
  -p, --player PLAYER   Stream extracted URL to a PLAYER
  -c, --cookies COOKIES_FILE
                        Load cookies.txt or cookies.sqlite
  -t, --timeout SECONDS
                        Set socket timeout
  -d, --debug           Show traceback and other debug info
  -I, --input-file FILE
                        Read non-playlist URLs from FILE
  -P, --password PASSWORD
                        Set video visit password to PASSWORD
  -l, --playlist        Prefer to download a playlist
  -a, --auto-rename     Auto rename same name different files
  -k, --insecure        ignore ssl errors
  -m, --m3u8            download video using an m3u8 url

Playlist optional options:
  --first FIRST         the first number
  --last LAST           the last number
  --size, --page-size PAGE_SIZE
                        the page size number

Proxy options:
  -x, --http-proxy HOST:PORT
                        Use an HTTP proxy for downloading
  -y, --extractor-proxy HOST:PORT
                        Use an HTTP proxy for extracting only
  --no-proxy            Never use a proxy
  -s, --socks-proxy HOST:PORT or USERNAME:PASSWORD@HOST:PORT
                        Use an SOCKS5 proxy for downloading

you-get http://xxx.com/BVxxxxx   # 直接下载到当前目录
you-get -o /Downloads http://xxx.com/BVxxxxx   # 下载到指定/Downloads目录
you-get -i http://xxx.com/BVxxxxx    # 查看视频信息,可以看到支持哪些格式
you-get -o /Downloads --format=flv720 http://xxx.com/BVxxxxx # 指定格式下载
you-get -o /Downloads -l http://xxx.com/BVxxxxx  # 指定视频列表进行下载

2.Python脚本

一般4K视频需要有VIP会员,且登录; 即使是1080p,至少也要正式会员登录。

import subprocess

video_url = ""
download_cmd = f"you-get -o ./ {video_url}"
download_list_cmd = f"you-get -o ./ -l {video_url}"
download_cookie_cmd = f"you-get -o ./ {video_url}"

subprocess.run(download_cmd,shell=True)

ref: 下载B站4k视频