mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-09-05 07:28:04 +08:00
feat: 将下载视频时选择流的部分参数提取为配置 (#47)
This commit is contained in:
+1
-1
@@ -358,7 +358,7 @@ async def get_video(v: video.Video, page_id: int, tmp_video_path: Path, tmp_audi
|
|||||||
await amakedirs(video_path.parent, exist_ok=True)
|
await amakedirs(video_path.parent, exist_ok=True)
|
||||||
# 分析对应分p的视频流
|
# 分析对应分p的视频流
|
||||||
detector = video.VideoDownloadURLDataDetecter(await v.get_download_url(page_index=page_id))
|
detector = video.VideoDownloadURLDataDetecter(await v.get_download_url(page_index=page_id))
|
||||||
streams = detector.detect_best_streams()
|
streams = detector.detect_best_streams(**settings.stream.model_dump())
|
||||||
if detector.check_flv_stream():
|
if detector.check_flv_stream():
|
||||||
# 对于 flv,直接下载
|
# 对于 flv,直接下载
|
||||||
await download_content(streams[0].url, tmp_video_path)
|
await download_content(streams[0].url, tmp_video_path)
|
||||||
|
|||||||
+29
-10
@@ -1,7 +1,7 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from bilibili_api.video import VideoCodecs
|
from bilibili_api.video import AudioQuality, VideoCodecs, VideoQuality
|
||||||
from pydantic import BaseModel, Field, field_validator
|
from pydantic import BaseModel, Field, field_validator, root_validator
|
||||||
from pydantic_core import PydanticCustomError
|
from pydantic_core import PydanticCustomError
|
||||||
from typing_extensions import Annotated
|
from typing_extensions import Annotated
|
||||||
|
|
||||||
@@ -17,6 +17,26 @@ class SubtitleConfig(BaseModel):
|
|||||||
static_time: float = 10 # 静态弹幕持续时间
|
static_time: float = 10 # 静态弹幕持续时间
|
||||||
|
|
||||||
|
|
||||||
|
class StreamConfig(BaseModel):
|
||||||
|
video_max_quality: VideoQuality = VideoQuality._8K
|
||||||
|
audio_max_quality: AudioQuality = AudioQuality._192K
|
||||||
|
video_min_quality: VideoQuality = VideoQuality._360P
|
||||||
|
audio_min_quality: AudioQuality = AudioQuality._64K
|
||||||
|
codecs: list[VideoCodecs] = Field(
|
||||||
|
default_factory=lambda: [VideoCodecs.AV1, VideoCodecs.AVC, VideoCodecs.HEV], min_length=1
|
||||||
|
)
|
||||||
|
no_dolby_video: bool = False
|
||||||
|
no_dolby_audio: bool = False
|
||||||
|
no_hdr: bool = False
|
||||||
|
no_hires: bool = False
|
||||||
|
|
||||||
|
@field_validator("codecs", mode="after")
|
||||||
|
def codec_validator(cls, codecs: list[VideoCodecs]) -> list[VideoCodecs]:
|
||||||
|
if len(codecs) != len(set(codecs)):
|
||||||
|
raise PydanticCustomError("unique_list", "List must be unique")
|
||||||
|
return codecs
|
||||||
|
|
||||||
|
|
||||||
class Config(BaseModel):
|
class Config(BaseModel):
|
||||||
sessdata: Annotated[str, Field(min_length=1)] = ""
|
sessdata: Annotated[str, Field(min_length=1)] = ""
|
||||||
bili_jct: Annotated[str, Field(min_length=1)] = ""
|
bili_jct: Annotated[str, Field(min_length=1)] = ""
|
||||||
@@ -26,16 +46,15 @@ class Config(BaseModel):
|
|||||||
interval: int = 20
|
interval: int = 20
|
||||||
path_mapper: dict[int, str] = Field(default_factory=dict)
|
path_mapper: dict[int, str] = Field(default_factory=dict)
|
||||||
subtitle: SubtitleConfig = Field(default_factory=SubtitleConfig)
|
subtitle: SubtitleConfig = Field(default_factory=SubtitleConfig)
|
||||||
codec: list[VideoCodecs] = Field(
|
stream: StreamConfig = Field(default_factory=StreamConfig)
|
||||||
default_factory=lambda: [VideoCodecs.AV1, VideoCodecs.AVC, VideoCodecs.HEV], min_length=1
|
|
||||||
)
|
|
||||||
paginated_video: bool = False
|
paginated_video: bool = False
|
||||||
|
|
||||||
@field_validator("codec", mode="after")
|
@root_validator(pre=True)
|
||||||
def codec_validator(cls, codecs: list[VideoCodecs]) -> list[VideoCodecs]:
|
def migrate(cls, values: dict) -> dict:
|
||||||
if len(codecs) != len(set(codecs)):
|
# 把旧版本的 codec 迁移为 stream 中的 codecs
|
||||||
raise PydanticCustomError("unique_list", "List must be unique")
|
if "codec" in values and "stream" not in values:
|
||||||
return codecs
|
values["stream"] = {"codecs": values.pop("codec")}
|
||||||
|
return values
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load(path: Path | None = None) -> "Config":
|
def load(path: Path | None = None) -> "Config":
|
||||||
|
|||||||
Reference in New Issue
Block a user