Skip to content

Commit

Permalink
[change] use new multi-player
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiram committed Dec 15, 2024
1 parent 9fb123b commit 457f735
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 273 deletions.
4 changes: 2 additions & 2 deletions src/renderer/src/components/player/src/utils/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ const mediaUtils = (() => {
};

// 映射 Content-Type 到具体格式
const mapContentTypeToFormat = (contentType: string): string => {
const mapContentTypeToFormat = (contentType: string): string | undefined => {
const entry = Object.entries(supportedFormats).find(([type]) =>
contentType.includes(type)
);
return entry ? entry[1] : 'unknown';
return entry ? entry[1] : undefined;
};

// 使用 fetch 获取媒体类型
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/locales/lang/en_US/pages/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default {
official: 'Analyze completed, data is derived from {0}',
noDefaultAnalyze: 'Not set default, Choose your own analysis line',
noRecommendSearch: 'No recommendation found for this source, Switch is not performed',
noPlayUrl: 'Play address acquisition failed, please change the source or line',
next: 'One moment, Switching next',
},
placeholder: {
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/locales/lang/zh_CN/pages/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default {
sniiferError: '嗅探失败,请换源',
noDefaultAnalyze: '未配置默认解析,自行选择解析线路',
noRecommendSearch: '该源未找到相关推荐,不执行本次切换',
noPlayUrl: '播放地址获取失败,请换源或线路',
next: '请稍候,正在切换下集',
},
placeholder: {
Expand Down
11 changes: 5 additions & 6 deletions src/renderer/src/pages/analyze/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</div>
</div>
<div class="player-content">
<player-view ref="playerRef" />
<multi-player ref="playerRef" />
</div>
</div>
<div class="analyze-setting">
Expand Down Expand Up @@ -71,14 +71,14 @@ import { putHistory, addHistory, findHistory } from '@/api/history';
import { fetchAnalyzeHelper } from '@/utils/common/film';
import emitter from '@/utils/emitter';
import { MultiPlayer } from '@/components/player';
import DialogHistoryView from './components/DialogHistory.vue';
import DialogIframemView from './components/DialogIframe.vue';
import DialogSearchView from './components/DialogSearch.vue';
import SharePopup from '@/components/share-popup/index.vue';
import CommonNav from '@/components/common-nav/index.vue';
import PlayerView from '@/components/player/index.vue';
const store = usePlayStore();
const storePlayer = usePlayStore();
const searchText = ref('');
const urlTitle = ref(''); // 播放地址的标题
const analyzeUrl = ref<string>(''); // 输入需要解析地址
Expand Down Expand Up @@ -181,13 +181,12 @@ const getVideoInfo = async (url: string, title: string) => {
playFormData.value.type = analyzeRes.mediaType;
playFormData.value.url = analyzeRes.url;
playFormData.value.headers = analyzeRes.headers;
const { playerMode } = store.setting;
const playerMode = storePlayer.setting.playerMode;
if (playerMode.type === 'custom') {
window.electron.ipcRenderer.send('call-player', playerMode.external, playFormData.value.url);
} else {
if (playerRef.value) {
await playerRef.value.init();
await playerRef.value.create(playFormData.value);
await playerRef.value.create(playFormData.value, playerMode.type);
};
}
Expand Down
58 changes: 35 additions & 23 deletions src/renderer/src/pages/lab/components/snifferPlay/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<t-option label="Flv" value="flv" />
<t-option label="Mp4" value="mp4" />
<t-option label="Dash" value="mpd" />
<t-option label="Torrent" value="magnet" />
</t-select>
</t-form-item>
</t-form>
Expand All @@ -65,7 +66,7 @@
</t-badge>
<t-badge :count="$t('pages.lab.snifferPlay.preview')" style="flex: 1;" color="var(--td-success-color)" shape="round">
<div class="result card" style="height: 100%; width: 100%;">
<player-view ref="playerRef" style="border-radius: var(--td-radius-default); overflow: hidden;" />
<multi-player ref="playerRef" style="border-radius: var(--td-radius-default); overflow: hidden;" />
</div>
</t-badge>
</div>
Expand All @@ -75,11 +76,13 @@

<script lang="ts" setup>
import { ref, useTemplateRef } from 'vue';
import sniffer from '@/utils/sniffer';
import PlayerView from '@/components/player/index.vue';
import { MessagePlugin } from 'tdesign-vue-next';
import sniffer from '@/utils/sniffer';
import { t } from '@/locales';
import { checkMediaType } from '@/utils/tool';
import { usePlayStore } from '@/store';
import { MultiPlayer, mediaUtils } from '@/components/player';
const storePlayer = usePlayStore();
const formData = ref({
sniffer: {
Expand Down Expand Up @@ -115,13 +118,13 @@ const sniiferEvent = async () => {
};
const playerPlayEvent = async () => {
if (!playerRef.value) return;
if (!formData.value.player.url || !formData.value.player.url.startsWith('http')) {
let { url, headers, type } = formData.value.player;
if (!headers) headers = '{}';
if (!url || !(/^(http:\/\/|https:\/\/)/.test(url) || url.includes('magnet:'))) {
MessagePlugin.warning(t('pages.lab.snifferPlay.message.playerNoUrl'));
return;
};
let headers = formData.value.player.headers || '{}';
try {
headers = JSON.parse(headers);
if (typeof headers === 'object' && headers !== null && !Array.isArray(headers)) {
Expand All @@ -133,23 +136,32 @@ const playerPlayEvent = async () => {
MessagePlugin.warning(t('pages.lab.snifferPlay.message.headersNoJson'));
return;
}
let mediaType = formData.value.player.type;
if (mediaType === 'auto') {
const checkType = await checkMediaType(formData.value.player.url);
if (checkType === 'unknown' && !checkType) {
MessagePlugin.warning(t('pages.lab.snifferPlay.message.mediaNoType'));
return;
const playerMode = storePlayer.setting.playerMode;
if (playerMode.type === 'custom') {
window.electron.ipcRenderer.send('call-player', playerMode.external, url);
} else {
let mediaType = type;
if (mediaType === 'auto') {
const checkType = await mediaUtils.checkMediaType(url);
if (checkType === 'unknown' && !checkType) {
MessagePlugin.warning(t('pages.lab.snifferPlay.message.mediaNoType'));
return;
}
mediaType = checkType;
};
if (playerRef.value) {
await playerRef.value.create({
url: url,
isLive: false,
headers: headers,
type: mediaType,
container: 'lab-mse'
}, playerMode.type);
}
mediaType = checkType;
};
await playerRef.value.init();
await playerRef.value.create({
url: formData.value.player.url,
isLive: false,
headers: headers,
type: mediaType,
container: 'lab-mse'
});
}
MessagePlugin.success(t('pages.setting.form.success'));
};
Expand Down
172 changes: 89 additions & 83 deletions src/renderer/src/pages/play/componets/AsideFilm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,7 @@ import {
} from '@/utils/common/film';
import { fetchRecommPage } from '@/api/site';
import { fetchAnalyzeActive } from '@/api/analyze';
import { fetchConfig } from '@/api/setting';
import { t } from '@/locales';
import { hash } from '@/utils/crypto';
import DialogDownloadView from './DialogDownload.vue';
import DialogSettingView from './DialogSetting.vue';
import SharePopup from '@/components/share-popup/index.vue';
Expand Down Expand Up @@ -472,56 +470,64 @@ const settingEvent = () => {
// 调用播放器
const callPlay = async (item) => {
let { url } = formatIndex(item);
url = decodeURIComponent(url);
const originUrl = url;
active.value.filmIndex = item;
const analyzeInfo = analyzeData.value.list.find(item => item.id === active.value.analyzeId);
let response;
if (tmp.value.preloadNext.init && tmp.value.preloadNext.load) {
response = { url: tmp.value.preloadNext.url, headers: tmp.value.preloadNext.headers, mediaType: tmp.value.preloadNext.mediaType };
} else {
let analyzeType = analyzeInfo?.type !== undefined ? analyzeInfo?.type : -1;
if (active.value.official) {
if (!analyzeInfo || typeof analyzeInfo !== 'object' || Object.keys(analyzeInfo).length === 0) {
MessagePlugin.warning(t('pages.film.message.notSelectAnalyze'));
return;
};
url = `${analyzeInfo.url}${url}`;
analyzeType = analyzeInfo.type;
try {
let { url } = formatIndex(item);
url = decodeURIComponent(url);
const originUrl = url;
active.value.filmIndex = item;
const analyzeInfo = analyzeData.value.list.find(item => item.id === active.value.analyzeId);
let response;
if (tmp.value.preloadNext.init && tmp.value.preloadNext.load) {
response = { url: tmp.value.preloadNext.url, headers: tmp.value.preloadNext.headers, mediaType: tmp.value.preloadNext.mediaType };
} else {
analyzeType = -1;
}
response = await playHelper(url, extConf.value.site, active.value.flimSource, analyzeType, extConf.value.setting.playConf.skipAd);
};
let analyzeType = analyzeInfo?.type !== undefined ? analyzeInfo?.type : -1;
if (active.value.official) {
if (!analyzeInfo || typeof analyzeInfo !== 'object' || Object.keys(analyzeInfo).length === 0) {
MessagePlugin.warning(t('pages.film.message.notSelectAnalyze'));
return;
};
url = `${analyzeInfo.url}${url}`;
analyzeType = analyzeInfo.type;
} else {
analyzeType = -1;
}
response = await playHelper(url, extConf.value.site, active.value.flimSource, analyzeType, extConf.value.setting.playConf.skipAd);
};
if (response?.url &&/^(http:\/\/|https:\/\/)/.test(response.url)) {
videoData.value.url = response.url;
emits('play', { url: response.url, type: response.mediaType! || '', headers: response.headers, startTime: videoData.value.skipTime });
};
if (!response?.url || !/^(http:\/\/|https:\/\/)/.test(response.url)) {
MessagePlugin.warning(t('pages.player.message.noPlayUrl'));
return;
} else {
videoData.value.url = response.url;
emits('play', { url: response.url, type: response.mediaType! || '', headers: response.headers, startTime: videoData.value.skipTime });
};
let barrage: any[] = [];
if (tmp.value.preloadNext.init && tmp.value.preloadNext.load) {
if (tmp.value.preloadNext.barrage.length > 0) barrage = tmp.value.preloadNext.barrage;
} else {
barrage = await fetchBarrageData(originUrl, extConf.value.setting.barrage, active.value);
};
if (/^(http:\/\/|https:\/\/)/.test(response?.url) && barrage.length > 0) {
emits('barrage', { comments: barrage, url: extConf.value.setting.barrage.url, id: hash['md5-16'](originUrl) });
};
let barrage: any[] = [];
if (tmp.value.preloadNext.init && tmp.value.preloadNext.load) {
if (tmp.value.preloadNext.barrage.length > 0) barrage = tmp.value.preloadNext.barrage;
} else {
barrage = await fetchBarrageData(originUrl, extConf.value.setting.barrage, active.value);
};
// 临时数据恢复默认
tmp.value = {
preloadNext: {
url: '',
headers: {},
load: false,
init: false,
barrage: [],
mediaType: '',
},
end: false,
};
if (barrage.length > 0) {
const { origin, pathname } = new URL(originUrl);
const formatBarrageUrl = `${origin}${pathname}`;
emits('barrage', { comments: barrage, url: extConf.value.setting.barrage.url, id: formatBarrageUrl });
};
} finally {
// 临时数据恢复默认
tmp.value = {
preloadNext: {
url: '',
headers: {},
load: false,
init: false,
barrage: [],
mediaType: '',
},
end: false,
};
}
};
// 切换线路
Expand Down Expand Up @@ -714,8 +720,8 @@ const timerUpdatePlayProcess = async(currentTime: number, duration: number) => {
};
// 2.获取跳过时间
const { preloadNext, skipStartEnd, playConf, barrage } = extConf.value.setting;
const watchTime = skipStartEnd ? currentTime + videoData.value.skipTimeInEnd : currentTime;
const { playConf, barrage } = extConf.value.setting;
const watchTime = playConf.skipHeadAndEnd ? currentTime + videoData.value.skipTimeInEnd : currentTime;
// console.log(
// `[player][timeUpdate] - current:${currentTime}; watch:${watchTime}; duration:${duration}; percentage:${Math.trunc((currentTime / duration) * 100)}%`,
// );
Expand All @@ -726,47 +732,47 @@ const timerUpdatePlayProcess = async(currentTime: number, duration: number) => {
if (watchTime >= duration) videoData.value.playEnd = true;
throttlePutHistory();
// 4.播放下集 不是最后一集 & 时间>尾部跳过时间+观看时间
// 4.播放下集 不是最后一集 & 开启续集 & 观看时间+尾部跳过时间 >= 总时长
if (!isLast() && playConf.playNextEnabled && watchTime >= duration && duration !== 0 && !tmp.value.end) {
tmp.value.end = true;
tmp.value.end = true; // 标识是否触发下集
const nextIndex = active.value.reverseOrder ? index + 1 : index - 1;
const nextInfo = seasonData.value[active.value.flimSource][nextIndex];
await switchSeasonEvent(nextInfo);
return;
};
// 5.预加载下集链接 提前30秒预加载
if (watchTime + 30 >= duration && duration !== 0) {
if (!isLast() && !tmp.value.preloadNext.load && preloadNext) {
tmp.value.preloadNext.load = true; // 标识是否触发预加载
try {
const nextIndex = active.value.reverseOrder ? index + 1 : index - 1;
const nextInfo = seasonData.value[active.value.flimSource][nextIndex];
let url = formatIndex(nextInfo).url;
url = decodeURIComponent(url);
const originUrl = url;
const analyzeInfo = analyzeData.value.list.find(item => item.id === active.value.analyzeId);
let analyzeType = analyzeInfo?.type !== undefined ? analyzeInfo?.type : -1;
if (active.value.official) {
if (!analyzeInfo || typeof analyzeInfo !== 'object' || Object.keys(analyzeInfo).length === 0) return;
url = `${analyzeInfo.url}${url}`;
analyzeType = analyzeInfo.type;
} else {
analyzeType = -1;
}
const response = await playHelper(url, extConf.value.site, active.value.flimSource, analyzeType, playConf.skipAd);
if (response?.url &&/^(http:\/\/|https:\/\/)/.test(response.url)) {
tmp.value.preloadNext.url = response.url;
tmp.value.preloadNext.headers = response.headers;
tmp.value.preloadNext.mediaType = response.mediaType;
tmp.value.preloadNext.init = true; // 标识是否预加载完毕
const barrageRes: any[] = await fetchBarrageData(originUrl, barrage, active.value);
if (Array.isArray(barrageRes) && barrageRes.length === 0) tmp.value.preloadNext.barrage = barrageRes;
}
} catch (err: any) {
console.log(`[player][timeUpdate][preloadNext][error]`, err.message);
// 5.预加载下集链接 不是最后一集 & 开启预加载 & 提前30秒预加载+观看时间+尾部跳过时间 >= 总时长
if (!isLast() && playConf.playNextPreload && watchTime + 30 >= duration && duration !== 0 && !tmp.value.preloadNext.load) {
tmp.value.preloadNext.load = true; // 标识是否触发预加载
try {
const nextIndex = active.value.reverseOrder ? index + 1 : index - 1;
const nextInfo = seasonData.value[active.value.flimSource][nextIndex];
let url = formatIndex(nextInfo).url;
url = decodeURIComponent(url);
const originUrl = url;
const analyzeInfo = analyzeData.value.list.find(item => item.id === active.value.analyzeId);
let analyzeType = analyzeInfo?.type !== undefined ? analyzeInfo?.type : -1;
if (active.value.official) {
if (!analyzeInfo || typeof analyzeInfo !== 'object' || Object.keys(analyzeInfo).length === 0) return;
url = `${analyzeInfo.url}${url}`;
analyzeType = analyzeInfo.type;
} else {
analyzeType = -1;
}
const response = await playHelper(url, extConf.value.site, active.value.flimSource, analyzeType, playConf.skipAd);
if (response?.url &&/^(http:\/\/|https:\/\/)/.test(response.url)) {
tmp.value.preloadNext.url = response.url;
tmp.value.preloadNext.headers = response.headers;
tmp.value.preloadNext.mediaType = response.mediaType;
tmp.value.preloadNext.init = true; // 标识是否预加载完毕
const barrageRes: any[] = await fetchBarrageData(originUrl, barrage, active.value);
if (Array.isArray(barrageRes) && barrageRes.length > 0) tmp.value.preloadNext.barrage = barrageRes;
}
} catch (err: any) {
console.log(`[player][timeUpdate][preloadNext][error]`, err);
}
};
};
Expand Down
Loading

0 comments on commit 457f735

Please sign in to comment.