JUNGU commited on
Commit
1b8cfc0
·
verified ·
1 Parent(s): 728322d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -2
app.py CHANGED
@@ -1,6 +1,12 @@
1
  import gradio as gr
2
  import yt_dlp as youtube_dl
3
  import os
 
 
 
 
 
 
4
 
5
  def download_youtube_video(youtube_url):
6
  """Downloads a YouTube video using yt_dlp and returns the downloaded file path."""
@@ -16,10 +22,10 @@ def download_youtube_video(youtube_url):
16
 
17
  with youtube_dl.YoutubeDL(ydl_opts) as ydl:
18
  result = ydl.extract_info(youtube_url, download=True)
19
- video_title = result.get('title', None)
20
  video_ext = 'mp4' # We're forcing mp4 format
21
  downloaded_file = f"{video_title}.{video_ext}"
22
- return os.path.abspath(downloaded_file)
23
 
24
  except Exception as e:
25
  print("An error occurred:", e)
 
1
  import gradio as gr
2
  import yt_dlp as youtube_dl
3
  import os
4
+ import re
5
+
6
+ def sanitize_filename(filename):
7
+ """Remove or replace characters that are unsafe for filenames."""
8
+ unsafe_chars = r'[<>:"/\\|?*\u0000-\u001F]'
9
+ return re.sub(unsafe_chars, '_', filename)
10
 
11
  def download_youtube_video(youtube_url):
12
  """Downloads a YouTube video using yt_dlp and returns the downloaded file path."""
 
22
 
23
  with youtube_dl.YoutubeDL(ydl_opts) as ydl:
24
  result = ydl.extract_info(youtube_url, download=True)
25
+ video_title = sanitize_filename(result.get('title', 'video'))
26
  video_ext = 'mp4' # We're forcing mp4 format
27
  downloaded_file = f"{video_title}.{video_ext}"
28
+ return downloaded_file
29
 
30
  except Exception as e:
31
  print("An error occurred:", e)