YT-DLP / app.py
Lamara091's picture
Update app.py
e88502f verified
raw
history blame
2.02 kB
import gradio as gr
import yt_dlp
import os
from glob import glob
def convert_time_to_seconds(time_str):
hours, minutes, seconds = map(int, time_str.split(':'))
return hours * 3600 + minutes * 60 + seconds
def download_videos(text_input):
lines = text_input.strip().split('\n')
output_files = []
for line in lines:
parts = line.split(',')
if len(parts) == 3:
video_url = parts[0].strip()
start_time = parts[1].strip()
end_time = parts[2].strip()
start_seconds = convert_time_to_seconds(start_time)
end_seconds = convert_time_to_seconds(end_time)
options = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '128',
}],
'outtmpl': '%(title)s.%(ext)s',
'postprocessor_args': [
'-ss', str(start_seconds),
'-to', str(end_seconds)
]
}
with yt_dlp.YoutubeDL(options) as ydl:
ydl.download([video_url])
download_files = glob('*.mp3')
for file in download_files:
os.rename(file, os.path.join('downloaded', file))
output_files.append(os.path.join('downloaded', file))
if output_files:
return output_files
return "No MP3 file found."
interface = gr.Interface(
fn=download_videos,
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),
outputs=gr.File(label="Downloaded MP3 Files"),
title="Batch YouTube Video Downloader",
description="Enter each video's URL, start time, and end time on a new line, separated by commas."
)
interface.launch()