Spaces:
Paused
Paused
| import os | |
| import requests | |
| from datetime import datetime, timedelta | |
| from pytube import YouTube | |
| from moviepy.editor import VideoFileClip | |
| from tqdm import tqdm | |
| import gradio as gr | |
| def download_youtube(url, nama_channel, new_name, extension): | |
| response = requests.get(url, stream=True) | |
| file_name = new_name + "." + extension | |
| download = f"/home/user/app/Hasil Download/Youtube/{nama_channel}" | |
| if not os.path.exists(download): | |
| os.makedirs(download) | |
| filename = f"{download}/{file_name}" | |
| with open(filename, 'wb') as file: | |
| total_size = int(response.headers.get("Content-Length", 0)) | |
| progress_bar = tqdm(total=total_size, unit="B", unit_scale=True, ncols=80) | |
| for chunk in response.iter_content(chunk_size=1024): | |
| if chunk: | |
| file.write(chunk) | |
| progress_bar.update(len(chunk)) | |
| progress_bar.close() | |
| print("") | |
| return filename | |
| def format_number(number): | |
| if number < 1000: | |
| return str(number) | |
| elif number < 1000000: | |
| return f"{round(number / 1000)} ribu" | |
| elif number < 1000000000: | |
| return f"{round(number / 1000000)} juta" | |
| else: | |
| return f"{round(number / 1000000000)} miliar" | |
| def cut_video(link, resolusi_input, start_time_str, end_time_str): | |
| yt = YouTube(link) | |
| nama_channel = yt.author | |
| judul_video = yt.title.replace('/', ' ') | |
| tanggal_upload = yt.publish_date.strftime("%-d %B %Y") | |
| jumlah_viewer = format_number(yt.views) | |
| selisih_hari = (datetime.now() - yt.publish_date).days | |
| rata2_viewer_per_hari = format_number(int(yt.views if selisih_hari < 1 else yt.views / selisih_hari)) | |
| durasi_video = str(timedelta(seconds=yt.length)) | |
| print("Nama Channel:", nama_channel) | |
| print("Judul Video:", judul_video) | |
| print("Tanggal Upload:", tanggal_upload) | |
| print("Jumlah Viewer:", jumlah_viewer) | |
| print("Rata-rata Viewer per Hari:", rata2_viewer_per_hari) | |
| print("Durasi Video:", durasi_video) | |
| resolusi_tersedia = [stream.resolution for stream in yt.streams.filter(progressive=True)] | |
| print("Resolusi yang tersedia:", ", ".join(resolusi_tersedia)) | |
| resolusi = resolusi_input + "p" | |
| stream = yt.streams.filter(progressive=True, resolution=resolusi).first() | |
| if stream is None: | |
| print("Maaf resolusi yang anda masukkan tidak ada") | |
| return None | |
| else: | |
| filename = download_youtube(stream.url, nama_channel, judul_video, 'mp4') | |
| start_time_parts = start_time_str.split(':') | |
| end_time_parts = end_time_str.split(':') | |
| start_time_seconds = int(start_time_parts[0]) * 3600 + int(start_time_parts[1]) * 60 + float(start_time_parts[2]) | |
| end_time_seconds = int(end_time_parts[0]) * 3600 + int(end_time_parts[1]) * 60 + float(end_time_parts[2]) | |
| potong = "/home/user/app/Hasil Potong" | |
| if not os.path.exists(potong): | |
| os.makedirs(potong) | |
| output_file_path = f'{potong}/{judul_video}.mp4' | |
| with VideoFileClip(filename) as video: | |
| subclip = video.subclip(start_time_seconds, end_time_seconds) | |
| subclip.write_videofile(output_file_path) | |
| return output_file_path | |
| iface = gr.Interface( | |
| fn=cut_video, | |
| inputs=[ | |
| gr.inputs.Textbox(lines=1, label="Link Video"), | |
| gr.inputs.Textbox(lines=1, label="Resolusi"), | |
| gr.inputs.Textbox(lines=1, label="Start Time (HH:MM:SS.MS)"), | |
| gr.inputs.Textbox(lines=1, label="End Time (HH:MM:SS.MS)"), | |
| ], | |
| outputs=gr.outputs.File(label="Download Cut Video"), | |
| title="YouTube Video Cutter", | |
| description="Potong dan download sebagian video YouTube.", | |
| ) | |
| iface.launch() | |