File size: 2,017 Bytes
cec3e7a
7f5c593
cec3e7a
 
88457d4
 
 
 
 
e88502f
 
7f5c593
e88502f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cec3e7a
7f5c593
 
e88502f
800c436
7f5c593
e88502f
7f5c593
cec3e7a
7f5c593
1
2
3
4
5
6
7
8
9
10
11
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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()