Chrunos commited on
Commit
222a7c1
·
verified ·
1 Parent(s): a780585

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py CHANGED
@@ -126,6 +126,52 @@ async def get_video_url(youtube_url: str):
126
 
127
 
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  # Define a global temporary download directory
130
  global_download_dir = tempfile.mkdtemp()
131
 
 
126
 
127
 
128
 
129
+ @app.get("/script")
130
+ async def get_transcript(youtube_url: str):
131
+ try:
132
+ # 定义 ydl_opts
133
+ ydl_opts = {
134
+ 'skip_download': True,
135
+ 'writesubtitles': True,
136
+ 'writeautomaticsub': True,
137
+ 'subtitleslangs': ['en'], # 可以根据需要修改语言
138
+ 'format': 'bestaudio/best',
139
+ 'outtmpl': '%(id)s.%(ext)s',
140
+ 'noplaylist': True
141
+ }
142
+ cookiefile = "firefox-cookies.txt"
143
+ ydl_opts["cookiefile"] = cookiefile
144
+
145
+ with YoutubeDL(ydl_opts) as ydl:
146
+ info = ydl.extract_info(youtube_url, download=False)
147
+ subtitles = info.get('subtitles', {})
148
+ auto_subtitles = info.get('automatic_captions', {})
149
+
150
+ transcript = None
151
+ if subtitles:
152
+ # 优先使用手动字幕
153
+ for lang in ydl_opts['subtitleslangs']:
154
+ if lang in subtitles:
155
+ transcript = subtitles[lang]
156
+ break
157
+ if not transcript and auto_subtitles:
158
+ # 如果没有手动字幕,使用自动生成的字幕
159
+ for lang in ydl_opts['subtitleslangs']:
160
+ if lang in auto_subtitles:
161
+ transcript = auto_subtitles[lang]
162
+ break
163
+
164
+ if transcript:
165
+ text = ' '.join([entry['text'] for entry in transcript])
166
+ return jsonify({'transcript': text})
167
+ else:
168
+ return jsonify({'transcript': 'No transcript available'})
169
+
170
+ except Exception as e:
171
+ raise HTTPException(status_code=500, detail=str(e))
172
+
173
+
174
+
175
  # Define a global temporary download directory
176
  global_download_dir = tempfile.mkdtemp()
177