adamchanadam commited on
Commit
aecb255
·
verified ·
1 Parent(s): dbb6bf9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -27
app.py CHANGED
@@ -21,6 +21,11 @@ from logging.handlers import RotatingFileHandler
21
  from werkzeug.utils import secure_filename
22
  from collections import deque
23
 
 
 
 
 
 
24
  # 設置基本日誌配置
25
  logging.basicConfig(level=logging.INFO)
26
  logger = logging.getLogger(__name__)
@@ -81,35 +86,15 @@ def send_sse_message(q, data):
81
  def clean_filename(filename):
82
  return ''.join(c for c in filename if c.isalnum() or c in (' ', '.', '_')).rstrip()
83
 
84
- def download_audio(youtube_url, save_directory, q):
85
  send_sse_message(q, {"status": "開始下載 YouTube 音頻..."})
86
- unique_id = str(uuid.uuid4())[:8] # 生成一個唯一的識別碼
87
  output_filename = f"audio_{unique_id}"
88
  output_path = os.path.join(save_directory, output_filename)
89
 
90
- ydl_opts = {
91
- 'format': 'bestaudio/best',
92
- 'postprocessors': [{
93
- 'key': 'FFmpegExtractAudio',
94
- 'preferredcodec': 'mp3',
95
- 'preferredquality': '192',
96
- }],
97
- 'ffmpeg_location': ffmpeg_path,
98
  'outtmpl': output_path + ".%(ext)s",
99
- 'quiet': True,
100
- 'no_warnings': True,
101
- 'no_check_certificate': True,
102
- 'ignoreerrors': True,
103
- 'nocheckcertificate': True,
104
- 'logtostderr': False,
105
- 'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
106
- 'socket_timeout': 30, # 增加超時時間
107
- 'retries': 5, # 失敗時的重試次數
108
- 'verbose': True, # 啟用詳細日誌輸出,有助於調試
109
- 'extract_flat': 'in_playlist',
110
- 'youtube_include_dash_manifest': False,
111
- 'source_address': '0.0.0.0', # 綁定到所有接口
112
- }
113
 
114
  try:
115
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
@@ -317,6 +302,17 @@ def process_video(url_or_path, q, local_video_description=''):
317
  logger.info(f"開始處理視頻: {url_or_path}")
318
  save_directory = config['save_directory']
319
 
 
 
 
 
 
 
 
 
 
 
 
320
  processed_description = ""
321
  if url_or_path.startswith('http'):
322
  logger.info("檢測到 YouTube URL,開始獲取視頻信息")
@@ -331,9 +327,7 @@ def process_video(url_or_path, q, local_video_description=''):
331
  'outtmpl': os.path.join(save_directory, 'audio_%(id)s.%(ext)s'),
332
  'quiet': True,
333
  'no_warnings': True,
334
- 'no_check_certificate': True,
335
  'ignoreerrors': True,
336
- 'nocheckcertificate': True,
337
  'logtostderr': False,
338
  'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
339
  'socket_timeout': 30,
@@ -342,6 +336,7 @@ def process_video(url_or_path, q, local_video_description=''):
342
  'extract_flat': 'in_playlist',
343
  'youtube_include_dash_manifest': False,
344
  'source_address': '0.0.0.0',
 
345
  }
346
 
347
  try:
@@ -365,7 +360,7 @@ def process_video(url_or_path, q, local_video_description=''):
365
  processed_description = process_youtube_description(raw_description)
366
 
367
  logger.info("開始下載 YouTube 音頻")
368
- audio_path, video_title = download_audio(url_or_path, save_directory, q)
369
  except yt_dlp.utils.DownloadError as e:
370
  error_message = str(e)
371
  if "Sign in to confirm you're not a bot" in error_message:
@@ -378,11 +373,14 @@ def process_video(url_or_path, q, local_video_description=''):
378
  send_sse_message(q, {"status": f"錯誤:{str(e)}"})
379
  logger.error(f"值錯誤: {str(e)}")
380
  raise
 
381
  else:
 
382
  logger.info("檢測到本地文件路徑,開始處理本地視頻")
383
  audio_path, video_title = process_local_video(url_or_path, save_directory, q)
384
  processed_description = local_video_description if local_video_description else "這是一個本地視頻文件,用戶沒有提供視頻描述。"
385
 
 
386
  if not audio_path or not os.path.exists(audio_path):
387
  raise FileNotFoundError(f"音頻文件不存在: {audio_path}")
388
 
