danilotpnta commited on
Commit
035bc5f
·
1 Parent(s): 1b65381

Force Headless Mode

Browse files
Files changed (2) hide show
  1. app.py +16 -10
  2. download_video.py +2 -13
app.py CHANGED
@@ -2,31 +2,34 @@ import whisper
2
  import gradio as gr
3
  import os
4
  import asyncio
5
-
6
  import subprocess
7
 
8
- # Ensure Playwright is installed and system dependencies are downloaded
9
- def install_playwright_deps():
10
  try:
11
  # Install Playwright via pip if not already installed
12
  subprocess.run(["pip", "install", "playwright"], check=True)
13
 
14
- # Install system dependencies for Playwright
15
- subprocess.run(["playwright", "install-deps"], check=True)
16
-
17
  # Install the Playwright browsers
18
  subprocess.run(["playwright", "install"], check=True)
19
 
20
- print("Playwright dependencies and browsers installed.")
21
  except subprocess.CalledProcessError as e:
22
  print(f"Error during Playwright setup: {e}")
23
  exit(1)
24
 
25
- # Call the function to install system dependencies and Playwright
26
- install_playwright_deps()
27
 
28
  from download_video import download_mp3_playwright
29
 
 
 
 
 
 
 
 
30
 
31
  # Function to download the audio, title, and thumbnail from YouTube
32
  async def download_video_info(url):
@@ -34,7 +37,10 @@ async def download_video_info(url):
34
  # Call the async function to download video and get title and thumbnail
35
  title, thumbnail_url = await download_mp3_playwright(url)
36
  audio_file = "downloaded_video.mp4" # Path to the downloaded audio
37
- return audio_file, title, thumbnail_url
 
 
 
38
  except Exception as e:
39
  return None, None, str(e)
40
 
 
2
  import gradio as gr
3
  import os
4
  import asyncio
 
5
  import subprocess
6
 
7
+ # Ensure Playwright is installed and browsers are downloaded
8
+ def install_playwright():
9
  try:
10
  # Install Playwright via pip if not already installed
11
  subprocess.run(["pip", "install", "playwright"], check=True)
12
 
 
 
 
13
  # Install the Playwright browsers
14
  subprocess.run(["playwright", "install"], check=True)
15
 
16
+ print("Playwright and browsers installed.")
17
  except subprocess.CalledProcessError as e:
18
  print(f"Error during Playwright setup: {e}")
19
  exit(1)
20
 
21
+ # Call the function to install Playwright
22
+ install_playwright()
23
 
24
  from download_video import download_mp3_playwright
25
 
26
+ # Function to convert MP4 to MP3 using FFmpeg
27
+ def convert_to_mp3(input_file, output_file):
28
+ command = ["ffmpeg", "-i", input_file, "-q:a", "0", "-map", "a", output_file]
29
+ try:
30
+ subprocess.run(command, check=True)
31
+ except subprocess.CalledProcessError as e:
32
+ print(f"Error converting {input_file} to {output_file}: {e}")
33
 
34
  # Function to download the audio, title, and thumbnail from YouTube
35
  async def download_video_info(url):
 
37
  # Call the async function to download video and get title and thumbnail
38
  title, thumbnail_url = await download_mp3_playwright(url)
39
  audio_file = "downloaded_video.mp4" # Path to the downloaded audio
40
+
41
+ # Convert MP4 to MP3 before passing to Whisper
42
+ convert_to_mp3(audio_file, "downloaded_audio.mp3")
43
+ return "downloaded_audio.mp3", title, thumbnail_url
44
  except Exception as e:
45
  return None, None, str(e)
46
 
download_video.py CHANGED
@@ -1,11 +1,9 @@
1
- import asyncio
2
  from playwright.async_api import async_playwright
3
- import requests
4
 
5
  async def download_mp3_playwright(youtube_url):
6
  async with async_playwright() as p:
7
  # Launch browser in headless mode
8
- browser = await p.chromium.launch(headless=True)
9
  page = await browser.new_page()
10
 
11
  # Open the YouTube video page
@@ -67,13 +65,4 @@ async def download_mp3_playwright(youtube_url):
67
  await browser.close()
68
 
69
  # Return the title and thumbnail for display
70
- return title, thumbnail_url
71
-
72
- # Call the async function
73
- async def main():
74
- youtube_url = "https://youtu.be/MAZyQ-38b8M?si=q0dai-wF6FQz6MGN"
75
- await download_mp3_playwright(youtube_url)
76
-
77
- # Run the asyncio loop
78
- if __name__ == "__main__":
79
- asyncio.run(main())
 
 
1
  from playwright.async_api import async_playwright
 
2
 
3
  async def download_mp3_playwright(youtube_url):
4
  async with async_playwright() as p:
5
  # Launch browser in headless mode
6
+ browser = await p.chromium.launch(headless=True, args=['--no-sandbox'])
7
  page = await browser.new_page()
8
 
9
  # Open the YouTube video page
 
65
  await browser.close()
66
 
67
  # Return the title and thumbnail for display
68
+ return title, thumbnail_url