Skip to content

一分钟搭建影视站,支持Docker等部署方式

feat(播放器): 添加视频源速率测试功能并优化资源切换界面

Frank426c7cc1adca
diff --git a/css/modals.css b/css/modals.css
index 837969a..b4ba3d3 100644
--- a/css/modals.css
+++ b/css/modals.css
@@ -212,3 +212,54 @@
.modal-overlay.hide {
animation: fadeOut 0.3s forwards;
}
+
+/* 资源速率测试相关样式 */
+.speed-indicator {
+ display: inline-flex;
+ align-items: center;
+ gap: 2px;
+ font-size: 10px;
+ font-weight: 500;
+ line-height: 1;
+}
+
+.speed-indicator.good {
+ color: #10b981;
+}
+
+.speed-indicator.medium {
+ color: #f59e0b;
+}
+
+.speed-indicator.poor {
+ color: #ef4444;
+}
+
+.speed-indicator.error {
+ color: #ef4444;
+}
+
+/* 资源卡片悬停效果增强 */
+.resource-card {
+ transition: all 0.2s ease;
+}
+
+.resource-card:hover {
+ transform: translateY(-2px);
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
+}
+
+.resource-card.current {
+ border: 1px solid #3b82f6;
+ box-shadow: 0 0 0 1px rgba(59, 130, 246, 0.3);
+}
+
+/* 速率徽章样式 */
+.speed-badge {
+ backdrop-filter: blur(4px);
+ font-size: 9px;
+ font-weight: 600;
+ text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
+ border-radius: 4px;
+ padding: 2px 4px;
+}
diff --git a/js/player.js b/js/player.js
index c0dd0e5..19013a8 100644
--- a/js/player.js
+++ b/js/player.js
@@ -1485,6 +1485,114 @@ function renderResourceInfoBar() {
`;
}
+// 测试视频源速率的函数
+async function testVideoSourceSpeed(sourceKey, vodId) {
+ try {
+ const startTime = performance.now();
+
+ // 构建API参数
+ let apiParams = '';
+ if (sourceKey.startsWith('custom_')) {
+ const customIndex = sourceKey.replace('custom_', '');
+ const customApi = getCustomApiInfo(customIndex);
+ if (!customApi) {
+ return { speed: -1, error: 'API配置无效' };
+ }
+ if (customApi.detail) {
+ apiParams = '&customApi=' + encodeURIComponent(customApi.url) + '&customDetail=' + encodeURIComponent(customApi.detail) + '&source=custom';
+ } else {
+ apiParams = '&customApi=' + encodeURIComponent(customApi.url) + '&source=custom';
+ }
+ } else {
+ apiParams = '&source=' + sourceKey;
+ }
+
+ // 添加时间戳防止缓存
+ const timestamp = new Date().getTime();
+ const cacheBuster = `&_t=${timestamp}`;
+
+ // 获取视频详情
+ const response = await fetch(`/api/detail?id=${encodeURIComponent(vodId)}${apiParams}${cacheBuster}`, {
+ method: 'GET',
+ cache: 'no-cache'
+ });
+
+ if (!response.ok) {
+ return { speed: -1, error: '获取失败' };
+ }
+
+ const data = await response.json();
+
+ if (!data.episodes || data.episodes.length === 0) {
+ return { speed: -1, error: '无播放源' };
+ }
+
+ // 测试第一个播放链接的响应速度
+ const firstEpisodeUrl = data.episodes[0];
+ if (!firstEpisodeUrl) {
+ return { speed: -1, error: '链接无效' };
+ }
+
+ // 测试视频链接响应时间
+ const videoTestStart = performance.now();
+ try {
+ const videoResponse = await fetch(firstEpisodeUrl, {
+ method: 'HEAD',
+ mode: 'no-cors',
+ cache: 'no-cache',
+ signal: AbortSignal.timeout(5000) // 5秒超时
+ });
+
+ const videoTestEnd = performance.now();
+ const totalTime = videoTestEnd - startTime;
+
+ // 返回总响应时间(毫秒)
+ return {
+ speed: Math.round(totalTime),
+ episodes: data.episodes.length,
+ error: null
+ };
+ } catch (videoError) {
+ // 如果视频链接测试失败,只返回API响应时间
+ const apiTime = performance.now() - startTime;
+ return {
+ speed: Math.round(apiTime),
+ episodes: data.episodes.length,
+ error: null,
+ note: 'API响应'
+ };
+ }
+
+ } catch (error) {
+ return {
+ speed: -1,
+ error: error.name === 'AbortError' ? '超时' : '测试失败'
+ };
+ }
+}
+
+// 格式化速度显示
+function formatSpeedDisplay(speedResult) {
+ if (speedResult.speed === -1) {
+ return `<span class="speed-indicator error">❌ ${speedResult.error}</span>`;
+ }
+
+ const speed = speedResult.speed;
+ let className = 'speed-indicator good';
+ let icon = '🟢';
+
+ if (speed > 2000) {
+ className = 'speed-indicator poor';
+ icon = '🔴';
+ } else if (speed > 1000) {
+ className = 'speed-indicator medium';
+ icon = '🟡';
+ }
+
+ const note = speedResult.note ? ` (${speedResult.note})` : '';
+ return `<span class="${className}">${icon} ${speed}ms${note}</span>`;
+}
+
async function showSwitchResourceModal() {
const urlParams = new URLSearchParams(window.location.search);
const currentSourceCode = urlParams.get('source');
@@ -1525,6 +1633,17 @@ async function showSwitchResourceModal() {
allResults[opt.key] = result;
}));
+ // 更新状态显示:开始速率测试
+ modalContent.innerHTML = '<div style="text-align:center;padding:20px;color:#aaa;grid-column:1/-1;">正在测试各资源速率...</div>';
+
+ // 同时测试所有资源的速率
+ const speedResults = {};
+ await Promise.all(Object.entries(allResults).map(async ([sourceKey, result]) => {
+ if (result) {
+ speedResults[sourceKey] = await testVideoSourceSpeed(sourceKey, result.vod_id);
+ }
+ }));
+
// 对结果进行排序
const sortedResults = Object.entries(allResults).sort(([keyA, resultA], [keyB, resultB]) => {
// 当前播放的源放在最前面
@@ -1534,15 +1653,19 @@ async function showSwitchResourceModal() {
if (isCurrentA && !isCurrentB) return -1;
if (!isCurrentA && isCurrentB) return 1;
- // 其余按照 selectedAPIs 的顺序排列
- const indexA = selectedAPIs.indexOf(keyA);
- const indexB = selectedAPIs.indexOf(keyB);
+ // 其余按照速度排序,速度快的在前面(速度为-1表示失败,排到最后)
+ const speedA = speedResults[keyA]?.speed || 99999;
+ const speedB = speedResults[keyB]?.speed || 99999;
- return indexA - indexB;
+ if (speedA === -1 && speedB !== -1) return 1;
+ if (speedA !== -1 && speedB === -1) return -1;
+ if (speedA === -1 && speedB === -1) return 0;
+
+ return speedA - speedB;
});
// 渲染资源列表
- let html = '<div class="grid grid-cols-3 sm:grid-cols-4 md:grid-cols-5 lg:grid-cols-6 gap-3 p-4">';
+ let html = '<div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-4 p-4">';
for (const [sourceKey, result] of sortedResults) {
if (!result) continue;
@@ -1550,23 +1673,32 @@ async function showSwitchResourceModal() {
// 修复 isCurrentSource 判断,确保类型一致
const isCurrentSource = String(sourceKey) === String(currentSourceCode) && String(result.vod_id) === String(currentVideoId);
const sourceName = resourceOptions.find(opt => opt.key === sourceKey)?.name || '未知资源';
+ const speedResult = speedResults[sourceKey] || { speed: -1, error: '未测试' };
html += `
<div class="relative group ${isCurrentSource ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer hover:scale-105 transition-transform'}"
${!isCurrentSource ? `onclick="switchToResource('${sourceKey}', '${result.vod_id}')"` : ''}>
- <div class="aspect-[2/3] rounded-lg overflow-hidden bg-gray-800">
+ <div class="aspect-[2/3] rounded-lg overflow-hidden bg-gray-800 relative">
<img src="${result.vod_pic}"
alt="${result.vod_name}"
class="w-full h-full object-cover"
onerror="this.src='data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgc3Ryb2tlPSIjNjY2IiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCI+PHJlY3QgeD0iMyIgeT0iMyIgd2lkdGg9IjE4IiBoZWlnaHQ9IjE4IiByeD0iMiIgcnk9IjIiPjwvcmVjdD48cGF0aCBkPSJNMjEgMTV2NGEyIDIgMCAwIDEtMiAySDVhMiAyIDAgMCAxLTItMnYtNCI+PC9wYXRoPjxwb2x5bGluZSBwb2ludHM9IjE3IDggMTIgMyA3IDgiPjwvcG9seWxpbmU+PHBhdGggZD0iTTEyIDN2MTIiPjwvcGF0aD48L3N2Zz4='">
+
+ <!-- 速率显示在图片右上角 -->
+ <div class="absolute top-1 right-1 speed-badge bg-black bg-opacity-75">
+ ${formatSpeedDisplay(speedResult)}
+ </div>
</div>
- <div class="mt-1">
+ <div class="mt-2">
<div class="text-xs font-medium text-gray-200 truncate">${result.vod_name}</div>
- <div class="text-[10px] text-gray-400">${sourceName}</div>
+ <div class="text-[10px] text-gray-400 truncate">${sourceName}</div>
+ <div class="text-[10px] text-gray-500 mt-1">
+ ${speedResult.episodes ? `${speedResult.episodes}集` : ''}
+ </div>
</div>
${isCurrentSource ? `
<div class="absolute inset-0 flex items-center justify-center">
- <div class="bg-black bg-opacity-50 rounded-lg px-2 py-0.5 text-xs text-white">
+ <div class="bg-blue-600 bg-opacity-75 rounded-lg px-2 py-0.5 text-xs text-white font-medium">
当前播放
</div>
</div>