[Plugins Required] Copy and Clean Video Transcripts

Disclaimer

Is this project open source? Yes
Is this project completely free? Yes
Is this project made with AI beyond the author’s ability to comprehend how it works? No


Ever wanted to copy a video transcript from YouTube into your vault so that you could archive a specific section, quote, or maybe just the entire thing because the creator’s entire video was so informative?

TIP: If you’re a Firefox user, extensions exist (I can’t validate their safety at all; this is not an advertisement) so you don’t have to drag your cursor from start to end; I use YouTube Transcript Copier by Dislike Lever.

Showcase

  1. STEP 1: Go to my other post and follow the instructions to fix the disjoint paste from YouTube transcripts: https://forum.obsidian.md/t/plugins-required-fixing-disjoint-line-breaks-in-pdfs-when-copying-text/118042
  2. STEP 2: Install the QuickAdd plugin (and maybe the Editing Toolbar plugin if you have a lot of scripts/don’t remember what they all are).
  3. STEP 3: Copy the script at the bottom of this post. Add it to User Plugins.
  4. STEP 4: Create a QuickAdd Macro to User Plugins: Paste & Normalize, then wait some milliseconds (I set it to 1,000, though that’s overkill), and then to run the new script User Plugins: Clean Video Transcript. Call it whatever, though I named it Paste Video Transcript for a generic name.
/* Transcripts: Removes timestamps/filler words/repetition, adds capitalization */

plugin.addCommand({
    name: 'Clean Video Transcript',
    id: "clean-video-transcript",
    callback: async () => {
        const activeView = app.workspace.activeLeaf.view;
        const editor = activeView.editor;
        let corrected = await editor.getSelection();

        if (!corrected) {
            window.alert("No text in selection");
            return;
        }
        
        // PART 1: Remove timestamps
        corrected = corrected.replace(/(\n- )*([\s?!.,;…])*(\s*?)(\([0-9]+(:[0-9]+)+\))(\s*)/g, "$1$2");
        corrected = corrected.replace(/(\n- )*([\s?!.,;…])*(\s*?)([0-9]+(:[0-9]+)+((,|\s)*[0-9]+ (minute|second|hour)(s*))*)(\s*)/g, "$1$2");

        // PART 2: Remove "uh" and [music]
        corrected = corrected.replace(/(^|[^A-Za-z0-9])((U|u)h|(U|u)m|(Y|y)ou know)([,]*)(\s*)/g, "$1");
        corrected = corrected.replace(/(\[(M|m)usic\])(\s*)/g, "");
        corrected = corrected.replace(/(\[\s*__\s*\])/g, "\\[expletive\\]");

        // PART 3: Capitalize properly
        let re = /([?!.…]\s|\n- )[a-z]/g;
        corrected = corrected.replace(re, function(x){ return x.toUpperCase(); });

        // PART 4: Remove repetition (2x words)
        corrected = corrected.split(" ");
        let corrected2 = [];
        while(corrected.length > 0) {
            let c = corrected.shift();
            if(corrected2.length == 0 || !(c.toUpperCase() === corrected2[corrected2.length - 1].toUpperCase())) { corrected2.push(c); }
        } 
        corrected2 = corrected2.join(" ")

        editor.replaceSelection(corrected2);
    }
});