diff --git a/src/bot/instance.ts b/src/bot/instance.ts index 0e19128..ee6ce7f 100755 --- a/src/bot/instance.ts +++ b/src/bot/instance.ts @@ -49,6 +49,8 @@ export interface BotStatus { volume: number; playMode: PlayMode; elapsed: number; // ground truth elapsed seconds from frame count + /** 当前曲实际播放时长(秒)。试听片段=试听秒数;完整曲=duration。缺失时前端回退 currentSong.duration。 */ + effectiveDuration?: number; } export class BotInstance extends EventEmitter { @@ -78,6 +80,8 @@ export class BotInstance extends EventEmitter { private fmProvider: MusicProvider | null = null; /** Results of the most recent !search, for "#N" selection (issue #90). */ private lastSearchResults: Song[] = []; + /** 当前曲实际播放时长(试听片段秒数或完整 duration);resolveAndPlay 赋值。 */ + private effectiveDuration: number | undefined; constructor(options: BotInstanceOptions) { super(); @@ -460,8 +464,8 @@ export class BotInstance extends EventEmitter { this.voteSkipUsers.clear(); const provider = this.getProviderFor(song.platform); try { - const url = await provider.getSongUrl(song.id); - if (!url) { + const result = await provider.getSongUrl(song.id); + if (!result?.url) { this.logger.warn({ songId: song.id, name: song.name }, "No URL available, skipping"); return false; } @@ -477,8 +481,10 @@ export class BotInstance extends EventEmitter { ); return false; } - song.url = url; - this.player.play(url, 0, song.duration); + song.url = result.url; + // 试听片段用试听时长(让 player nearEnd 正确触发自动切歌);完整曲回退 song.duration + this.effectiveDuration = result.trialDuration ?? song.duration; + this.player.play(result.url, 0, this.effectiveDuration); // Fresh playback (re)start — clear auto-pause so a later occupancy // change won't try to "resume" a track the user already restarted. this.autoPaused = false; @@ -1063,6 +1069,7 @@ export class BotInstance extends EventEmitter { volume: this.player.getVolume(), playMode: this.queue.getMode(), elapsed: this.player.getElapsed(), + effectiveDuration: this.effectiveDuration, }; } diff --git a/src/music/bilibili.ts b/src/music/bilibili.ts index 7a692d2..1942f02 100644 --- a/src/music/bilibili.ts +++ b/src/music/bilibili.ts @@ -3,6 +3,7 @@ import axios, { type AxiosInstance } from "axios"; import type { MusicProvider, Song, + SongUrlResult, Playlist, LyricLine, SearchResult, @@ -231,7 +232,7 @@ export class BiliBiliProvider implements MusicProvider { return this.cidCache.get(bvid) ?? null; } - async getSongUrl(songId: string, _quality?: string): Promise { + async getSongUrl(songId: string, _quality?: string): Promise { const cid = await this.getCid(songId); if (!cid) return null; @@ -253,7 +254,8 @@ export class BiliBiliProvider implements MusicProvider { (b.bandwidth ?? 0) > (a.bandwidth ?? 0) ? b : a ); - return best.baseUrl ?? best.base_url ?? null; + const biliUrl = best.baseUrl ?? best.base_url; + return biliUrl ? { url: biliUrl } : null; } catch { return null; } diff --git a/src/music/netease.test.ts b/src/music/netease.test.ts index fc50ffd..af68d94 100644 --- a/src/music/netease.test.ts +++ b/src/music/netease.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from "vitest"; -import { parseLyrics, mapNeteaseAlbums, mapNeteaseSongs } from "./netease.js"; +import { parseLyrics, mapNeteaseAlbums, mapNeteaseSongs, parseNeteaseTrial } from "./netease.js"; describe("NetEase adapter", () => { it("parses LRC format lyrics", () => { @@ -77,4 +77,19 @@ describe("NetEase adapter", () => { ]); expect(out[0]).toMatchObject({ artist: "B", album: "Al2", coverUrl: "p2", vip: false }); }); + + it("parseNeteaseTrial maps freeTrialInfo to trial seconds", () => { + // 无试听(VIP/免费) + expect(parseNeteaseTrial({})).toBeUndefined(); + expect(parseNeteaseTrial({ freeTrialInfo: null })).toBeUndefined(); + // 标准秒 + expect(parseNeteaseTrial({ freeTrialInfo: { start: 0, end: 30 } })).toBe(30); + expect(parseNeteaseTrial({ freeTrialInfo: { start: 5, end: 35 } })).toBe(30); + // 别名容忍 begin/trialBegin + expect(parseNeteaseTrial({ freeTrialInfo: { begin: 0, end: 30 } })).toBe(30); + // 毫秒兜底(end>1000) + expect(parseNeteaseTrial({ freeTrialInfo: { start: 0, end: 30000 } })).toBe(30); + // 异常 end<=start + expect(parseNeteaseTrial({ freeTrialInfo: { start: 0, end: 0 } })).toBeUndefined(); + }); }); diff --git a/src/music/netease.ts b/src/music/netease.ts index 6701ed6..3c2d564 100644 --- a/src/music/netease.ts +++ b/src/music/netease.ts @@ -2,6 +2,7 @@ import axios, { type AxiosInstance } from "axios"; import type { MusicProvider, Song, + SongUrlResult, Playlist, PlaylistDetail, LyricLine, @@ -83,6 +84,18 @@ export function mapNeteaseSongs(raw: any[] | null | undefined): Song[] { })); } +/** 解析网易云 freeTrialInfo → 试听秒数;无片段(VIP/免费)返回 undefined。 + * 真实字段 {start,end} 单位秒;容忍 begin/trialBegin 别名 + 毫秒兜底(end>1000)。 */ +export function parseNeteaseTrial(item: any): number | undefined { + const t = item?.freeTrialInfo; + if (!t || typeof t !== "object") return undefined; + const start = Number(t.start ?? t.begin ?? t.trialBegin ?? 0); + const end = Number(t.end ?? t.trialEnd); + if (!Number.isFinite(end) || end <= start) return undefined; + const secs = end > 1000 ? (end - start) / 1000 : end - start; + return Math.round(secs); +} + // NetEase quality levels: standard(128k) higher(192k) exhigh(320k) lossless(flac) hires(hi-res) jyeffect jymaster export const NETEASE_QUALITY_LEVELS = [ { value: "standard", label: "标准 (128kbps)", bitrate: 128 }, @@ -153,12 +166,15 @@ export class NeteaseProvider implements MusicProvider { return { songs, playlists, albums }; } - async getSongUrl(songId: string, quality?: string): Promise { + async getSongUrl(songId: string, quality?: string): Promise { const level = quality ?? this.quality; const res = await this.api.get("/song/url/v1", { params: { id: songId, level, ...this.cookieParams }, }); - return res.data?.data?.[0]?.url ?? null; + const item = res.data?.data?.[0]; + const url = item?.url; + if (!url) return null; + return { url, trialDuration: parseNeteaseTrial(item) }; } async getSongDetail(songId: string): Promise { diff --git a/src/music/provider.ts b/src/music/provider.ts index 28560c3..6317bbc 100644 --- a/src/music/provider.ts +++ b/src/music/provider.ts @@ -15,6 +15,13 @@ export interface SongWithUrl extends Song { url: string; } +/** getSongUrl 解析结果。trialDuration 缺省 = 完整可播放(VIP 账号 / 免费曲)。 */ +export interface SongUrlResult { + url: string; + /** 试听片段时长(秒)。VIP/免费曲为 undefined → 调用方回退完整 duration。 */ + trialDuration?: number; +} + export interface Playlist { id: string; name: string; @@ -68,7 +75,7 @@ export interface MusicProvider { readonly platform: "netease" | "qq" | "bilibili" | "youtube"; search(query: string, limit?: number): Promise; - getSongUrl(songId: string, quality?: string): Promise; + getSongUrl(songId: string, quality?: string): Promise; setQuality(quality: string): void; getQuality(): string; getSongDetail(songId: string): Promise; diff --git a/src/music/qq.test.ts b/src/music/qq.test.ts index 38ba697..1d33538 100644 --- a/src/music/qq.test.ts +++ b/src/music/qq.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from "vitest"; -import { mapQqAlbums, mapQqSongs } from "./qq.js"; +import { mapQqAlbums, mapQqSongs, parseQqTrial } from "./qq.js"; describe("QQ adapter", () => { it("mapQqSongs maps QQMusicApi-style song entries", () => { @@ -40,6 +40,19 @@ describe("QQ adapter", () => { expect(out[3].vip).toBe(false); }); + it("parseQqTrial maps isTryout/tryout to trial seconds", () => { + // 非试听(VIP/免费) + expect(parseQqTrial({ isTryout: 0 })).toBeUndefined(); + expect(parseQqTrial({})).toBeUndefined(); + // 试听(秒) + expect(parseQqTrial({ isTryout: 1, tryBegin: 0, tryEnd: 30 })).toBe(30); + expect(parseQqTrial({ tryout: true, begin: 0, end: 45 })).toBe(45); + // 毫秒兜底 + expect(parseQqTrial({ isTryout: 1, tryBegin: 0, tryEnd: 30000 })).toBe(30); + // 异常 + expect(parseQqTrial({ isTryout: 1, tryEnd: 0 })).toBeUndefined(); + }); + it("mapQqAlbums maps albumMID-style raw entries", () => { const raw = [ { diff --git a/src/music/qq.ts b/src/music/qq.ts index 63f3fbc..329ada6 100644 --- a/src/music/qq.ts +++ b/src/music/qq.ts @@ -2,6 +2,7 @@ import axios, { type AxiosInstance } from "axios"; import type { MusicProvider, Song, + SongUrlResult, Playlist, PlaylistDetail, LyricLine, @@ -58,6 +59,18 @@ export function mapQqSongs(raw: any[] | null | undefined): Song[] { }).filter((s) => s.id); } +/** 解析 QQ 试听标记 → 试听秒数;非试听(VIP/免费)返回 undefined。 + * 字段 isTryout===1 / tryout===true + tryBegin/tryEnd;容忍 begin/start 别名 + 毫秒兜底。 */ +export function parseQqTrial(playUrl: any): number | undefined { + if (!playUrl || typeof playUrl !== "object") return undefined; + if (playUrl.isTryout !== 1 && playUrl.tryout !== true) return undefined; + const begin = Number(playUrl.tryBegin ?? playUrl.begin ?? playUrl.start ?? 0); + const end = Number(playUrl.tryEnd ?? playUrl.end); + if (!Number.isFinite(end) || end <= begin) return undefined; + const secs = end > 1000 ? (end - begin) / 1000 : end - begin; + return Math.round(secs); +} + export function mapQqAlbums(raw: any[] | null | undefined): Album[] { if (!Array.isArray(raw)) return []; return raw.map((a) => { @@ -248,13 +261,13 @@ export class QQMusicProvider implements MusicProvider { return { songs, playlists: [], albums }; } - async getSongUrl(songId: string, quality?: string): Promise { + async getSongUrl(songId: string, quality?: string): Promise { try { const res = await this.api.get("/getMusicPlay", { params: { songmid: songId, quality: quality ?? this.quality, ...this.cookieParams }, }); const playUrl = res.data?.data?.playUrl?.[songId]; - if (playUrl?.url) return playUrl.url; + if (playUrl?.url) return { url: playUrl.url, trialDuration: parseQqTrial(playUrl) }; } catch { // try with songid try { @@ -262,7 +275,7 @@ export class QQMusicProvider implements MusicProvider { params: { songid: songId, quality: quality ?? this.quality, ...this.cookieParams }, }); const playUrl = res.data?.data?.playUrl?.[songId]; - if (playUrl?.url) return playUrl.url; + if (playUrl?.url) return { url: playUrl.url, trialDuration: parseQqTrial(playUrl) }; } catch { // ignore } diff --git a/src/music/youtube.ts b/src/music/youtube.ts index 6ae6a34..209d057 100644 --- a/src/music/youtube.ts +++ b/src/music/youtube.ts @@ -7,6 +7,7 @@ import type { MusicProvider, Song, SongWithUrl, + SongUrlResult, Playlist, Album, SearchResult, @@ -134,7 +135,7 @@ export class YouTubeProvider implements MusicProvider { } } - async getSongUrl(songId: string): Promise { + async getSongUrl(songId: string): Promise { try { const url = `https://www.youtube.com/watch?v=${songId}`; const raw = await runYtDlp([ @@ -146,7 +147,7 @@ export class YouTubeProvider implements MusicProvider { "--quiet", ], 45_000); const audioUrl = raw.trim().split("\n")[0]; - return audioUrl || null; + return audioUrl ? { url: audioUrl } : null; } catch { return null; }