Last active
November 19, 2025 20:32
-
-
Save kellenmace/2b597319990cf0c3c1fb525e90b7b5bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Innertube } from "youtubei.js"; | |
| /** | |
| * Fetches details and transcript for a YouTube video. | |
| * @param {string} videoId - The YouTube Video ID. | |
| * @return {Object} Video details and transcript. | |
| */ | |
| async function getVideo(videoId) { | |
| const youtube = await Innertube.create(); | |
| const info = await youtube.getInfo(videoId); | |
| let transcript = null; | |
| try { | |
| const transcriptData = await info.getTranscript(); | |
| const segments = transcriptData.transcript.content.body.initial_segments; | |
| if (segments && segments.length > 0) { | |
| transcript = segments | |
| .filter((segment) => segment.snippet && segment.snippet.text) | |
| .map((segment) => ({ | |
| text: segment.snippet.text, | |
| start_ms: segment.start_ms, | |
| })); | |
| } | |
| } catch (transcriptError) { | |
| console.warn( | |
| "Could not retrieve transcript. The video may not have captions enabled." | |
| ); | |
| console.warn("Error message:", transcriptError.message); | |
| } | |
| return { | |
| id: info.basic_info.id, | |
| title: info.basic_info.title, | |
| author: info.basic_info.author, | |
| channel_id: info.basic_info.channel?.id ?? null, | |
| duration: info.basic_info.duration, | |
| view_count: info.basic_info.view_count, | |
| transcript: transcript, | |
| }; | |
| } | |
| // Example Usage: | |
| try { | |
| const video = await getVideo("jNQXAC9IVRw"); | |
| console.log(video); | |
| } catch (error) { | |
| console.error("An error occurred while fetching the video:", error); | |
| } | |
| // Example Response: | |
| // { | |
| // id: 'jNQXAC9IVRw', | |
| // title: 'Me at the zoo', | |
| // author: 'jawed', | |
| // channel_id: 'UC4QobU6STFB0P71PMvOGN5A', | |
| // duration: 19, | |
| // view_count: 375769099, | |
| // transcript: [ | |
| // { | |
| // text: 'All right, so here we are, in front of the\nelephants', | |
| // start_ms: '1200' | |
| // }, | |
| // { | |
| // text: 'the cool thing about these guys is that they\nhave really...', | |
| // start_ms: '5318' | |
| // }, | |
| // { text: 'really really long trunks', start_ms: '7974' }, | |
| // { text: "and that's cool", start_ms: '12616' }, | |
| // { text: '(baaaaaaaaaaahhh!!)', start_ms: '14421' }, | |
| // { | |
| // text: "and that's pretty much all there is to\nsay", | |
| // start_ms: '16881' | |
| // } | |
| // ] | |
| // } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment