Skip to content

Commit

Permalink
chore(large-preview): prefer highest quality
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdawn committed Jan 2, 2025
1 parent adb4587 commit ccd4429
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
11 changes: 8 additions & 3 deletions src/components/VideoCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -576,19 +576,24 @@ const VideoCardInner = memo(function VideoCardInner({
const videoCurrentTimeRef = useRef<number | undefined>(undefined)
const extraContent = (
<>
{(showLargePreview || isHoveringOnLargePreview) && videoPreviewDataBox.state?.playUrl && (
{(showLargePreview || isHoveringOnLargePreview) && videoPreviewDataBox.state?.playUrls && (
<LargePreview ref={largePreviewRef} aspectRatio={itemDimension.aspectRatio}>
<RecoverableVideo
src={videoPreviewDataBox.state?.playUrl}
currentTimeRef={videoCurrentTimeRef}
autoPlay
controls
loop
poster={cover}
css={css`
width: 100%;
height: 100%;
object-fit: contain;
`}
/>
>
{videoPreviewDataBox.state.playUrls.map((url, i) => (
<source key={i} src={url} />
))}
</RecoverableVideo>
</LargePreview>
)}
</>
Expand Down
16 changes: 8 additions & 8 deletions src/components/VideoCard/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ export function isImagePreviewDataValid(data?: ImagePreviewData) {

// #region VideoPreview
export type VideoPreviewData = {
playUrl?: string
playUrls?: string[]
dimension?: VideoDetailData['dimension']
}

export const isVideoPreviewDataValid = (data?: VideoPreviewData): boolean => {
return !!data?.playUrl
return !!data?.playUrls?.length
}

const videoPreviewCache = new QuickLRU<string, VideoPreviewData>({
Expand All @@ -143,21 +143,21 @@ export const fetchVideoPreviewData = reusePendingPromise(async (bvid: string, ci
const cached = videoPreviewCache.get(cacheKey)
if (cached) return cached

let playUrl: string | undefined
let playUrls: string[] = []
let dimension: VideoDetailData['dimension'] | undefined
if (typeof cid === 'undefined') {
const detail = await getVideoDetail(bvid)
cid = detail.cid
dimension = detail.dimension
}

playUrl = await getVideoPlayUrl(bvid, cid)
debug('playUrl: bvid=%s cid=%s %s', bvid, cid, playUrl)
if (playUrl) {
videoPreviewCache.set(cacheKey, { playUrl, dimension })
playUrls = await getVideoPlayUrl(bvid, cid)
debug('playUrl: bvid=%s cid=%s %s', bvid, cid, playUrls)
if (playUrls) {
videoPreviewCache.set(cacheKey, { playUrls, dimension })
}

return { playUrl, dimension }
return { playUrls, dimension }
})

// #endregion
11 changes: 7 additions & 4 deletions src/modules/bilibili/video/play-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import { request } from '$request'
import { orderBy } from 'es-toolkit'
import type { VideoPlayUrlJson } from './play-url-types'

/**
Expand Down Expand Up @@ -61,8 +62,10 @@ export async function getVideoPlayUrl(videoId: string | number, cid: number) {
const res = await request.get('/x/player/wbi/playurl', { params })
const json = res.data as VideoPlayUrlJson

const pickedVideos = json.data?.dash?.video.filter((v) => v.id === EResolution._480p) || []
const hevcUrl = pickedVideos.find((x) => x.codecid === ECodecId.HEVC)?.baseUrl
const avcUrl = pickedVideos.find((x) => x.codecid === ECodecId.AVC)?.baseUrl
return hevcUrl || avcUrl
const video = orderBy(json.data?.dash?.video || [], ['id', 'codecid'], ['desc', 'desc'])
const urls = video
.map((x) => [x.baseUrl])
.flat()
.filter(Boolean)
return urls
}

0 comments on commit ccd4429

Please sign in to comment.