GiorgioV commited on
Commit
b2e59c2
·
verified ·
1 Parent(s): 2fe3f26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -30
app.py CHANGED
@@ -248,36 +248,40 @@ def generate_video(
248
 
249
  export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
250
 
251
- try:
252
- # Команда ffmpeg для добавления тихого аудио
253
- cmd = [
254
- 'ffmpeg',
255
- '-f', 'lavfi',
256
- '-i', 'anullsrc=channel_layout=stereo:sample_rate=44100',
257
- '-i', video_no_audio_path,
258
- '-c:v', 'copy', # Копируем видео без перекодирования
259
- '-c:a', 'aac', # Кодируем аудио в AAC
260
- '-shortest', # Обрезаем по короткому потоку
261
- '-y', # Перезаписываем выходной файл
262
- video_with_audio_path
263
- ]
264
-
265
- # Запускаем ffmpeg
266
- result = subprocess.run(cmd, capture_output=True, text=True, check=True)
267
-
268
- # Удаляем временный файл без звука
269
- os.unlink(video_no_audio_path)
270
-
271
- return video_with_audio_path, current_seed
272
-
273
- except subprocess.CalledProcessError as e:
274
- print(f"FFmpeg error: {e}")
275
- print(f"FFmpeg stderr: {e.stderr}")
276
- # В случае ошибки возвращаем видео без звука
277
- return video_no_audio_path, current_seed
278
- except Exception as e:
279
- print(f"Unexpected error: {e}")
280
- return video_no_audio_path, current_seed
 
 
 
 
281
 
282
 
283
  with gr.Blocks() as demo:
@@ -329,5 +333,12 @@ with gr.Blocks() as demo:
329
  inputs=[input_image_component, prompt_input, steps_slider], outputs=[video_output, seed_input], fn=generate_video, cache_examples="lazy"
330
  )
331
 
 
 
 
 
 
 
 
332
  if __name__ == "__main__":
333
  demo.queue().launch(mcp_server=True)
 
248
 
249
  export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
250
 
251
+ if check_ffmpeg():
252
+ try:
253
+ # Создаем временный файл для видео с звуком
254
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as audio_tmpfile:
255
+ video_with_audio_path = audio_tmpfile.name
256
+
257
+ # Команда ffmpeg для добавления тихого аудио
258
+ cmd = [
259
+ 'ffmpeg',
260
+ '-f', 'lavfi',
261
+ '-i', 'anullsrc=channel_layout=stereo:sample_rate=44100',
262
+ '-i', video_path,
263
+ '-c:v', 'copy',
264
+ '-c:a', 'aac',
265
+ '-shortest',
266
+ '-y',
267
+ video_with_audio_path
268
+ ]
269
+
270
+ # Запускаем ffmpeg
271
+ subprocess.run(cmd, capture_output=True, check=True)
272
+
273
+ # Удаляем исходный файл без звука
274
+ os.unlink(video_path)
275
+
276
+ return video_with_audio_path, current_seed
277
+
278
+ except Exception as e:
279
+ print(f"Error adding audio: {e}")
280
+ # В случае ошибки возвращаем видео без звука
281
+ return video_path, current_seed
282
+ else:
283
+ print("FFmpeg not available, returning video without audio")
284
+ return video_path, current_seed
285
 
286
 
287
  with gr.Blocks() as demo:
 
333
  inputs=[input_image_component, prompt_input, steps_slider], outputs=[video_output, seed_input], fn=generate_video, cache_examples="lazy"
334
  )
335
 
336
+ def check_ffmpeg():
337
+ try:
338
+ subprocess.run(['ffmpeg', '-version'], capture_output=True, check=True)
339
+ return True
340
+ except (subprocess.CalledProcessError, FileNotFoundError):
341
+ return False
342
+
343
  if __name__ == "__main__":
344
  demo.queue().launch(mcp_server=True)