Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yt_dlp
|
2 |
+
import gradio as gr
|
3 |
+
import subprocess
|
4 |
+
from uuid import uuid4
|
5 |
+
from tempfile import TemporaryDirectory
|
6 |
+
|
7 |
+
cachedir = ".cache"
|
8 |
+
|
9 |
+
|
10 |
+
def clip(yt_url, start_timestamp, duration):
|
11 |
+
start_timestamp = round(float(start_timestamp), 3)
|
12 |
+
duration = round(float(duration), 3)
|
13 |
+
|
14 |
+
with TemporaryDirectory() as tmpdir:
|
15 |
+
video_output_path = os.path.join(tmpdir, "video.mp4")
|
16 |
+
ydl_opts = {
|
17 |
+
"quiet": True,
|
18 |
+
"outtmpl": video_output_path,
|
19 |
+
"postprocessors": [{"key": "FFmpegVideoConvertor", "preferedformat": "mp4"}],
|
20 |
+
"cachedir": cachedir
|
21 |
+
}
|
22 |
+
if not os.path.exists(video_output_path):
|
23 |
+
try:
|
24 |
+
ydl_opts["format"] = "22"
|
25 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
26 |
+
ydl.download([video_url])
|
27 |
+
except:
|
28 |
+
ydl_opts["format"] = "bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]/best[height<=480[ext=mp4]/best"
|
29 |
+
sleep(5)
|
30 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
31 |
+
ydl.download([video_url])
|
32 |
+
clip_path = os.path.join(tmpdir, f"{uuid4()}.mp3")
|
33 |
+
subprocess.run(f'ffmpeg -ss {start_timestamp} -t {duration} -i "{video_output_path}" "{clip_path}"', shell=True)
|
34 |
+
return clip_path
|
35 |
+
|
36 |
+
|
37 |
+
url_link = gr.Textbox(label="YouTube URL")
|
38 |
+
start_timestamp = gr.Textbox(label="Clip start timestamp (in seconds)")
|
39 |
+
clip_length = gr.Textbox(label="Clip length (in seconds)")
|
40 |
+
|
41 |
+
interface = gr.Interface(clip, [url_link, start_timestamp, clip_length], "audio", title="YouTube Clipper")
|
42 |
+
|
43 |
+
if __name__=="__main__":
|
44 |
+
interface.queue().launch()
|