GilbertClaus commited on
Commit
504bb98
·
1 Parent(s): b6af08b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -53
app.py CHANGED
@@ -1,11 +1,11 @@
1
  import os
 
2
  from datetime import datetime, timedelta
3
  from pytube import YouTube
4
  from moviepy.editor import VideoFileClip
5
  from tqdm import tqdm
6
  import gradio as gr
7
 
8
- # Function to download YouTube video
9
  def download_youtube(url, nama_channel, new_name, extension):
10
  response = requests.get(url, stream=True)
11
  file_name = new_name + "." + extension
@@ -27,34 +27,7 @@ def download_youtube(url, nama_channel, new_name, extension):
27
  progress_bar.close()
28
  print("")
29
 
30
- return f'File berhasil diunduh dan disimpan di direktori:\n\n{filename}', filename
31
-
32
- # Function to cut and save the video
33
- def cut_video(link, start_time_str, end_time_str):
34
- yt = YouTube(link)
35
- stream = yt.streams.filter(progressive=True, resolution='720p').first()
36
-
37
- if stream is None:
38
- return "Maaf, resolusi yang Anda pilih tidak tersedia."
39
-
40
- nama_channel = yt.author
41
- judul_video = yt.title.replace('/', ' ')
42
- filename = download_youtube(stream.url, nama_channel, judul_video, 'mp4')
43
-
44
- start_time_parts = start_time_str.split(':')
45
- end_time_parts = end_time_str.split(':')
46
- start_time_seconds = int(start_time_parts[0]) * 3600 + int(start_time_parts[1]) * 60 + float(start_time_parts[2])
47
- end_time_seconds = int(end_time_parts[0]) * 3600 + int(end_time_parts[1]) * 60 + float(end_time_parts[2])
48
- potong = "/content/Hasil Potong"
49
- if not os.path.exists(potong):
50
- os.makedirs(potong)
51
- output_file_path = f'{potong}/{judul_video}.mp4'
52
-
53
- with VideoFileClip(filename) as video:
54
- subclip = video.subclip(start_time_seconds, end_time_seconds)
55
- subclip.write_videofile(output_file_path)
56
-
57
- return output_file_path
58
 
59
  def format_number(number):
60
  if number < 1000:
@@ -65,40 +38,63 @@ def format_number(number):
65
  return f"{round(number / 1000000)} juta"
66
  else:
67
  return f"{round(number / 1000000000)} miliar"
 
 
 
68
 
69
- link = gr.inputs.Textbox(lines=1, label="Input Link Youtube")
70
- yt = YouTube(link)
71
-
72
- nama_channel = yt.author
73
- judul_video = yt.title.replace('/', ' ')
74
- tanggal_upload = yt.publish_date.strftime("%-d %B %Y")
75
- jumlah_viewer = format_number(yt.views)
76
- selisih_hari = (datetime.now() - yt.publish_date).days
77
- rata2_viewer_per_hari = format_number(int(yt.views if selisih_hari < 1 else yt.views / selisih_hari))
78
- durasi_video = str(timedelta(seconds=yt.length))
79
 
80
- print("Nama Channel:", nama_channel)
81
- print("Judul Video:", judul_video)
82
- print("Tanggal Upload:", tanggal_upload)
83
- print("Jumlah Viewer:", jumlah_viewer)
84
- print("Rata-rata Viewer per Hari:", rata2_viewer_per_hari)
85
- print("Durasi Video:", durasi_video)
86
 
87
- resolusi_tersedia = [stream.resolution for stream in yt.streams.filter(progressive=True)]
88
- print("Resolusi yang tersedia:", ", ".join(resolusi_tersedia))
89
 
90
- resolusi_input = gr.inputs.Dropdown(choices=resolusi_tersedia, label="Pilih Resolusi")
91
- start_time_input = gr.inputs.Textbox(lines=1, label="Start Time (HH:MM:SS.MS)")
92
- end_time_input = gr.inputs.Textbox(lines=1, label="End Time (HH:MM:SS.MS)")
93
 
