Update app.py
Browse files
app.py
CHANGED
|
@@ -7,50 +7,52 @@ def convert_time_to_seconds(time_str):
|
|
| 7 |
hours, minutes, seconds = map(int, time_str.split(':'))
|
| 8 |
return hours * 3600 + minutes * 60 + seconds
|
| 9 |
|
| 10 |
-
def download_videos(
|
|
|
|
| 11 |
output_files = []
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
'
|
| 25 |
-
'
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
'
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
interface = gr.Interface(
|
| 45 |
fn=download_videos,
|
| 46 |
-
inputs=gr.
|
| 47 |
-
gr.Textbox(label="YouTube Video URL", placeholder="Enter YouTube video URL here..."),
|
| 48 |
-
gr.Textbox(label="Start Time (HH:MM:SS)", placeholder="00:00:00"),
|
| 49 |
-
gr.Textbox(label="End Time (HH:MM:SS)", placeholder="00:05:00")
|
| 50 |
-
]),
|
| 51 |
outputs=gr.File(label="Downloaded MP3 Files"),
|
| 52 |
title="Batch YouTube Video Downloader",
|
| 53 |
-
description="
|
| 54 |
)
|
| 55 |
|
| 56 |
interface.launch()
|
|
|
|
| 7 |
hours, minutes, seconds = map(int, time_str.split(':'))
|
| 8 |
return hours * 3600 + minutes * 60 + seconds
|
| 9 |
|
| 10 |
+
def download_videos(text_input):
|
| 11 |
+
lines = text_input.strip().split('\n')
|
| 12 |
output_files = []
|
| 13 |
+
|
| 14 |
+
for line in lines:
|
| 15 |
+
parts = line.split(',')
|
| 16 |
+
if len(parts) == 3:
|
| 17 |
+
video_url = parts[0].strip()
|
| 18 |
+
start_time = parts[1].strip()
|
| 19 |
+
end_time = parts[2].strip()
|
| 20 |
+
|
| 21 |
+
start_seconds = convert_time_to_seconds(start_time)
|
| 22 |
+
end_seconds = convert_time_to_seconds(end_time)
|
| 23 |
+
|
| 24 |
+
options = {
|
| 25 |
+
'format': 'bestaudio/best',
|
| 26 |
+
'postprocessors': [{
|
| 27 |
+
'key': 'FFmpegExtractAudio',
|
| 28 |
+
'preferredcodec': 'mp3',
|
| 29 |
+
'preferredquality': '128',
|
| 30 |
+
}],
|
| 31 |
+
'outtmpl': '%(title)s.%(ext)s',
|
| 32 |
+
'postprocessor_args': [
|
| 33 |
+
'-ss', str(start_seconds),
|
| 34 |
+
'-to', str(end_seconds)
|
| 35 |
+
]
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
with yt_dlp.YoutubeDL(options) as ydl:
|
| 39 |
+
ydl.download([video_url])
|
| 40 |
+
|
| 41 |
+
download_files = glob('*.mp3')
|
| 42 |
+
for file in download_files:
|
| 43 |
+
os.rename(file, os.path.join('downloaded', file))
|
| 44 |
+
output_files.append(os.path.join('downloaded', file))
|
| 45 |
+
|
| 46 |
+
if output_files:
|
| 47 |
+
return output_files
|
| 48 |
+
return "No MP3 file found."
|
| 49 |
|
| 50 |
interface = gr.Interface(
|
| 51 |
fn=download_videos,
|
| 52 |
+
inputs=gr.Textbox(label="Enter video URL, start time, end time per line (comma separated)", placeholder="URL, Start Time (HH:MM:SS), End Time (HH:MM:SS)\nExample:\nhttps://youtu.be/xxx, 00:00:00, 00:05:00", lines=10),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
outputs=gr.File(label="Downloaded MP3 Files"),
|
| 54 |
title="Batch YouTube Video Downloader",
|
| 55 |
+
description="Enter each video's URL, start time, and end time on a new line, separated by commas."
|
| 56 |
)
|
| 57 |
|
| 58 |
interface.launch()
|