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