Spaces:
Paused
Paused
Commit ·
c7bd87f
1
Parent(s): e7fdf55
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,5 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import requests
|
| 3 |
-
from datetime import datetime, timedelta
|
| 4 |
-
from pytube import YouTube
|
| 5 |
-
from moviepy.editor import VideoFileClip
|
| 6 |
-
from tqdm import tqdm
|
| 7 |
import gradio as gr
|
| 8 |
-
|
| 9 |
-
def download_youtube(url, nama_channel, new_name, extension):
|
| 10 |
-
response = requests.get(url, stream=True)
|
| 11 |
-
file_name = new_name + "." + extension
|
| 12 |
-
|
| 13 |
-
download = f"/home/user/app/Hasil Download/Youtube/{nama_channel}"
|
| 14 |
-
if not os.path.exists(download):
|
| 15 |
-
os.makedirs(download)
|
| 16 |
-
|
| 17 |
-
filename = f"{download}/{file_name}"
|
| 18 |
-
with open(filename, 'wb') as file:
|
| 19 |
-
total_size = int(response.headers.get("Content-Length", 0))
|
| 20 |
-
progress_bar = tqdm(total=total_size, unit="B", unit_scale=True, ncols=80)
|
| 21 |
-
|
| 22 |
-
for chunk in response.iter_content(chunk_size=1024):
|
| 23 |
-
if chunk:
|
| 24 |
-
file.write(chunk)
|
| 25 |
-
progress_bar.update(len(chunk))
|
| 26 |
-
|
| 27 |
-
progress_bar.close()
|
| 28 |
-
print("")
|
| 29 |
-
|
| 30 |
-
return filename
|
| 31 |
-
|
| 32 |
-
def format_number(number):
|
| 33 |
-
if number < 1000:
|
| 34 |
-
return str(number)
|
| 35 |
-
elif number < 1000000:
|
| 36 |
-
return f"{round(number / 1000)} ribu"
|
| 37 |
-
elif number < 1000000000:
|
| 38 |
-
return f"{round(number / 1000000)} juta"
|
| 39 |
-
else:
|
| 40 |
-
return f"{round(number / 1000000000)} miliar"
|
| 41 |
-
|
| 42 |
-
def cut_video(link, resolusi_input, start_time_str, end_time_str):
|
| 43 |
-
yt = YouTube(link)
|
| 44 |
-
nama_channel = yt.author
|
| 45 |
-
judul_video = yt.title.replace('/', '-').replace('\\', '-')
|
| 46 |
-
tanggal_upload = yt.publish_date.strftime("%-d %B %Y")
|
| 47 |
-
jumlah_viewer = format_number(yt.views)
|
| 48 |
-
selisih_hari = (datetime.now() - yt.publish_date).days
|
| 49 |
-
rata2_viewer_per_hari = format_number(int(yt.views if selisih_hari < 1 else yt.views / selisih_hari))
|
| 50 |
-
durasi_video = str(timedelta(seconds=yt.length))
|
| 51 |
-
|
| 52 |
-
video_info = f"Nama Channel: {nama_channel}\n"
|
| 53 |
-
video_info += f"Judul Video: {judul_video}\n"
|
| 54 |
-
video_info += f"Tanggal Upload: {tanggal_upload}\n"
|
| 55 |
-
video_info += f"Jumlah Viewer: {jumlah_viewer}\n"
|
| 56 |
-
video_info += f"Rata-rata Viewer per Hari: {rata2_viewer_per_hari}\n"
|
| 57 |
-
video_info += f"Durasi Video: {durasi_video}\n"
|
| 58 |
-
|
| 59 |
-
resolusi_tersedia = [stream.resolution for stream in yt.streams.filter(progressive=True)]
|
| 60 |
-
video_info += f"Resolusi yang tersedia: {', '.join(resolusi_tersedia)}\n"
|
| 61 |
-
|
| 62 |
-
resolusi = resolusi_input + "p"
|
| 63 |
-
stream = yt.streams.filter(progressive=True, resolution=resolusi).first()
|
| 64 |
-
|
| 65 |
-
if stream is None:
|
| 66 |
-
print("Maaf resolusi yang anda masukkan tidak ada")
|
| 67 |
-
return None
|
| 68 |
-
else:
|
| 69 |
-
filename = download_youtube(stream.url, nama_channel, judul_video, 'mp4')
|
| 70 |
-
|
| 71 |
-
start_time_parts = start_time_str.split(':')
|
| 72 |
-
end_time_parts = end_time_str.split(':')
|
| 73 |
-
start_time_seconds = int(start_time_parts[0]) * 3600 + int(start_time_parts[1]) * 60 + float(start_time_parts[2])
|
| 74 |
-
end_time_seconds = int(end_time_parts[0]) * 3600 + int(end_time_parts[1]) * 60 + float(end_time_parts[2])
|
| 75 |
-
potong = "/home/user/app/Hasil Potong"
|
| 76 |
-
if not os.path.exists(potong):
|
| 77 |
-
os.makedirs(potong)
|
| 78 |
-
output_file_path = f'{potong}/{judul_video}.mp4'
|
| 79 |
-
|
| 80 |
-
with VideoFileClip(filename) as video:
|
| 81 |
-
subclip = video.subclip(start_time_seconds, end_time_seconds)
|
| 82 |
-
subclip.write_videofile(output_file_path)
|
| 83 |
-
|
| 84 |
-
return video_info, output_file_path, output_file_path
|
| 85 |
|
| 86 |
iface = gr.Interface(
|
| 87 |
fn=cut_video,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from youtube import cut_video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
iface = gr.Interface(
|
| 5 |
fn=cut_video,
|