94
- cut_button = gr.outputs.Button(label="Cut Video")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  iface = gr.Interface(
97
  fn=cut_video,
98
- inputs=[link, start_time_input, end_time_input, cut_button],
 
 
 
 
 
99
  outputs=gr.outputs.File(label="Download Cut Video"),
100
  title="YouTube Video Cutter",
101
- description="Cut and download portions of YouTube videos.",
102
  )
103
 
104
  iface.launch()
 
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
 
27
  progress_bar.close()
28
  print("")
29
 
30
+ return filename
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  def format_number(number):
33
  if number < 1000:
 
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
 
45
+ nama_channel = yt.author
46
+ judul_video = yt.title.replace('/', ' ')
47
+ tanggal_upload = yt.publish_date.strftime("%-d %B %Y")
48
+ jumlah_viewer = format_number(yt.views)
49
+ selisih_hari = (datetime.now() - yt.publish_date).days
50
+ rata2_viewer_per_hari = format_number(int(yt.views if selisih_hari < 1 else yt.views / selisih_hari))
51
+ durasi_video = str(timedelta(seconds=yt.length))
 
 
 
52
 
53
+ print("Nama Channel:", nama_channel)
54
+ print("Judul Video:", judul_video)
55
+ print("Tanggal Upload:", tanggal_upload)
56
+ print("Jumlah Viewer:", jumlah_viewer)
57
+ print("Rata-rata Viewer per Hari:", rata2_viewer_per_hari)
58
+ print("Durasi Video:", durasi_video)
59
 
60
+ resolusi_tersedia = [stream.resolution for stream in yt.streams.filter(progressive=True)]
61
+ print("Resolusi yang tersedia:", ", ".join(resolusi_tersedia))
62
 
63
+ resolusi = resolusi_input + "p"
64
+ stream = yt.streams.filter(progressive=True, resolution=resolusi).first()
 
65
 
66
+ if stream is None:
67
+ print("Maaf resolusi yang anda masukkan tidak ada")
68
+ return None
69
+ else:
70
+ filename = download_youtube(stream.url, nama_channel, judul_video, 'mp4')
71
+
72
+ start_time_parts = start_time_str.split(':')
73
+ end_time_parts = end_time_str.split(':')
74
+ start_time_seconds = int(start_time_parts[0]) * 3600 + int(start_time_parts[1]) * 60 + float(start_time_parts[2])
75
+ end_time_seconds = int(end_time_parts[0]) * 3600 + int(end_time_parts[1]) * 60 + float(end_time_parts[2])
76
+ potong = "Hasil Potong" # Ganti dengan path yang sesuai di server Hugging Face
77
+ if not os.path.exists(potong):
78
+ os.makedirs(potong)
79
+ output_file_path = f'{potong}/{judul_video}.mp4'
80
+
81
+ with VideoFileClip(filename) as video:
82
+ subclip = video.subclip(start_time_seconds, end_time_seconds)
83
+ subclip.write_videofile(output_file_path)
84
+
85
+ return output_file_path
86
 
87
  iface = gr.Interface(
88
  fn=cut_video,
89
+ inputs=[
90
+ gr.inputs.Textbox(lines=1, label="Link Video"),
91
+ gr.inputs.Dropdown(choices=resolusi_tersedia, label="Resolusi"),
92
+ gr.inputs.Textbox(lines=1, label="Start Time (HH:MM:SS.MS)"),
93
+ gr.inputs.Textbox(lines=1, label="End Time (HH:MM:SS.MS)"),
94
+ ],
95
  outputs=gr.outputs.File(label="Download Cut Video"),
96
  title="YouTube Video Cutter",
97
+ description="Potong dan download sebagian video YouTube.",
98
  )
99
 
100
  iface.launch()