Skip to content

Commit e47c84e

Browse files
committed
✨ feat:优化B站音频解析功能
1 parent 54cbb84 commit e47c84e

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

src/renderer/api/bilibili.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,32 @@ export const getBilibiliAudioUrl = async (bvid: string, cid: number): Promise<st
152152
throw error;
153153
}
154154
};
155+
156+
157+
// 根据音乐名称搜索并直接返回音频URL
158+
export const searchAndGetBilibiliAudioUrl = async (
159+
keyword: string
160+
): Promise<string> => {
161+
try {
162+
// 搜索B站视频,取第一页第一个结果
163+
const res = await searchBilibili({ keyword, page: 1, pagesize: 1 });
164+
const result = res.data?.data?.result;
165+
if (!result || result.length === 0) {
166+
throw new Error('未找到相关B站视频');
167+
}
168+
const first = result[0];
169+
const bvid = first.bvid;
170+
// 需要获取视频详情以获得cid
171+
const detailRes = await getBilibiliVideoDetail(bvid);
172+
const pages = detailRes.data.pages;
173+
if (!pages || pages.length === 0) {
174+
throw new Error('未找到视频分P信息');
175+
}
176+
const cid = pages[0].cid;
177+
// 获取音频URL
178+
return await getBilibiliAudioUrl(bvid, cid);
179+
} catch (error) {
180+
console.error('根据名称搜索B站音频URL失败:', error);
181+
throw error;
182+
}
183+
}

src/renderer/api/gdmusic.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,6 @@ export const parseFromGDMusic = async (
126126
}
127127
};
128128

129-
/**
130-
* 获取音质映射
131-
* @param qualitySetting 设置中的音质选项
132-
* @returns 映射到GD音乐台的音质参数
133-
*/
134-
export const getQualityMapping = (qualitySetting: string): string => {
135-
const qualityMap: Record<string, string> = {
136-
standard: '128',
137-
higher: '320',
138-
exhigh: '320',
139-
lossless: '740',
140-
hires: '999',
141-
jyeffect: '999',
142-
sky: '999',
143-
dolby: '999',
144-
jymaster: '999'
145-
};
146-
return qualityMap[qualitySetting] || '320';
147-
};
148-
149129
interface GDMusicUrlResult {
150130
url: string;
151131
br: string;

src/renderer/api/music.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { isElectron } from '@/utils';
55
import request from '@/utils/request';
66
import requestMusic from '@/utils/request_music';
77
import { cloneDeep } from 'lodash';
8-
import { parseFromGDMusic, getQualityMapping } from './gdmusic';
8+
import { parseFromGDMusic } from './gdmusic';
9+
import type { SongResult } from '@/type/music';
10+
import { searchAndGetBilibiliAudioUrl } from './bilibili';
911

1012
const { addData, getData, deleteData } = musicDB;
1113

@@ -80,7 +82,7 @@ export const getMusicLrc = async (id: number) => {
8082
}
8183
};
8284

83-
export const getParsingMusicUrl = async (id: number, data: any) => {
85+
export const getParsingMusicUrl = async (id: number, data: SongResult) => {
8486
const settingStore = useSettingsStore();
8587

8688
// 如果禁用了音乐解析功能,则直接返回空结果
@@ -98,7 +100,25 @@ export const getParsingMusicUrl = async (id: number, data: any) => {
98100
try {
99101
enabledSources = JSON.parse(savedSource);
100102
console.log(`使用歌曲 ${id} 自定义音源:`, enabledSources);
103+
if(enabledSources.includes('bilibili')){
104+
// 构建搜索关键词,依次判断歌曲名称、歌手名称和专辑名称是否存在
105+
const songName = data?.name || '';
106+
const artistName = Array.isArray(data?.ar) && data.ar.length > 0 && data.ar[0]?.name ? data.ar[0].name : '';
107+
const albumName = data?.al && typeof data.al === 'object' && data.al?.name ? data.al.name : '';
108+
const name = [songName, artistName, albumName].filter(Boolean).join(' ').trim();
109+
console.log('开始搜索bilibili音频', name);
110+
return {
111+
data: {
112+
code: 200,
113+
message: 'success',
114+
data: {
115+
url: await searchAndGetBilibiliAudioUrl(name)
116+
}
117+
}
118+
}
119+
}
101120
} catch (e) {
121+
console.error('e',e)
102122
console.error('解析自定义音源失败, 使用全局设置', e);
103123
enabledSources = settingStore.setData.enabledMusicSources || [];
104124
}
@@ -108,13 +128,11 @@ export const getParsingMusicUrl = async (id: number, data: any) => {
108128
}
109129

110130
// 检查是否选择了GD音乐台解析
131+
111132
if (enabledSources.includes('gdmusic')) {
112133
// 获取音质设置并转换为GD音乐台格式
113134
try {
114-
const quality = getQualityMapping(settingStore.setData.musicQuality || 'higher');
115-
116-
// 调用封装的GD音乐台解析服务
117-
const gdResult = await parseFromGDMusic(id, data, quality);
135+
const gdResult = await parseFromGDMusic(id, data, '999');
118136
if (gdResult) {
119137
return gdResult;
120138
}

0 commit comments

Comments
 (0)