Spaces:
Sleeping
Sleeping
deepak191z
commited on
Commit
•
510ee15
1
Parent(s):
d4b22a3
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import zipfile
|
3 |
+
import subprocess
|
4 |
+
|
5 |
+
# Function to install missing packages
|
6 |
+
def install_packages():
|
7 |
+
try:
|
8 |
+
import yt_dlp
|
9 |
+
import moviepy
|
10 |
+
import gradio
|
11 |
+
except ImportError:
|
12 |
+
subprocess.check_call(["python", "-m", "pip", "install", "yt-dlp==2024.8.6", "moviepy", "gradio"])
|
13 |
+
|
14 |
+
install_packages()
|
15 |
+
|
16 |
+
from moviepy.editor import VideoFileClip
|
17 |
+
import yt_dlp
|
18 |
+
import gradio as gr
|
19 |
+
|
20 |
+
def download_video(url, output_dir="downloads"):
|
21 |
+
if not os.path.exists(output_dir):
|
22 |
+
os.makedirs(output_dir)
|
23 |
+
|
24 |
+
ydl_opts = {
|
25 |
+
'format': 'bestvideo+bestaudio/best',
|
26 |
+
'outtmpl': os.path.join(output_dir, 'video.mp4'),
|
27 |
+
'noplaylist': True,
|
28 |
+
'quiet': True
|
29 |
+
}
|
30 |
+
|
31 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
32 |
+
ydl.download([url])
|
33 |
+
|
34 |
+
def split_video(input_path, output_dir="segments", segment_length=60):
|
35 |
+
if not os.path.exists(output_dir):
|
36 |
+
os.makedirs(output_dir)
|
37 |
+
|
38 |
+
clip = VideoFileClip(input_path)
|
39 |
+
duration = int(clip.duration)
|
40 |
+
segments = []
|
41 |
+
|
42 |
+
for start in range(0, duration, segment_length):
|
43 |
+
end = min(start + segment_length, duration)
|
44 |
+
segment = clip.subclip(start, end)
|
45 |
+
segment_filename = os.path.join(output_dir, f"segment_{start // segment_length + 1}.mp4")
|
46 |
+
segment.write_videofile(segment_filename, codec="libx264")
|
47 |
+
segments.append(segment_filename)
|
48 |
+
|
49 |
+
return segments
|
50 |
+
|
51 |
+
def create_zip(files, zip_filename="video_segments.zip"):
|
52 |
+
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
53 |
+
for file in files:
|
54 |
+
zipf.write(file, os.path.basename(file))
|
55 |
+
return zip_filename
|
56 |
+
|
57 |
+
def process_video(url):
|
58 |
+
download_video(url)
|
59 |
+
|
60 |
+
input_path = "downloads/video.mp4"
|
61 |
+
segments_dir = "segments"
|
62 |
+
zip_filename = "video_segments.zip"
|
63 |
+
|
64 |
+
segments = split_video(input_path, segments_dir)
|
65 |
+
zip_file = create_zip(segments, zip_filename)
|
66 |
+
|
67 |
+
return zip_file
|
68 |
+
|
69 |
+
# Gradio Interface
|
70 |
+
def gradio_interface(url):
|
71 |
+
zip_file = process_video(url)
|
72 |
+
return zip_file
|
73 |
+
|
74 |
+
interface = gr.Interface(
|
75 |
+
fn=gradio_interface,
|
76 |
+
inputs="text",
|
77 |
+
outputs="file",
|
78 |
+
title="YouTube Video Downloader and Splitter",
|
79 |
+
description="Download a YouTube video at the highest quality, split it into 1-minute segments, and zip the segments."
|
80 |
+
)
|
81 |
+
|
82 |
+
interface.launch()
|