feat:精选资源视频解析修复

This commit is contained in:
小张 2025-10-15 15:17:54 +08:00
parent 412a796ecb
commit e66b21a871
2 changed files with 115 additions and 28 deletions

View File

@ -143,7 +143,7 @@ const initDPlayer = async (url: string) => {
await loadHLSScript()
}
// DPlayer - 使
// DPlayer
const options: any = {
container: container,
autoplay: props.autoplay,
@ -154,8 +154,24 @@ const initDPlayer = async (url: string) => {
hotkey: true,
preload: 'auto',
volume: 0.8,
mutex: true,
video: {
mutex: true
}
// quality
if (props.qualities && props.qualities.length > 1) {
console.log('🎬 配置多清晰度:', props.qualities)
options.video = {
quality: props.qualities.map(q => ({
name: q.label,
url: q.url,
type: isHLS ? 'hls' : 'normal'
})),
defaultQuality: props.qualities.findIndex(q => q.value === props.currentQuality) || 0,
pic: props.poster || ''
}
} else {
//
options.video = {
url: url,
pic: props.poster || '',
type: 'normal'
@ -166,8 +182,17 @@ const initDPlayer = async (url: string) => {
// HLS
if (isHLS && (window as any).Hls && (window as any).Hls.isSupported()) {
console.log('🔧 使用HLS.js播放HLS流:', url)
options.video.type = 'hls'
console.log('🔧 使用HLS.js播放HLS流')
// type
if (options.video.quality) {
options.video.quality.forEach((q: any) => {
q.type = 'hls'
})
} else {
options.video.type = 'hls'
}
options.video.customType = {
hls: function(video: HTMLVideoElement, _player: DPlayer) {
console.log('🎬 配置HLS.js实例')

View File

@ -127,9 +127,19 @@
</button>
</div>
<div class="video-modal-body">
<DPlayerVideo ref="videoPlayerRef" :video-url="currentVideoUrl" :placeholder-image="currentVideo?.thumbnailUrl || currentVideo?.image"
:placeholder-text="'点击播放视频'" :title="currentVideo?.name || currentVideo?.title || '视频播放'" @play="handleVideoPlay"
@pause="handleVideoPause" @ended="handleVideoEnded" @error="handleVideoError" />
<DPlayerVideo
ref="videoPlayerRef"
:video-url="currentVideoUrl"
:placeholder-image="currentVideo?.thumbnailUrl || currentVideo?.image"
:placeholder-text="'点击播放视频'"
:title="currentVideo?.name || currentVideo?.title || '视频播放'"
:video-qualities="currentVideoQualities"
:current-quality="currentQuality"
@play="handleVideoPlay"
@pause="handleVideoPause"
@ended="handleVideoEnded"
@error="handleVideoError"
/>
</div>
</div>
</div>
@ -146,6 +156,8 @@ const showVideoModal = ref(false)
const currentVideo = ref<any>(null)
const currentVideoUrl = ref('')
const videoPlayerRef = ref<InstanceType<typeof DPlayerVideo>>()
const currentVideoQualities = ref<Array<{ label: string; value: string; url: string }>>([])
const currentQuality = ref('720')
//
const VIDEO_CONFIG = {
@ -318,21 +330,63 @@ const parseVideoUrls = (fileUrl: string): string[] => {
return fileUrl.split(',').map(url => url.trim()).filter(url => url)
}
// URL
const getBestVideoUrl = (fileUrl: string): string => {
console.log('🔍 解析视频URL:', fileUrl)
// URL
const extractQualityLabel = (url: string): string => {
if (url.includes('1080p')) return '1080P'
if (url.includes('720p')) return '720P'
if (url.includes('480p')) return '480P'
if (url.includes('360p')) return '360P'
return '标清'
}
//
const parseVideoQualities = (fileUrl: string) => {
const urls = parseVideoUrls(fileUrl)
console.log('🔍 解析后的URL数组:', urls)
if (urls.length === 0) {
console.warn('⚠️ 没有有效的视频URL使用本地视频作为备用')
return VIDEO_CONFIG.LOCAL
return {
qualities: [],
defaultQuality: '720',
defaultUrl: VIDEO_CONFIG.LOCAL
}
}
// 720p
const preferredUrl = urls.find(url => url.includes('720p')) || urls[0]
console.log('✅ 选择的视频URL:', preferredUrl)
return preferredUrl
// URL
const qualities = urls.map(url => {
let qualityValue = '360'
if (url.includes('1080p')) qualityValue = '1080'
else if (url.includes('720p')) qualityValue = '720'
else if (url.includes('480p')) qualityValue = '480'
return {
label: extractQualityLabel(url),
value: qualityValue,
url: url
}
})
//
const qualityOrder: Record<string, number> = { '1080': 3, '720': 2, '480': 1, '360': 0 }
qualities.sort((a, b) => (qualityOrder[b.value] || 0) - (qualityOrder[a.value] || 0))
// 720
const defaultQuality = qualities.find(q => q.value === '720')?.value || qualities[0]?.value || '720'
const defaultUrl = qualities.find(q => q.value === defaultQuality)?.url || urls[0]
console.log('🎬 解析的清晰度选项:', qualities)
console.log('🎯 默认清晰度:', defaultQuality, '默认URL:', defaultUrl)
return {
qualities,
defaultQuality,
defaultUrl
}
}
// URL
const getBestVideoUrl = (fileUrl: string): string => {
const { defaultUrl } = parseVideoQualities(fileUrl)
return defaultUrl
}
//
@ -354,20 +408,20 @@ const handleVideoClick = async (video: FeaturedResource) => {
currentVideo.value = video
// URL
const videoUrl = getBestVideoUrl(video.fileUrl)
currentVideoUrl.value = videoUrl
//
const { qualities, defaultQuality, defaultUrl } = parseVideoQualities(video.fileUrl)
currentVideoQualities.value = qualities
currentQuality.value = defaultQuality
currentVideoUrl.value = defaultUrl
console.log('🎯 最终使用的视频URL:', currentVideoUrl.value)
console.log('🎯 清晰度选项:', qualities)
console.log('🎯 默认清晰度:', defaultQuality)
console.log('🎯 默认URL:', defaultUrl)
showVideoModal.value = true
//
// watch
await nextTick()
if (videoPlayerRef.value) {
console.log('🔧 初始化播放器URL:', currentVideoUrl.value)
await videoPlayerRef.value.initializePlayer(currentVideoUrl.value)
}
}
//
@ -400,10 +454,18 @@ const closeVideoModal = () => {
showVideoModal.value = false
currentVideo.value = null
currentVideoUrl.value = ''
currentVideoQualities.value = []
currentQuality.value = '720'
//
if (videoPlayerRef.value) {
videoPlayerRef.value.destroy()
try {
if (typeof videoPlayerRef.value.destroy === 'function') {
videoPlayerRef.value.destroy()
}
} catch (e) {
console.warn('销毁播放器失败:', e)
}
}
}