improvement: isAudioOnly support in nicovideo

This commit is contained in:
mikhail 2024-05-22 22:47:18 +05:00
parent 6524d4d44c
commit f956af06ed
2 changed files with 29 additions and 21 deletions

View File

@ -189,7 +189,8 @@ export default async function(host, patternMatch, lang, obj) {
case "nicovideo": case "nicovideo":
r = await nicovideo({ r = await nicovideo({
id: patternMatch.id, id: patternMatch.id,
quality: obj.vQuality quality: obj.vQuality,
isAudioOnly: isAudioOnly,
}); });
break; break;
default: default:

View File

@ -91,7 +91,7 @@ async function fetchContentURL(id, actionTrackId, accessRightKey, outputs) {
return data.data.contentUrl; return data.data.contentUrl;
} }
async function getHLSContent(contentURL, quality) { async function getHLSContent(contentURL, quality, isAudioOnly) {
const hls = await fetch(contentURL) const hls = await fetch(contentURL)
.then((response) => response.text()) .then((response) => response.text())
.then((response) => HLS.parse(response)); .then((response) => HLS.parse(response));
@ -110,41 +110,48 @@ async function getHLSContent(contentURL, quality) {
.shift(); .shift();
} }
return { const audioUrl = hlsContent.audio.pop().uri;
resolution: hlsContent.resolution, return isAudioOnly
urls: [hlsContent.uri, hlsContent.audio.pop().uri], ? { resolution: null, urls: audioUrl, type: "audio" }
}; : {
resolution: hlsContent.resolution,
urls: [hlsContent.uri, audioUrl],
type: "video",
};
} }
// TODO @synzr only audio support
// TODO @synzr better error handling // TODO @synzr better error handling
export default async function nicovideo({ id, quality }) { export default async function nicovideo({ id, quality, isAudioOnly }) {
try { try {
const { actionTrackId, title, author } = await getBasicVideoInformation(id); const { actionTrackId, title, author } = await getBasicVideoInformation(id);
const { const { resolution, urls, type } = await fetchGuestData(id, actionTrackId)
resolution,
urls: [video, audio],
} = await fetchGuestData(id, actionTrackId)
.then(({ accessRightKey, outputs }) => .then(({ accessRightKey, outputs }) =>
fetchContentURL(id, actionTrackId, accessRightKey, outputs) fetchContentURL(id, actionTrackId, accessRightKey, outputs)
) )
.then((contentURL) => getHLSContent(contentURL, quality)); .then((contentURL) => getHLSContent(contentURL, quality, isAudioOnly));
return { return {
urls: [video, audio], urls,
isAudioOnly: type === "audio",
fileMetadata: {
title: cleanString(title.trim()),
artist: cleanString(author.nickname.trim()),
},
// bible accurate object concatenation
filenameAttributes: { filenameAttributes: {
service: "nicovideo", service: "nicovideo",
id, id,
title, title,
author: author.nickname, author: author.nickname,
resolution: `${resolution.width}x${resolution.height}`, ...(type === "video"
qualityLabel: `${resolution.height}p`, ? {
extension: "mp4", extension: "mp4",
}, qualityLabel: `${resolution.height}p`,
fileMetadata: { resolution: `${resolution.width}x${resolution.height}`,
title: cleanString(title.trim()), }
artist: cleanString(author.nickname.trim()), : {}),
}, },
...(type === "audio" ? { isM3U8: true, bestAudio: "mp3" } : {}),
}; };
} catch (error) { } catch (error) {
return { error: "ErrorEmptyDownload" }; return { error: "ErrorEmptyDownload" };