A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
extract clean transcript, test
Christian Fei9dd5a5fd7b33
diff --git a/index.html b/index.htmlindex 94634b5..68bc1b0 100644--- a/index.html+++ b/index.html@@ -141,7 +141,7 @@);opacity: 0.85;}-body.dialog-opened {+ body.dialog-opened {overflow: hidden;}</style>@@ -330,7 +330,7 @@ body.dialog-opened {const $closeSettings = $settings.querySelector("button")$openSettings.addEventListener("click", () => $settings.showModal())$closeSettings.addEventListener("click", () => $settings.close())-+// video player modalconst $videoPlayer = document.querySelector('dialog#video-player')const $closeVideoPlayer = $videoPlayer.querySelector("button")diff --git a/lib/subtitles-summary.js b/lib/subtitles-summary.jsnew file mode 100644index 0000000..9790dd6--- /dev/null+++ b/lib/subtitles-summary.js@@ -0,0 +1,12 @@+export function cleanTranscript (transcript) {+ return transcript+ .split('\n')+ .map(line => {+ // if line contains single number+ if (/^\d+/.test(line)) return+ // remove from lines the following pattern: 00:00:06,879 --> 00:00:14,080+ line = line.replace(/\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}/g, '')+ // remove all tags from the line+ return line.replace(/<[^>]+>/g, '').trim()+ }).filter(Boolean).join('\n').trim()+}diff --git a/server.js b/server.jsindex 964ec90..042d4b3 100644--- a/server.js+++ b/server.js@@ -3,6 +3,7 @@ import { URL } from 'url'import { execa } from 'execa'import fs from 'fs'import { exec } from 'child_process'+import { cleanTranscript } from './lib/subtitles-summary.js'fs.mkdirSync('./data', { recursive: true })fs.mkdirSync('./data/videos', { recursive: true })@@ -171,16 +172,18 @@ async function summarizeVideo(id, repo) {preserve new lines*/const transcriptPath = `./data/videos/${id}-sub.en.srt`- const transcriptContent = fs.readFileSync(transcriptPath, 'utf-8')- let cleanedTranscript = transcriptContent.replace(/\d+\n\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}\n<font color="white" size=".72c">(.*?)<\/font>/g, '$1\n').replace(/\n+/g, '\n')+ const transcript = fs.readFileSync(transcriptPath, 'utf-8')+ let cleanedTranscript = cleanTranscript(transcript)+ console.log('cleanedTranscript.length', cleanedTranscript.length)+ // cleanedTranscript = await makeSenseLMSTUDIO(cleanedTranscript)+ // console.log(cleanedTranscript)repo.setVideoTranscript(id, cleanedTranscript)fs.writeFileSync(`./data/videos/${id}-sub.en.txt`, cleanedTranscript)- const shortenedText = await shortenTextLMSTUDIO(cleanedTranscript)- const prompt = `Summarize the following transcript, in max 10 paragraphs, your output should just contain the summary, keep it short, succint, to the point: \n\n ${cleanedTranscript}`+ const prompt = `Summarize the following video transcript, in max 10 sentences, your output should just contain the summary, don't repeat yourself, keep it short, reply with just the summary: \n\n ${cleanedTranscript}`console.log('prompt', prompt)const summary = await fetchAIResponseLMSTUDIO(prompt)- console.log(summary)+ console.log('summary', summary)repo.setVideoSummary(id, summary)console.log('Download completed')@@ -214,7 +217,7 @@ async function fetchAIResponseOLLAMA (prompt) {}}async function fetchAIResponseLMSTUDIO (prompt) {- if (prompt.length > 2500) prompt = prompt.substring(0, 2500)+ if (prompt.length > 5000) prompt = prompt.substring(0, 5000)try {const response = await fetch('http://localhost:1234/v1/completions', {method: 'POST',@@ -222,7 +225,7 @@ async function fetchAIResponseLMSTUDIO (prompt) {'Content-Type': 'application/json'},body: JSON.stringify({- "model": "mlx-community/Llama-3.2-3B-Instruct-4bit",+ "model": "llama-3.2-3b-instruct","prompt": prompt,"temperature": 0.7,// "max_tokens": 10,@@ -275,27 +278,28 @@ async function shortenTextOLLAMA(text) {}return responses.join(' \n')}-async function shortenTextLMSTUDIO(text) {+async function makeSenseLMSTUDIO(text) {// split the text into 1600 character blocksconst chunks = []- for (let i = 0; i < text.length; i += 1800) {- chunks.push(text.substring(i, i + 1800))+ for (let i = 0; i < text.length; i += 1500) {+ chunks.push(text.substring(i, i + 1500))}+ console.log(chunks.length, 'chunks')const responses = []try {for (const chunk of chunks) {- console.log('shortening chunk', chunk.substring(0, 20))- const prompt = `shorten the following text: \n ${chunk}`+ console.log('making sense of chunk', chunk.substring(0, 20))+ const prompt = `rewrite the following youtube video transcript, reply in short form, always reply in plain text, don't repeat yourself, MAXIMUM 10 sentences: \n ${chunk}`const response = await fetch('http://localhost:1234/v1/completions', {method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify({- "model": "mlx-community/Llama-3.2-3B-Instruct-4bit",+ "model": "llama-3.2-3b-instruct","prompt": prompt,"temperature": 0.7,- // "max_tokens": 10,+ "max_tokens": 1000,"stream": false,// "stop": "\n"})diff --git a/test/subtitles.summary.test.js b/test/subtitles.summary.test.jsnew file mode 100644index 0000000..c9908e3--- /dev/null+++ b/test/subtitles.summary.test.js@@ -0,0 +1,26 @@+import {test} from 'node:test'+import assert from 'assert';+import { cleanTranscript } from '../lib/subtitles-summary.js'++test('cleans transcript', () => {+ const transcript = `+1+00:00:01,240 --> 00:00:06,879+<font color="white" size=".72c">when we are about to do something</font>++2+00:00:04,160 --> 00:00:10,519+<font color="white" size=".72c">new</font>++3+00:00:06,879 --> 00:00:14,080+<font color="white" size=".72c">Eh after finishing</font>+ `.trim()+ const expectedCleanTranscript = `+when we are about to do something+new+Eh after finishing+ `.trim()++ assert.strictEqual(cleanTranscript(transcript), expectedCleanTranscript)+})\ No newline at end of filediff --git a/yt.test.js b/yt.test.jsnew file mode 100644index 0000000..dab336c--- /dev/null+++ b/yt.test.js@@ -0,0 +1,31 @@+import {test} from 'node:test'+import assert from 'assert';+// read json file data/mr_rip.json+// import fs module to read the file+import fs, { chownSync } from 'fs';+import path from 'path';++const __dirname = new URL('.', import.meta.url).pathname;++const filePath = path.join(__dirname, 'data', 'veritasium.json');+const jsonData = JSON.parse(fs.readFileSync(filePath, 'utf8'));++test('extracts videos', () => {+ const videoTab = jsonData.contents.twoColumnBrowseResultsRenderer.tabs.find(t => t.tabRenderer.title === 'Video')+ assert.ok(videoTab);+ const videoContents = videoTab.tabRenderer.content.richGridRenderer.contents+ assert.ok(videoContents)+ const v = videoContents.filter(v => {+ return v.richItemRenderer?.content?.videoRenderer?.title?.runs[0]?.text+ }).map(v => ({+ title: v.richItemRenderer.content.videoRenderer.title.runs[0].text,+ url: `https://www.youtube.com/watch?v=${v.richItemRenderer.content.videoRenderer.videoId}`,+ thumbnail: v.richItemRenderer.content.videoRenderer.thumbnail.thumbnails[0].url,+ description: v.richItemRenderer.content.videoRenderer.descriptionSnippet.runs[0].text,+ videoId: v.richItemRenderer.content.videoRenderer.videoId,+ publishedTime: v.richItemRenderer.content.videoRenderer.publishedTimeText.simpleText,+ viewCount: v.richItemRenderer.content.videoRenderer.viewCountText.simpleText,+ duration: v.richItemRenderer.content.videoRenderer.lengthText ? v.richItemRenderer.content.videoRenderer.lengthText.simpleText : null,+ }))+ console.log(v)+});\ No newline at end of file