GilbertClaus commited on
Commit
18cba5a
·
1 Parent(s): faf3708
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Function to download YouTube video
10
+ def download_youtube(url, nama_channel, new_name, extension):
11
+ response = requests.get(url, stream=True)
12
+ file_name = new_name + "." + extension
13
+
14
+ download = f"/content/Hasil Download/Youtube/{nama_channel}"
15
+ if not os.path.exists(download):
16
+ os.makedirs(download)
17
+
18
+ filename = f"{download}/{file_name}"
19
+ with open(filename, 'wb') as file:
20
+ total_size = int(response.headers.get("Content-Length", 0))
21
+ progress_bar = tqdm(total=total_size, unit="B", unit_scale=True, ncols=80)
22
+
23
+ for chunk in response.iter_content(chunk_size=1024):
24
+ if chunk:
25
+ file.write(chunk)
26
+ progress_bar.update(len(chunk))
27
+
28
+ progress_bar.close()
29
+ print("")
30
+
31
+ return f'File berhasil diunduh dan disimpan di direktori:\n\n{filename}', filename
32
+
33
+ # Function to cut and save the video
34
+ def cut_video(start_time_str, end_time_str, filename, judul_video):
35
+ start_time_parts = start_time_str.split(':')
36
+ end_time_parts = end_time_str.split(':')
37
+ start_time_seconds = int(start_time_parts[0]) * 3600 + int(start_time_parts[1]) * 60 + float(start_time_parts[2])
38
+ end_time_seconds = int(end_time_parts[0]) * 3600 + int(end_time_parts[1]) * 60 + float(end_time_parts[2])
39
+ potong = "/content/Hasil Potong"
40
+ if not os.path.exists(potong):
41
+ os.makedirs(potong)
42
+ output_file_path = f'{potong}/{judul_video}.mp4'
43
+
44
+ with VideoFileClip(filename) as video:
45
+ subclip = video.subclip(start_time_seconds, end_time_seconds)
46
+ subclip.write_videofile(output_file_path)
47
+
48
+ return output_file_path
49
+
50
+ def format_number(number):
51
+ if number < 1000:
52
+ return str(number)
53
+ elif number < 1000000:
54
+ return f"{round(number / 1000)} ribu"
55
+ elif number < 1000000000:
56
+ return f"{round(number / 1000000)} juta"
57
+ else:
58
+ return f"{round(number / 1000000000)} miliar"
59
+
60
+ def get_resolution(link):
61
+ yt = YouTube(link)
62
+ resolusi_tersedia = [stream.resolution for stream in yt.streams.filter(progressive=True)]
63
+ return resolusi_tersedia
64
+
65
+ iface = gr.Interface(
66
+ fn=cut_video,
67
+ inputs=[
68
+ gr.inputs.Textbox(lines=1, label="Start Time (HH:MM:SS.MS)"),
69
+ gr.inputs.Textbox(lines=1, label="End Time (HH:MM:SS.MS)"),
70
+ gr.inputs.Textbox(lines=1, label="Original Video File Path"),
71
+ gr.inputs.Textbox(lines=1, label="Video Title"),
72
+ ],
73
+ outputs=gr.outputs.File(label="Download Cut Video"),
74
+ title="YouTube Video Cutter",
75
+ description="Cut and download portions of YouTube videos.",
76
+ )
77
+
78
+ iface.launch()