Guest User

Untitled

a guest
Mar 8th, 2025
1,712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import sys
  2. import re
  3. import requests
  4. from youtube_transcript_api import YouTubeTranscriptApi
  5.  
  6. def extract_video_id(input_str):
  7. """Extract the video ID from a full YouTube URL or use it directly if an ID is given."""
  8. if "youtube.com" in input_str or "youtu.be" in input_str:
  9. return input_str.split("v=")[-1].split("&")[0].split("?")[0]
  10. return input_str # Assume it's a video ID
  11.  
  12. def get_video_title(video_id):
  13. """Try to get the video title without using pytube."""
  14. try:
  15. url = f"https://www.youtube.com/watch?v={video_id}"
  16. response = requests.get(url)
  17. if response.status_code == 200:
  18. match = re.search(r'<title>(.*?) - YouTube</title>', response.text)
  19. if match:
  20. return match.group(1).strip()
  21. except Exception as e:
  22. print(f"Warning: Failed to fetch title ({e})")
  23. return "UnknownTitle" # Fallback if title cannot be fetched
  24.  
  25. def get_youtube_transcript(video_id):
  26. """Retrieve transcript for a given YouTube video ID."""
  27. try:
  28. transcript = YouTubeTranscriptApi.get_transcript(video_id)
  29. return "\n".join([entry["text"] for entry in transcript])
  30. except Exception as e:
  31. return f"Error retrieving transcript: {e}"
  32.  
  33. def sanitize_filename(name):
  34. """Sanitize filename to remove special characters."""
  35. return re.sub(r'[\/:*?"<>|]', "", name)
  36.  
  37. def main():
  38. if len(sys.argv) < 2:
  39. print("Usage: python script.py '<YouTube video URL or ID>'")
  40. sys.exit(1)
  41.  
  42. input_str = sys.argv[1]
  43. video_id = extract_video_id(input_str)
  44. video_title = get_video_title(video_id)
  45.  
  46. transcript = get_youtube_transcript(video_id)
  47.  
  48. # Save transcript to a file with title and video ID
  49. filename = f"{sanitize_filename(video_title)}_{video_id}_transcript.txt"
  50. with open(filename, "w", encoding="utf-8") as f:
  51. f.write(transcript)
  52.  
  53. print(f"Transcript saved as: {filename}")
  54.  
  55. if __name__ == "__main__":
  56. main()
  57.  
Advertisement
Add Comment
Please, Sign In to add comment