salomonsky commited on
Commit
99ba69c
·
1 Parent(s): ec90571

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -67
app.py CHANGED
@@ -9,6 +9,7 @@ from moviepy.editor import vfx
9
  from streamlit_option_menu import option_menu
10
  from youtube import youtube, download_youtube
11
  from moviepy.video.io.VideoFileClip import VideoFileClip
 
12
 
13
  def session(info_list):
14
  st.video(info_list[1])
@@ -16,10 +17,8 @@ def session(info_list):
16
  if len(info_list) == 5:
17
  st.text(f"Duration: {info_list[3]} - {info_list[4]} seconds")
18
 
19
- def random_cuts(video_file, title, num_cuts=1, duration=60, speed_factor=1.0, mute_audio=False, use_aspect_ratio=None):
20
- clip = VideoFileClip(video_file)
21
-
22
- total_duration = clip.duration
23
  cuts = []
24
 
25
  for _ in range(num_cuts):
@@ -35,36 +34,29 @@ def random_cuts(video_file, title, num_cuts=1, duration=60, speed_factor=1.0, mu
35
  continue
36
 
37
  output_file = f"{title}_cut_{i+1}.mp4"
38
- clipped_clip = clip.subclip(start_time, end_time)
39
-
40
- if mute_audio:
41
- clipped_clip = clipped_clip.set_audio(None)
42
-
43
- clipped_clip = clipped_clip.fx(vfx.speedx, speed_factor)
44
-
45
- if use_aspect_ratio:
46
- original_width, original_height = clip.size
47
-
48
- if use_aspect_ratio == '16:9':
49
- target_ratio = 16 / 9
50
- elif use_aspect_ratio == '9:16':
51
- target_ratio = 9 / 16
52
- elif use_aspect_ratio == '21:9':
53
- target_ratio = 21 / 9
54
- else:
55
- target_ratio = 16 / 9
56
-
57
  if use_aspect_ratio:
58
- original_width, original_height = clip.size
59
-
60
  if use_aspect_ratio == '21:9':
61
  target_ratio = 21 / 9
62
  else:
63
  target_ratio = 16 / 9 if use_aspect_ratio == '16:9' else 9 / 16
64
-
65
  target_width = int(original_height * target_ratio)
66
  target_height = original_height
67
-
68
  if use_aspect_ratio == '21:9':
69
  target_height = int(original_width / target_ratio)
70
  margin = (original_height - target_height) // 2
@@ -72,12 +64,12 @@ def random_cuts(video_file, title, num_cuts=1, duration=60, speed_factor=1.0, mu
72
  else:
73
  margin = (original_width - target_width) // 2
74
  clipped_clip = clipped_clip.crop(x1=margin, x2=original_width - margin)
75
-
76
- with st.spinner(f'Procesando video {i+1} de {num_cuts}...'):
77
- clipped_clip.write_videofile(output_file, codec="libx264", audio_codec="aac", verbose=False)
78
 
79
- output_files.append(output_file)
80
- prev_end_time = end_time
81
 
82
  return output_files, cuts
83
 
@@ -94,49 +86,42 @@ def download_all_button(cut_files):
94
  href = f'<a href="data:application/zip;base64,{b64}" download="{zip_filename}">Descargar Todos</a>'
95
  st.markdown(href, unsafe_allow_html=True)
96
 
97
- opciones = ['YouTube']
 
 
98
 
99
- with st.sidebar:
100
- seleccionado = option_menu("Descargador", opciones, icons=['play'], menu_icon="cast", default_index=0)
 
 
101
 
102
- funciones = [youtube]
103
 
104
- if seleccionado:
105
- indice = opciones.index(seleccionado)
106
- funcion = funciones[indice]
107
- st.title(f"{seleccionado} Descargador de Videos")
108
- st.write(f"Descarga y corta una parte aleatoria de {seleccionado}.")
109
 
110
- video_link = st.text_input("Enlace del Video", value='https://www.youtube.com/watch?v=urhsbKIF9pE')
111
- aspect_ratio_options = ['16:9', '9:16', '21:9']
112
- aspect_ratio_choice = st.selectbox('Seleccionar Proporción:', aspect_ratio_options, index=2)
113
- resolution_options = [144, 240, 360, 720]
114
- resolution_choice = st.selectbox('Seleccionar Resolución:', resolution_options, index=2)
 
 
 
 
 
 
115
 
116
- choice = st.radio('Seleccionar Proceso:', ['Cortar Video', 'Descargar Completo'], 1)
117
-
118
- if choice == 'Cortar Video':
119
- num_cuts = st.slider('Seleccionar número de cortes', 1, 10, 1)
120
- duration_minutes_slider = st.slider('Seleccionar duración de los cortes (minutos)', 1, 180, 1)
121
- duration_seconds = duration_minutes_slider * 60
122
- speed_factor = st.slider('Seleccionar velocidad del video', -2.0, 3.0, 1.0)
123
- mute_audio = st.checkbox('Eliminar sonido de los videos cortados')
124
-
125
- if st.button(f"Descargar y Cortar {seleccionado}"):
126
- video_file, titulo_video, video_info, thumbnail_file = funcion(video_link, resolusi_input=resolution_choice)
127
- cut_files, cuts = random_cuts(video_file, titulo_video, num_cuts=num_cuts, duration=duration_seconds, speed_factor=speed_factor, mute_audio=mute_audio, use_aspect_ratio=aspect_ratio_choice)
128
-
129
- for i, cut_file in enumerate(cut_files):
130
- file_size = os.path.getsize(cut_file)
131
- info_list = [f"{titulo_video}_cut_{i+1}", cut_file, thumbnail_file, cuts[i][0], cuts[i][1]]
132
- session(info_list)
133
-
134
- download_all_button(cut_files)
135
-
136
  else:
137
  if st.button(f"Descargar {seleccionado}"):
138
  video_file, titulo_video, video_info, thumbnail_file = funcion(video_link, resolusi_input=resolution_choice)
139
  file_size = os.path.getsize(video_file)
140
  info_list = [titulo_video, video_file, thumbnail_file]
141
  download_all_button([video_file])
142
- session(info_list)
 
 
 
 
 
9
  from streamlit_option_menu import option_menu
10
  from youtube import youtube, download_youtube
11
  from moviepy.video.io.VideoFileClip import VideoFileClip
12
+ import tempfile
13
 
14
  def session(info_list):
15
  st.video(info_list[1])
 
17
  if len(info_list) == 5:
18
  st.text(f"Duration: {info_list[3]} - {info_list[4]} seconds")
19
 
20
+ def random_cuts(video_clip, title, num_cuts=1, duration=60, speed_factor=1.0, mute_audio=False, use_aspect_ratio=None):
21
+ total_duration = video_clip.duration
 
 
22
  cuts = []
23
 
24
  for _ in range(num_cuts):
 
34
  continue
35
 
36
  output_file = f"{title}_cut_{i+1}.mp4"
37
+ temp_filename = None
38
+
39
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_file:
40
+ temp_filename = temp_file.name
41
+
42
+ clipped_clip = video_clip.subclip(start_time, end_time)
43
+
44
+ if mute_audio:
45
+ clipped_clip = clipped_clip.set_audio(None)
46
+
47
+ clipped_clip = clipped_clip.fx(vfx.speedx, speed_factor)
48
+
 
 
 
 
 
 
 
49
  if use_aspect_ratio:
50
+ original_width, original_height = video_clip.size
51
+
52
  if use_aspect_ratio == '21:9':
53
  target_ratio = 21 / 9
54
  else:
55
  target_ratio = 16 / 9 if use_aspect_ratio == '16:9' else 9 / 16
56
+
57
  target_width = int(original_height * target_ratio)
58
  target_height = original_height
59
+
60
  if use_aspect_ratio == '21:9':
61
  target_height = int(original_width / target_ratio)
62
  margin = (original_height - target_height) // 2
 
64
  else:
65
  margin = (original_width - target_width) // 2
66
  clipped_clip = clipped_clip.crop(x1=margin, x2=original_width - margin)
67
+
68
+ with st.spinner(f'Procesando video {i+1} de {num_cuts}...'):
69
+ clipped_clip.write_videofile(temp_filename, codec="libx264", audio_codec="aac", verbose=False)
70
 
71
+ output_files.append(temp_filename)
72
+ prev_end_time = end_time
73
 
74
  return output_files, cuts
75
 
 
86
  href = f'<a href="data:application/zip;base64,{b64}" download="{zip_filename}">Descargar Todos</a>'
87
  st.markdown(href, unsafe_allow_html=True)
88
 
89
+ st.title("Descargador de Videos")
90
+ video_link = st.text_input("Enlace del Video", value='')
91
+ video_input = st.file_uploader("Cargar Video", type=["mp4", "avi", "mov"])
92
 
93
+ aspect_ratio_options = ['16:9', '9:16', '21:9']
94
+ aspect_ratio_choice = st.selectbox('Seleccionar Proporción:', aspect_ratio_options, index=2)
95
+ resolution_options = [144, 240, 360, 720]
96
+ resolution_choice = st.selectbox('Seleccionar Resolución:', resolution_options, index=2)
97
 
98
+ choice = st.radio('Seleccionar Proceso:', ['Cortar Video', 'Descargar Completo'], 1)
99
 
100
+ if st.button("Procesar"):
101
+ if video_input is not None:
102
+ video_clip = VideoFileClip(video_input)
103
+ video_clip = video_clip.subclip(0, video_clip.duration) # Ajusta la duración si es necesario
 
104
 
105
+ if choice == 'Cortar Video':
106
+ num_cuts = st.slider('Seleccionar número de cortes', 1, 10, 1)
107
+ duration_minutes_slider = st.slider('Seleccionar duración de los cortes (minutos)', 1, 180, 1)
108
+ duration_seconds = duration_minutes_slider * 60
109
+ speed_factor = st.slider('Seleccionar velocidad del video', -2.0, 3.0, 1.0)
110
+ mute_audio = st.checkbox('Eliminar sonido de los videos cortados')
111
+
112
+ cuts = random_cuts(video_clip, "video_cortado", num_cuts=num_cuts, duration=duration_seconds, speed_factor=speed_factor, mute_audio=mute_audio, use_aspect_ratio=aspect_ratio_choice)
113
+
114
+ output_files, _ = cuts
115
+ download_all_button(output_files)
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  else:
118
  if st.button(f"Descargar {seleccionado}"):
119
  video_file, titulo_video, video_info, thumbnail_file = funcion(video_link, resolusi_input=resolution_choice)
120
  file_size = os.path.getsize(video_file)
121
  info_list = [titulo_video, video_file, thumbnail_file]
122
  download_all_button([video_file])
123
+ session(info_list)
124
+ for temp_filename in output_files:
125
+ os.remove(temp_filename)
126
+
127
+ st.success("Proceso completado y archivos temporales eliminados.")