Created
September 15, 2024 20:23
-
-
Save seqis/9e226b84c056d39ac3fa8ed7b19ee5a7 to your computer and use it in GitHub Desktop.
This python script will take any YouTube URL in the clipboard & go out and grab the transcript (if available) and put it directly into the clipboard for pasting.
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
#!/usr/bin/env python3 | |
# This python script will take any YouTube URL in the clipboard & go out and grab the transcript (if available) and put it directly into the clipboard for pasting. | |
import pyperclip | |
from youtube_transcript_api import YouTubeTranscriptApi | |
import re | |
import tkinter as tk | |
def get_video_id(url): | |
video_id = re.search(r"(?<=v=)[^&#]+", url) | |
if not video_id: | |
video_id = re.search(r"(?<=be/)[^&#]+", url) | |
return video_id.group(0) if video_id else None | |
def fetch_transcript(video_id): | |
try: | |
transcript = YouTubeTranscriptApi.get_transcript(video_id) | |
transcript_text = "\n".join([entry['text'] for entry in transcript]) | |
return transcript_text | |
except Exception as e: | |
raise Exception(f"Error fetching transcript: {e}") | |
def show_error(error_message): | |
def copy_to_clipboard(): | |
pyperclip.copy(error_message) | |
root = tk.Tk() | |
root.title("Script Error") | |
text = tk.Text(root, wrap=tk.WORD, height=10, width=50) | |
text.insert(tk.END, error_message) | |
text.config(state=tk.DISABLED) | |
text.pack() | |
button = tk.Button(root, text="Copy to Clipboard", command=copy_to_clipboard) | |
button.pack() | |
root.mainloop() | |
def main(): | |
try: | |
url = pyperclip.paste().strip() | |
if not re.match(r'^(https?\:\/\/)?(www\.youtube\.com|youtu\.?be)\/.+$', url): | |
raise ValueError("Clipboard content is not a valid YouTube URL.") | |
video_id = get_video_id(url) | |
if video_id: | |
transcript = fetch_transcript(video_id) | |
pyperclip.copy(transcript) | |
print("Transcript copied to clipboard.") | |
else: | |
raise ValueError("Could not extract video ID from the URL.") | |
except Exception as e: | |
show_error(str(e)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment