salomonsky commited on
Commit
ba5897d
·
1 Parent(s): 36c149b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -32
app.py CHANGED
@@ -1,22 +1,16 @@
1
  import os
 
 
 
 
2
  import streamlit as st
3
  from streamlit_option_menu import option_menu
4
  from youtube import youtube, download_youtube
5
  from others import *
6
- import random
7
- import base64
8
- import zipfile
9
- from io import BytesIO
10
  from moviepy.video.io.VideoFileClip import VideoFileClip
11
- from moviepy.editor import vfx
12
-
13
- def session(info_list):
14
- st.video(info_list[1])
15
- st.text(f"Video Information: {info_list[0]}")
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, aspect_ratio='16:9'):
20
  clip = VideoFileClip(video_file)
21
 
22
  total_duration = clip.duration
@@ -28,10 +22,11 @@ def random_cuts(video_file, title, num_cuts=1, duration=60, speed_factor=1.0, mu
28
  cuts.append((start_time, end_time))
29
 
30
  output_files = []
31
- prev_end_time = 0 # Para evitar repeticiones de tiempo de corte
 
32
  for i, (start_time, end_time) in enumerate(cuts):
33
  if start_time == prev_end_time:
34
- continue # Evitar repeticiones de tiempo de corte
35
 
36
  output_file = f"{title}_cut_{i+1}.mp4"
37
  clipped_clip = clip.subclip(start_time, end_time)
@@ -41,16 +36,16 @@ def random_cuts(video_file, title, num_cuts=1, duration=60, speed_factor=1.0, mu
41
 
42
  clipped_clip = clipped_clip.fx(vfx.speedx, speed_factor)
43
 
44
- if aspect_ratio == '9:16':
45
-
46
  original_width, original_height = clip.size
47
- new_height = int(original_width * (9 / 16))
48
 
49
- top_margin = (original_height - new_height) // 2
50
- bottom_margin = original_height - top_margin
51
- clipped_clip = clipped_clip.crop(y1=top_margin, y2=bottom_margin)
52
 
53
- clipped_clip = clipped_clip.resize(width=new_height, height=original_width)
 
54
 
55
  with st.spinner(f'Procesando video {i+1} de {num_cuts}...'):
56
  clipped_clip.write_videofile(output_file, codec="libx264", audio_codec="aac", verbose=False)
@@ -60,15 +55,9 @@ def random_cuts(video_file, title, num_cuts=1, duration=60, speed_factor=1.0, mu
60
 
61
  return output_files, cuts
62
 
63
- opciones = ['YouTube']
64
-
65
- with st.sidebar:
66
- seleccionado = option_menu("Descargador", opciones, icons=['play'], menu_icon="cast", default_index=0)
67
-
68
- funciones = [youtube]
69
-
70
  def download_all_button(cut_files):
71
  zip_buffer = BytesIO()
 
72
  with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
73
  for cut_file in cut_files:
74
  zip_file.write(cut_file, os.path.basename(cut_file))
@@ -79,6 +68,13 @@ def download_all_button(cut_files):
79
  href = f'<a href="data:application/zip;base64,{b64}" download="{zip_filename}">Descargar Todos</a>'
80
  st.markdown(href, unsafe_allow_html=True)
81
 
 
 
 
 
 
 
 
82
  if seleccionado:
83
  indice = opciones.index(seleccionado)
84
  funcion = funciones[indice]
@@ -93,13 +89,12 @@ if seleccionado:
93
  duration_slider = st.slider('Seleccionar duración de los cortes', 10, 900, 60)
94
  speed_factor = st.slider('Seleccionar velocidad del video', -2.0, 3.0, 1.0)
95
  mute_audio = st.checkbox('Eliminar sonido de los videos cortados')
96
- aspect_ratio = st.radio('Seleccionar Formato de Aspecto:', ['16:9', '9:16'], 1)
97
 
98
  if st.button(f"Descargar y Cortar {seleccionado}"):
99
  video_file, titulo_video, video_info, thumbnail_file = funcion(video_link)
100
 
101
- cut_height = 1280 if aspect_ratio == '9:16' else None
102
- cut_files, cuts = random_cuts(video_file, titulo_video, num_cuts=num_cuts, duration=duration_slider, speed_factor=speed_factor, mute_audio=mute_audio, aspect_ratio=aspect_ratio)
103
 
104
  for i, cut_file in enumerate(cut_files):
105
  file_size = os.path.getsize(cut_file)
@@ -114,4 +109,4 @@ if seleccionado:
114
  file_size = os.path.getsize(video_file)
115
  info_list = [titulo_video, video_file, thumbnail_file]
116
  download_all_button([video_file])
117
- session(info_list)
 
1
  import os
2
+ from io import BytesIO
3
+ import zipfile
4
+ import random
5
+ import base64
6
  import streamlit as st
7
  from streamlit_option_menu import option_menu
8
  from youtube import youtube, download_youtube
9
  from others import *
 
 
 
 
10
  from moviepy.video.io.VideoFileClip import VideoFileClip
11
+ from moviepy.editor import vfx
 
 
 
 
 
 
12
 
13
+ def random_cuts(video_file, title, num_cuts=1, duration=60, speed_factor=1.0, mute_audio=False, use_9_16=False):
14
  clip = VideoFileClip(video_file)
15
 
16
  total_duration = clip.duration
 
22
  cuts.append((start_time, end_time))
23
 
24
  output_files = []
25
+ prev_end_time = 0
26
+
27
  for i, (start_time, end_time) in enumerate(cuts):
28
  if start_time == prev_end_time:
29
+ continue
30
 
31
  output_file = f"{title}_cut_{i+1}.mp4"
32
  clipped_clip = clip.subclip(start_time, end_time)
 
36
 
37
  clipped_clip = clipped_clip.fx(vfx.speedx, speed_factor)
38
 
39
+ if use_9_16:
 
40
  original_width, original_height = clip.size
41
+ new_width = int(original_height * (9 / 16))
42
 
43
+ left_margin = (original_width - new_width) // 2
44
+ right_margin = original_width - left_margin
45
+ clipped_clip = clipped_clip.crop(x1=left_margin, x2=right_margin)
46
 
47
+ # Cambio en las dimensiones para la relación de aspecto 9:16
48
+ clipped_clip = clipped_clip.resize(width=new_width, height=original_height)
49
 
50
  with st.spinner(f'Procesando video {i+1} de {num_cuts}...'):
51
  clipped_clip.write_videofile(output_file, codec="libx264", audio_codec="aac", verbose=False)
 
55
 
56
  return output_files, cuts
57
 
 
 
 
 
 
 
 
58
  def download_all_button(cut_files):
59
  zip_buffer = BytesIO()
60
+
61
  with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
62
  for cut_file in cut_files:
63
  zip_file.write(cut_file, os.path.basename(cut_file))
 
68
  href = f'<a href="data:application/zip;base64,{b64}" download="{zip_filename}">Descargar Todos</a>'
69
  st.markdown(href, unsafe_allow_html=True)
70
 
71
+ opciones = ['YouTube']
72
+
73
+ with st.sidebar:
74
+ seleccionado = option_menu("Descargador", opciones, icons=['play'], menu_icon="cast", default_index=0)
75
+
76
+ funciones = [youtube]
77
+
78
  if seleccionado:
79
  indice = opciones.index(seleccionado)
80
  funcion = funciones[indice]
 
89
  duration_slider = st.slider('Seleccionar duración de los cortes', 10, 900, 60)
90
  speed_factor = st.slider('Seleccionar velocidad del video', -2.0, 3.0, 1.0)
91
  mute_audio = st.checkbox('Eliminar sonido de los videos cortados')
92
+ use_9_16 = st.checkbox('Proporción 9:16')
93
 
94
  if st.button(f"Descargar y Cortar {seleccionado}"):
95
  video_file, titulo_video, video_info, thumbnail_file = funcion(video_link)
96
 
97
+ cut_files, cuts = random_cuts(video_file, titulo_video, num_cuts=num_cuts, duration=duration_slider, speed_factor=speed_factor, mute_audio=mute_audio, use_9_16=use_9_16)
 
98
 
99
  for i, cut_file in enumerate(cut_files):
100
  file_size = os.path.getsize(cut_file)
 
109
  file_size = os.path.getsize(video_file)
110
  info_list = [titulo_video, video_file, thumbnail_file]
111
  download_all_button([video_file])
112
+ session(info_list)