youngtsai commited on
Commit
f91a6d2
·
1 Parent(s): 0cfeb8d

parse_time

Browse files
Files changed (1) hide show
  1. app.py +19 -3
app.py CHANGED
@@ -22,6 +22,7 @@ import os
22
  import io
23
  import time
24
  import json
 
25
  from urllib.parse import urlparse, parse_qs
26
 
27
  from google.cloud import storage
@@ -347,6 +348,18 @@ def docx_to_text(file):
347
 
348
 
349
  # ---- YouTube link ----
 
 
 
 
 
 
 
 
 
 
 
 
350
  def format_seconds_to_time(seconds):
351
  """将秒数格式化为 时:分:秒 的形式"""
352
  hours = int(seconds // 3600)
@@ -1188,10 +1201,13 @@ def generate_key_moments(formatted_simple_transcript, formatted_transcript):
1188
  print(key_moments)
1189
  print("=====key_moments=====")
1190
  image_links = {entry['start_time']: entry['screenshot_path'] for entry in formatted_transcript}
 
1191
  for moment in key_moments:
1192
- start_time = moment['start']
1193
- end_time = moment['end']
1194
- moment_images = [image_links[time] for time in image_links if start_time <= time <= end_time]
 
 
1195
  moment['images'] = moment_images
1196
 
1197
  return key_moments
 
22
  import io
23
  import time
24
  import json
25
+ from datetime import timedelta
26
  from urllib.parse import urlparse, parse_qs
27
 
28
  from google.cloud import storage
 
348
 
349
 
350
  # ---- YouTube link ----
351
+ def parse_time(time_str):
352
+ """將時間字符串 'HH:MM:SS' 或 'MM:SS' 轉換為 timedelta 物件。"""
353
+ parts = list(map(int, time_str.split(':')))
354
+ if len(parts) == 3:
355
+ hours, minutes, seconds = parts
356
+ elif len(parts) == 2:
357
+ hours = 0 # 沒有小時部分時,將小時設為0
358
+ minutes, seconds = parts
359
+ else:
360
+ raise ValueError("時間格式不正確,應為 'HH:MM:SS' 或 'MM:SS'")
361
+ return timedelta(hours=hours, minutes=minutes, seconds=seconds)
362
+
363
  def format_seconds_to_time(seconds):
364
  """将秒数格式化为 时:分:秒 的形式"""
365
  hours = int(seconds // 3600)
 
1201
  print(key_moments)
1202
  print("=====key_moments=====")
1203
  image_links = {entry['start_time']: entry['screenshot_path'] for entry in formatted_transcript}
1204
+
1205
  for moment in key_moments:
1206
+ start_time = parse_time(moment['start'])
1207
+ end_time = parse_time(moment['end'])
1208
+ # 使用轉換後的 timedelta 物件進行時間比較
1209
+ moment_images = [image_links[time] for time in image_links
1210
+ if start_time <= parse_time(time) <= end_time]
1211
  moment['images'] = moment_images
1212
 
1213
  return key_moments