File size: 1,159 Bytes
4464026
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from langchain_community.document_loaders import YoutubeLoader
from langchain_community.document_loaders.youtube import TranscriptFormat
import os
import json


def get_youtube_transcript(video_url,src_language, target_language) -> None:
    """get youtube transcript"""
    try:

        loader = YoutubeLoader.from_youtube_url(video_url,add_video_info=True,language=[src_language, "id"],translation=target_language, transcript_format=TranscriptFormat.TEXT)
             
        #docs = loader.load_and_split(text_splitter)
        transcript = loader.load()
        
        #no transcript is found
        if len(transcript) == 0:
           return 'No transcript found', None, None
        
        #metadata = transcript[0].metadata
        
        #title = metadata.get('title')
        #metadata['transcript'] = transcript[0].page_content
        #metadata['youtubelink']=video_url
        if not transcript:
            raise ValueError(f"{src_language} transcript not available for this video.")
        else:
            return transcript[0].page_content
    except Exception as e:
        print(f"An error occurred: {e}")
        return None