sudo-soldier commited on
Commit
05939d0
·
verified ·
1 Parent(s): 06224c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -4,7 +4,7 @@ import gradio as gr
4
  import re
5
  from pydub import AudioSegment
6
 
7
- # Ensure 'downloads' directory exists
8
  os.makedirs("downloads", exist_ok=True)
9
 
10
  def sanitize_filename(filename):
@@ -20,14 +20,14 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
20
  ydl_opts = {
21
  'format': 'm4a/bestaudio',
22
  'outtmpl': 'downloads/%(title)s.%(ext)s',
23
- 'cookiefile': 'cookies.txt' # Ensure this file exists if needed
24
  }
25
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
26
  info = ydl.extract_info(url, download=True)
27
  filename = ydl.prepare_filename(info)
28
  song_name = sanitize_filename(info['title'])
29
 
30
- # Ensure filename is correct
31
  if not filename.endswith(".m4a"):
32
  possible_file = f"downloads/{song_name}.m4a"
33
  if os.path.exists(possible_file):
@@ -39,12 +39,12 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
39
  filename = uploaded_audio
40
  song_name = sanitize_filename(os.path.splitext(os.path.basename(uploaded_audio))[0])
41
 
42
- # Validate file existence
43
  if not filename or not os.path.exists(filename):
44
  print(f"File not found: {filename}")
45
  return None, None
46
 
47
- # Process audio
48
  audio = AudioSegment.from_file(filename)
49
  start_ms, end_ms = int(start_time * 1000), int(end_time * 1000)
50
  start_ms = max(0, min(start_ms, len(audio)))
@@ -52,11 +52,11 @@ def process_youtube_or_audio(url, uploaded_audio, start_time, end_time):
52
 
53
  trimmed_audio = audio[start_ms:end_ms]
54
 
55
- # Save as MP3 (Android)
56
  mp3_filename = f"downloads/{song_name}.mp3"
57
  trimmed_audio.export(mp3_filename, format="mp3")
58
 
59
- # Save as M4R (iPhone)
60
  m4r_filename = f"downloads/{song_name}.m4r"
61
  trimmed_audio.export(m4r_filename, format="m4r")
62
 
 
4
  import re
5
  from pydub import AudioSegment
6
 
7
+
8
  os.makedirs("downloads", exist_ok=True)
9
 
10
  def sanitize_filename(filename):
 
20
  ydl_opts = {
21
  'format': 'm4a/bestaudio',
22
  'outtmpl': 'downloads/%(title)s.%(ext)s',
23
+ 'cookiefile': 'cookies.txt' #yt
24
  }
25
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
26
  info = ydl.extract_info(url, download=True)
27
  filename = ydl.prepare_filename(info)
28
  song_name = sanitize_filename(info['title'])
29
 
30
+
31
  if not filename.endswith(".m4a"):
32
  possible_file = f"downloads/{song_name}.m4a"
33
  if os.path.exists(possible_file):
 
39
  filename = uploaded_audio
40
  song_name = sanitize_filename(os.path.splitext(os.path.basename(uploaded_audio))[0])
41
 
42
+
43
  if not filename or not os.path.exists(filename):
44
  print(f"File not found: {filename}")
45
  return None, None
46
 
47
+
48
  audio = AudioSegment.from_file(filename)
49
  start_ms, end_ms = int(start_time * 1000), int(end_time * 1000)
50
  start_ms = max(0, min(start_ms, len(audio)))
 
52
 
53
  trimmed_audio = audio[start_ms:end_ms]
54
 
55
+
56
  mp3_filename = f"downloads/{song_name}.mp3"
57
  trimmed_audio.export(mp3_filename, format="mp3")
58
 
59
+
60
  m4r_filename = f"downloads/{song_name}.m4r"
61
  trimmed_audio.export(m4r_filename, format="m4r")
62