Wrap in a main function and use args
This commit is contained in:
33
summarize.py
33
summarize.py
@@ -1,13 +1,11 @@
|
|||||||
#! /usr/bin/env python3
|
#! /usr/bin/env python3
|
||||||
|
|
||||||
|
import argparse
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import yt_dlp
|
import yt_dlp
|
||||||
from ollama import chat, ChatResponse, Client
|
from ollama import chat, ChatResponse, Client
|
||||||
|
|
||||||
#video_url = 'https://youtu.be/jl4HOY8ZaEA'
|
|
||||||
video_url = 'https://www.youtube.com/watch?v=kTctVqjhDEw'
|
|
||||||
|
|
||||||
ydl_opts = {
|
ydl_opts = {
|
||||||
'writesubtitles': True, # Enable downloading subtitles
|
'writesubtitles': True, # Enable downloading subtitles
|
||||||
'subtitleslangs': ['en'], # Specify subtitle language(s)
|
'subtitleslangs': ['en'], # Specify subtitle language(s)
|
||||||
@@ -69,9 +67,25 @@ def concatenate_subtitles(subtitle_json):
|
|||||||
# Join all collected text with spaces and return
|
# Join all collected text with spaces and return
|
||||||
return ' '.join(result)
|
return ' '.join(result)
|
||||||
|
|
||||||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Download subtitles from a video and summarize it using a local Ollama instance."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'url',
|
||||||
|
metavar='URL',
|
||||||
|
type=str,
|
||||||
|
help="The URL of the video to process."
|
||||||
|
)
|
||||||
|
|
||||||
|
# Parse out arguments
|
||||||
|
args = parser.parse_args()
|
||||||
|
video_url = args.url
|
||||||
|
|
||||||
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||||
# Extract metadata without downloading the video
|
# Extract metadata without downloading the video
|
||||||
info = ydl.extract_info(video_url, download=False)
|
info = ydl.extract_info(video_url, download=False)
|
||||||
|
print(f"Summarizing video: {info.get('title', 'Unknown Title')}...")
|
||||||
|
|
||||||
# Check if subtitles are available
|
# Check if subtitles are available
|
||||||
subtitle_lang = 'en' # Change this to your desired language
|
subtitle_lang = 'en' # Change this to your desired language
|
||||||
@@ -79,23 +93,26 @@ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|||||||
automatic_subtitles_available = info.get('automatic_captions', {})
|
automatic_subtitles_available = info.get('automatic_captions', {})
|
||||||
|
|
||||||
if subtitle_lang in subtitles_available:
|
if subtitle_lang in subtitles_available:
|
||||||
print(f"Downloading manual subtitles for language: {subtitle_lang}")
|
print(f"Downloading manual subtitles for language: {subtitle_lang}...")
|
||||||
subtitle_url = subtitles_available[subtitle_lang][0]['url']
|
subtitle_url = subtitles_available[subtitle_lang][0]['url']
|
||||||
elif subtitle_lang in automatic_subtitles_available:
|
elif subtitle_lang in automatic_subtitles_available:
|
||||||
print(f"No manual subtitles available. Falling back to auto-generated subtitles for language: {subtitle_lang}")
|
print(f"No manual subtitles available. Falling back to auto-generated subtitles for language: {subtitle_lang}...")
|
||||||
subtitle_url = automatic_subtitles_available[subtitle_lang][0]['url']
|
subtitle_url = automatic_subtitles_available[subtitle_lang][0]['url']
|
||||||
else:
|
else:
|
||||||
print(f"No subtitles (manual or auto-generated) available for language: {subtitle_lang}")
|
print(f"No subtitles (manual or auto-generated) available for language: {subtitle_lang}!")
|
||||||
subtitle_url = None
|
subtitle_url = None
|
||||||
|
exit(51)
|
||||||
|
|
||||||
# If a subtitle URL was found, download the subtitles
|
# If a subtitle URL was found, download the subtitles
|
||||||
if subtitle_url:
|
if subtitle_url:
|
||||||
subtitle_data = ydl.urlopen(subtitle_url).read().decode('utf-8')
|
subtitle_data = ydl.urlopen(subtitle_url).read().decode('utf-8')
|
||||||
else:
|
else:
|
||||||
print("Failed to download subtitles.")
|
print("Failed to download subtitles!")
|
||||||
exit(50)
|
exit(50)
|
||||||
|
|
||||||
subs = concatenate_subtitles(json.loads(subtitle_data))
|
subs = concatenate_subtitles(json.loads(subtitle_data))
|
||||||
print("Getting summary...")
|
print("Getting summary...")
|
||||||
summary = get_summary(subs)
|
summary = get_summary(subs)
|
||||||
print(summary)
|
print(summary)
|
||||||
|
|
||||||
|
main()
|
||||||
|
Reference in New Issue
Block a user