@@ -452,6 +450,11 @@ def process_video(url_or_path, q, local_video_description=''):
452
  except Exception as e:
453
  logger.exception("處理視頻時發生錯誤")
454
  send_sse_message(q, {"status": f"錯誤: {str(e)}"})
 
 
 
 
 
455
 
456
 
457
  # 在全局變量部分添加:
 
21
  from werkzeug.utils import secure_filename
22
  from collections import deque
23
 
24
+ from dotenv import load_dotenv
25
+ # 嘗試加載 .env 文件
26
+ if os.path.exists('.env'):
27
+ load_dotenv()
28
+
29
  # 設置基本日誌配置
30
  logging.basicConfig(level=logging.INFO)
31
  logger = logging.getLogger(__name__)
 
86
  def clean_filename(filename):
87
  return ''.join(c for c in filename if c.isalnum() or c in (' ', '.', '_')).rstrip()
88
 
89
+ def download_audio(youtube_url, save_directory, q, ydl_opts):
90
  send_sse_message(q, {"status": "開始下載 YouTube 音頻..."})
91
+ unique_id = str(uuid.uuid4())[:8]
92
  output_filename = f"audio_{unique_id}"
93
  output_path = os.path.join(save_directory, output_filename)
94
 
95
+ ydl_opts.update({
 
 
 
 
 
 
 
96
  'outtmpl': output_path + ".%(ext)s",
97
+ })
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  try:
100
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
 
302
  logger.info(f"開始處理視頻: {url_or_path}")
303
  save_directory = config['save_directory']
304
 
305
+ # 處理 YouTube cookies
306
+ cookies_content = os.environ.get('YOUTUBE_COOKIES')
307
+ cookies_file = os.path.join(os.path.dirname(__file__), 'youtube.com_cookies.txt')
308
+ if cookies_content:
309
+ cookies_content = cookies_content.strip('"').replace('\\n', '\n').replace('\\t', '\t')
310
+ with open(cookies_file, 'w') as f:
311
+ f.write(cookies_content)
312
+ logger.info("已創建 YouTube cookies 文件")
313
+ else:
314
+ logger.warning("未找到 YouTube cookies 環境變量")
315
+
316
  processed_description = ""
317
  if url_or_path.startswith('http'):
318
  logger.info("檢測到 YouTube URL,開始獲取視頻信息")
 
327
  'outtmpl': os.path.join(save_directory, 'audio_%(id)s.%(ext)s'),
328
  'quiet': True,
329
  'no_warnings': True,
 
330
  'ignoreerrors': True,
 
331
  'logtostderr': False,
332
  'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
333
  'socket_timeout': 30,
 
336
  'extract_flat': 'in_playlist',
337
  'youtube_include_dash_manifest': False,
338
  'source_address': '0.0.0.0',
339
+ 'cookiefile': cookies_file, # 添加 cookies 文件
340
  }
341
 
342
  try:
 
360
  processed_description = process_youtube_description(raw_description)
361
 
362
  logger.info("開始下載 YouTube 音頻")
363
+ audio_path, video_title = download_audio(url_or_path, save_directory, q, ydl_opts)
364
  except yt_dlp.utils.DownloadError as e:
365
  error_message = str(e)
366
  if "Sign in to confirm you're not a bot" in error_message:
 
373
  send_sse_message(q, {"status": f"錯誤:{str(e)}"})
374
  logger.error(f"值錯誤: {str(e)}")
375
  raise
376
+
377
  else:
378
+ # 本地文件處理邏輯保持不變
379
  logger.info("檢測到本地文件路徑,開始處理本地視頻")
380
  audio_path, video_title = process_local_video(url_or_path, save_directory, q)
381
  processed_description = local_video_description if local_video_description else "這是一個本地視頻文件,用戶沒有提供視頻描述。"
382
 
383
+ # 剩餘的處理邏輯保持不變
384
  if not audio_path or not os.path.exists(audio_path):
385
  raise FileNotFoundError(f"音頻文件不存在: {audio_path}")
386
 
 
450
  except Exception as e:
451
  logger.exception("處理視頻時發生錯誤")
452
  send_sse_message(q, {"status": f"錯誤: {str(e)}"})
453
+ finally:
454
+ # 處理完成後刪除 cookies 文件
455
+ if os.path.exists(cookies_file):
456
+ os.remove(cookies_file)
457
+ logger.info("已刪除 YouTube cookies 文件")
458
 
459
 
460
  # 在全局變量部分添加: