DEV Community

Cover image for Build Your YouTube Video Transcriber with Streamlit & Youtube API's πŸš€
Jagroop Singh
Jagroop Singh

Posted on

Build Your YouTube Video Transcriber with Streamlit & Youtube API's πŸš€

Hey there! πŸ‘‹ Imagine watching a YouTube video and thinking, "I wish I could have the transcript in seconds." Well, this handy YouTube Video Transcriber app does just that! Let’s dive straight in and see how it works. πŸ”

Here is the demo of this application :


What Does This App Do? πŸŽ₯✍️

This app lets you:

  1. Fetch Video Details: Get the title and description of a YouTube video.
  2. Transcribe Videos: Extract subtitles/transcripts in seconds (if available).

Cool, right? You don’t even need to leave your browser.


How Does It Work? πŸ› οΈ

  1. Enter YouTube API Key πŸ”‘: To access video details, you'll need your YouTube Data API Key.
  2. Paste YouTube URL πŸ”—: Share the video link you want transcribed.
  3. Hit Transcribe ✨: The app fetches the details and subtitles, displaying them instantly.

Keys Setup πŸ”‘

Step 1: Set Up a Google Cloud Project

1) Go to the Google Cloud Console.
Go to the [Google Cloud Console]

2) Sign in with your Google account.
3) Click Select a Project in the top menu, then click New Project.
Select a Project

4) Enter a project name (e.g., "Video Transcriber") and click Create.

Video Transcriber

Transcriber


Step 2: Enable the YouTube Data API

1) In the Cloud Console, go to the API Library.

[API Library]

2) Search for YouTube Data API v3.
3) Click on the YouTube Data API v3 result, then click Enable.

Enable

Step 3: Create API Credentials

1) Go to the Credentials page.
2) Click + CREATE CREDENTIALS and select API Key.
CREDENTIALS

3) Your API key will be generated. Copy it and save it somewhere safe.


Code Breakdown πŸ’»

Here’s the magic:

Extracting the Video ID πŸ“½οΈ

The get_video_id() function grabs the video ID from URLs like https://www.youtube.com/watch?v=VIDEO_ID.

def get_video_id(url):
    if "watch?v=" in url:
        return url.split("watch?v=")[1].split("&")[0]
    elif "youtu.be/" in url:
        return url.split("youtu.be/")[1].split("?")[0]
    return None
Enter fullscreen mode Exit fullscreen mode

Fetching Video Details πŸ“

Using the YouTube Data API, fetch_video_details() pulls video title and description.

youtube = build("youtube", "v3", developerKey=api_key)
request = youtube.videos().list(part="snippet", id=video_id)
response = request.execute()
Enter fullscreen mode Exit fullscreen mode

Fetching the Transcript πŸ—£οΈ

The YouTubeTranscriptApi library retrieves the transcript for the video (if enabled).

transcript = YouTubeTranscriptApi.get_transcript(video_id)
return "\n".join([item['text'] for item in transcript])
Enter fullscreen mode Exit fullscreen mode

Streamlit Magic πŸͺ„

Finally, Streamlit makes the app interactive and easy to use:

  • Accept inputs for API key and video URL.
  • Show video details and transcript on the same page.

Here is the final code details :

import streamlit as st
from googleapiclient.discovery import build
from youtube_transcript_api import YouTubeTranscriptApi

def get_video_id(url):
    """Extracts video ID from a YouTube URL."""
    if "watch?v=" in url:
        return url.split("watch?v=")[1].split("&")[0]
    elif "youtu.be/" in url:
        return url.split("youtu.be/")[1].split("?")[0]
    return None

def fetch_video_details(api_key, video_id):
    """Fetches video details using the YouTube Data API."""
    youtube = build("youtube", "v3", developerKey=api_key)
    request = youtube.videos().list(part="snippet", id=video_id)
    response = request.execute()

    if "items" in response and len(response["items"]) > 0:
        snippet = response["items"][0]["snippet"]
        title = snippet["title"]
        description = snippet["description"]
        return title, description
    return None, None

def fetch_transcript(video_id):
    """Fetches the transcript for a YouTube video."""
    try:
        transcript = YouTubeTranscriptApi.get_transcript(video_id)
        return "\n".join([item['text'] for item in transcript])
    except Exception as e:
        return f"Error fetching transcript: {str(e)}"

# Streamlit UI
st.title("YouTube Video Transcriber")

youtube_api_key = st.text_input("Enter your YouTube Data API Key:", type="password")
youtube_url = st.text_input("Enter YouTube Video URL:")

if st.button("Transcribe"):
    if not youtube_api_key or not youtube_url:
        st.error("Please provide both API Key and Video URL.")
    else:
        video_id = get_video_id(youtube_url)

        if not video_id:
            st.error("Invalid YouTube URL.")
        else:
            with st.spinner("Fetching video details..."):
                title, description = fetch_video_details(youtube_api_key, video_id)

            if not title:
                st.error("Unable to fetch video details. Check API Key and URL.")
            else:
                st.success("Video details fetched successfully!")
                st.write(f"**Title:** {title}")
                st.write(f"**Description:** {description}")

                with st.spinner("Fetching transcript..."):
                    transcript = fetch_transcript(video_id)

                st.text_area("Transcript:", value=transcript, height=300)
                st.success("Transcript fetched successfully!")

Enter fullscreen mode Exit fullscreen mode

Setup in 3 Steps ⚑

1) Install the dependencies:

   pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

2) Run the app:

   streamlit run app.py
Enter fullscreen mode Exit fullscreen mode

3) Enjoy your transcripts instantly! πŸŽ‰


Future Features? πŸ€”

Here are some cool ideas you can add:

  • Download Transcript as a text file. πŸ“„
  • Summarize Transcript using AI. πŸ€–
  • Translate Transcript to other languages. 🌍

Feel inspired? Head over to the GitHub Repo and contribute your ideas! πŸš€

GitHub logo Jagroop2001 / video-transcriber

Video Transcriber with Streamlit & Youtube API

YouTube Video Transcriber

This is a Streamlit application that allows you to fetch and transcribe the content of a YouTube video using the YouTube Data API and YouTube Transcript API.

Features

  • Extracts the video ID from a YouTube URL.
  • Fetches the title and description of the YouTube video using the YouTube Data API.
  • Retrieves and displays the transcript of the YouTube video.
  • User-friendly interface built with Streamlit.

Requirements

This project requires the following Python packages:

  • streamlit
  • google-api-python-client
  • youtube-transcript-api

To install the necessary dependencies, create a virtual environment and install the packages from the requirements.txt file:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Setting Up

  1. Obtain a YouTube Data API key by following the instructions on the Google Developers Console.
  2. Clone or download this repository to your local machine.
  3. Install the required dependencies using the command above.
  4. Run the Streamlit app:
streamlit run app.py
Enter fullscreen mode Exit fullscreen mode

Usage

  1. Open the application in your web browser (usually…




Let’s build something awesome together! πŸ™Œ

Top comments (8)

Collapse
 
hraifi profile image
sewiko

It cause issue which python version are you using for this. @jagroop2001

Collapse
 
jagroop2001 profile image
Jagroop Singh

My system have python version of 3.11.11 , But I think any python3 version will works well.

Collapse
 
hraifi profile image
sewiko

got it, I am using version 2 that's why it cause issue.

Thread Thread
 
jagroop2001 profile image
Jagroop Singh

It would also work for some python2 version.

Collapse
 
john12 profile image
john

Such a great, simple, clear project. I would surely contribute to this.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thanks @john12

Collapse
 
works profile image
Web

It's such a cool & simple mini project. Also code is very clearly written.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thanks @works