Skip to content

Instantly share code, notes, and snippets.

@Last-Order
Created July 27, 2022 12:07
Show Gist options
  • Save Last-Order/c786779c3c7a1e1127808c72d1aa19c8 to your computer and use it in GitHub Desktop.
Save Last-Order/c786779c3c7a1e1127808c72d1aa19c8 to your computer and use it in GitHub Desktop.
读取FLAC音频长度添加视频分段信息
// ==UserScript==
// @name 音频->分段
// @namespace Violentmonkey Scripts
// @match https://www.bilibili.com/video/*
// @grant none
// @version 1.0
// @author -
// @description 2022/7/27 11:52:53
// ==/UserScript==
(async () => {
const readFLACDuration = async (file) => {
return new Promise((resolve) => {
const reader = new FileReader();
reader.addEventListener("load", () => {
const temp = new Uint8Array(reader.result.slice(18, 21));
const sampleRate =
(temp[0] << 12) | (temp[1] << 4) | (temp[2] >>> 4);
const temp2 = new Uint8Array(reader.result.slice(0x15, 0x1a));
temp2[0] = temp2[0] & 0b00001111;
const totalSamples =
(temp2[0] << 32) |
(temp2[1] << 24) |
(temp2[2] << 16) |
(temp2[3] << 8) |
temp2[4];
resolve(Math.round(totalSamples / sampleRate));
});
reader.readAsArrayBuffer(file);
});
};
window.addCards = async () => {
const aid = window.aid;
const cid = window.cid;
const videoLength = window.__INITIAL_STATE__.videoData.duration;
const csrf = document.cookie.match(/bili_jct=(.+?);/)[1];
const titles = [];
const lengths = [];
const cards = [];
let counter = 0;
const fileInput = document.createElement("input");
fileInput.type = "file";
fileInput.multiple = true;
fileInput.addEventListener("change", async () => {
if (fileInput?.files?.length) {
for (const file of fileInput.files) {
const duration = await readFLACDuration(file);
titles.push(file.name.split(".").slice(0, -1).join(""));
lengths.push(duration);
}
titles.forEach((title, index) => {
cards.push({
from: counter,
to:
index === titles.length - 1
? videoLength
: counter + lengths[index],
content: title,
});
counter += lengths[index];
});
console.log(cards);
await fetch("https://member.bilibili.com/x/web/card/submit", {
method: "POST",
mode: "cors",
credentials: "include",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
aid,
cid,
csrf,
cards: JSON.stringify(cards),
type: 2,
permanent: true,
}),
});
}
});
fileInput.click();
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment