From a0232c3670513f63d00bebaaad3276db5b49edd5 Mon Sep 17 00:00:00 2001 From: jtrue Date: Fri, 8 Dec 2023 02:56:57 -0500 Subject: [PATCH] Add files via upload --- __init__.py | 3 +- jru_youtube2prompt.py | 70 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 jru_youtube2prompt.py diff --git a/__init__.py b/__init__.py index ccbeac8..1fc743b 100644 --- a/__init__.py +++ b/__init__.py @@ -10,7 +10,8 @@ node_list = [ #Add list of .py files containing nodes here "jru_concatstring", "jru_float2int", "jru_string2int", - "jru_int2string" + "jru_int2string", + "jru_youtube2prompt" ] diff --git a/jru_youtube2prompt.py b/jru_youtube2prompt.py new file mode 100644 index 0000000..57eb8b6 --- /dev/null +++ b/jru_youtube2prompt.py @@ -0,0 +1,70 @@ +import os +#import shutil +import json +from youtube_transcript_api import YouTubeTranscriptApi +from youtube_transcript_api.formatters import JSONFormatter +#https://pypi.org/project/youtube-transcript-api/ + +class YouTube2Prompt_jru: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "video_id": ("STRING", {"default":"VjMoRb5f-EQ","multiline": False}), + "fps": ("INT", {"default":10,"min":1,"max": 1000}), + "begin": ("INT", {"default":0,"min":0,"max": 10000000}), + "cache": (["enable", "disable"], {"default": "enable"}), +# "cache_reset":("BOOLEAN", {"default": False}),# "forceInput": True}),}, + } + } + RETURN_TYPES = ("STRING", ) + FUNCTION = "fetchIt2" + CATEGORY = "JaRue" + def fetchIt2(self, video_id, fps, begin, cache): + my_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "transcripts") + if not os.path.exists(my_path): + os.makedirs(my_path) + wr = os.path.join(my_path, video_id+".json") + if os.path.exists(wr) and cache == False: + print('We have deleted the cache!') + os.remove(wr) +# shutil.rmtree(wr) + if os.path.exists(wr) and cache == "enable": + print('We have cache!') + else: + print('We must YouTube it!') + transcript = YouTubeTranscriptApi.get_transcript(video_id) + formatter = JSONFormatter() + json_formatted = formatter.format_transcript(transcript) + with open(wr, 'w', encoding='utf-8') as json_file: + json_file.write(json_formatted) + f = open(wr) + data = json.load(f) + print("data length="+str(len(data))) + outTxt = "" + cnt = 0 + for d in data: + fr = d['start']*fps + if (begin > -1): + fr = fr-begin + if (fr < 0): + continue +# print(str(fr)+"..."+str(d['start'])+"="+str(d['text'])) + cnt += 1 + comma = "," + if cnt == len(data): + comma = "" + outTxt += "\""+str(int(fr))+"\": \""+str(d['text'])+"\""+comma+"\n" + f.close() + return {"ui": {"text": outTxt}, "result": ( outTxt,)} + +NODE_CLASS_MAPPINGS = { + "YouTube2Prompt_jru": YouTube2Prompt_jru +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "YouTube2Prompt_jru": "YouTube to Prompt" +}