Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -132,11 +132,6 @@ def create_subtitles(video_path): # Renamed from generate_subtitles
|
|
132 |
audio = AudioSegment.from_file(video_path)
|
133 |
audio = audio.set_channels(1).set_frame_rate(44100).set_sample_width(2)
|
134 |
audio.export(temp_audio.name, format='mp3', bitrate='128k')
|
135 |
-
|
136 |
-
# Check if the audio is silent
|
137 |
-
if audio.dBFS < -90: # Adjust the threshold as needed
|
138 |
-
os.unlink(temp_audio.name) # Clean up temp file
|
139 |
-
return "No audio detected in the video."
|
140 |
|
141 |
# Generate subtitles using Groq
|
142 |
with open(temp_audio.name, 'rb') as audio_file:
|
@@ -163,7 +158,7 @@ def create_subtitles(video_path): # Renamed from generate_subtitles
|
|
163 |
return vtt_content
|
164 |
|
165 |
|
166 |
-
def upload_video_to_hf(video_file, original_video_name, title, description, uploader, should_generate_subs=False, custom_thumbnail=None):
|
167 |
temp_dir = "temp"
|
168 |
if not os.path.exists(temp_dir):
|
169 |
os.makedirs(temp_dir)
|
@@ -193,26 +188,33 @@ def upload_video_to_hf(video_file, original_video_name, title, description, uplo
|
|
193 |
|
194 |
video_length = get_video_length(video_path)
|
195 |
|
|
|
|
|
|
|
|
|
196 |
# Generate and upload subtitles if requested and video is not too long
|
197 |
subtitle_location = ""
|
198 |
if should_generate_subs and video_length <= 3600: # 1 hour in seconds
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
|
|
|
|
|
|
216 |
|
217 |
# Upload video and thumbnail
|
218 |
video_location = f"videos/{video_name}"
|
|
|
132 |
audio = AudioSegment.from_file(video_path)
|
133 |
audio = audio.set_channels(1).set_frame_rate(44100).set_sample_width(2)
|
134 |
audio.export(temp_audio.name, format='mp3', bitrate='128k')
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
# Generate subtitles using Groq
|
137 |
with open(temp_audio.name, 'rb') as audio_file:
|
|
|
158 |
return vtt_content
|
159 |
|
160 |
|
161 |
+
def upload_video_to_hf(video_file, original_video_name, title, description, uploader, should_generate_subs=False, custom_thumbnail=None):
|
162 |
temp_dir = "temp"
|
163 |
if not os.path.exists(temp_dir):
|
164 |
os.makedirs(temp_dir)
|
|
|
188 |
|
189 |
video_length = get_video_length(video_path)
|
190 |
|
191 |
+
# Analyze audio level
|
192 |
+
audio = AudioSegment.from_file(video_path)
|
193 |
+
audio_dBFS = audio.dBFS
|
194 |
+
|
195 |
# Generate and upload subtitles if requested and video is not too long
|
196 |
subtitle_location = ""
|
197 |
if should_generate_subs and video_length <= 3600: # 1 hour in seconds
|
198 |
+
if audio_dBFS < -90:
|
199 |
+
subtitle_location = "" # Set to empty if audio is too quiet
|
200 |
+
else:
|
201 |
+
try:
|
202 |
+
vtt_content = create_subtitles(video_path) # Using renamed function
|
203 |
+
subtitle_name = f"{base_name}.vtt"
|
204 |
+
subtitle_path = os.path.join(temp_dir, subtitle_name)
|
205 |
+
|
206 |
+
with open(subtitle_path, 'w') as f:
|
207 |
+
f.write(vtt_content)
|
208 |
+
|
209 |
+
subtitle_location = f"subtitles/{subtitle_name}"
|
210 |
+
hf_api.upload_file(
|
211 |
+
path_or_fileobj=subtitle_path,
|
212 |
+
path_in_repo=subtitle_location,
|
213 |
+
repo_id="vericudebuget/ok4231",
|
214 |
+
repo_type="space",
|
215 |
+
)
|
216 |
+
except Exception as e:
|
217 |
+
st.warning(f"Failed to generate subtitles: {str(e)}")
|
218 |
|
219 |
# Upload video and thumbnail
|
220 |
video_location = f"videos/{video_name}"
|