GiorgioV commited on
Commit
a119648
·
verified ·
1 Parent(s): ee2a7c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -43
app.py CHANGED
@@ -6,50 +6,54 @@ import os
6
  import ffmpeg
7
 
8
  def finalise_video(start_video):
9
- try:
10
- with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as video_tmpfile:
11
- if isinstance(start_video, str):
12
- shutil.copy(start_video, video_tmpfile.name)
13
- else:
14
- start_video.save(video_tmpfile.name, format="MP4")
15
- video_path = video_tmpfile.name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as audio_tmpfile:
18
- video_with_audio_path = audio_tmpfile.name
19
- cmd = [
20
- 'ffmpeg',
21
- '-f', 'lavfi',
22
- '-i', 'anullsrc=channel_layout=stereo:sample_rate=44100',
23
- '-i', video_path,
24
- '-c:v', 'copy',
25
- '-c:a', 'aac',
26
- '-shortest',
27
- '-y',
28
- video_with_audio_path
29
- ]
30
-
31
- subprocess.run(cmd, capture_output=True, check=True)
32
-
33
- with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as blur_tmpfile:
34
- blurred_video_path = blur_tmpfile.name
35
-
36
- cmd_blur = [
37
- 'ffmpeg',
38
- '-i', video_with_audio_path, # Используем видео с аудио как источник
39
- '-vf', 'gblur=sigma=25', # Гауссово размытие с sigma=5
40
- '-c:a', 'copy', # Копируем аудио без изменений
41
- '-y',
42
- blurred_video_path
43
- ]
44
-
45
- subprocess.run(cmd_blur, capture_output=True, check=True)
46
-
47
- os.unlink(video_path)
48
-
49
- return video_with_audio_path, blurred_video_path
50
- except Exception as e:
51
- print(f"Error adding audio: {e}")
52
- return
53
 
54
  with gr.Blocks() as demo:
55
  gr.Markdown("Finaliser")
@@ -63,5 +67,14 @@ with gr.Blocks() as demo:
63
 
64
  ui_inputs = [input_video_component]
65
  generate_button.click(fn=finalise_video, inputs=ui_inputs, outputs=[video_output_1, video_output_2])
 
 
 
 
 
 
 
 
 
66
  if __name__ == "__main__":
67
  demo.queue().launch(mcp_server=True)
 
6
  import ffmpeg
7
 
8
  def finalise_video(start_video):
9
+ if check_ffmpeg():
10
+ try:
11
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as video_tmpfile:
12
+ if isinstance(start_video, str):
13
+ shutil.copy(start_video, video_tmpfile.name)
14
+ else:
15
+ start_video.save(video_tmpfile.name, format="MP4")
16
+ video_path = video_tmpfile.name
17
+
18
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as audio_tmpfile:
19
+ video_with_audio_path = audio_tmpfile.name
20
+ cmd = [
21
+ 'ffmpeg',
22
+ '-f', 'lavfi',
23
+ '-i', 'anullsrc=channel_layout=stereo:sample_rate=44100',
24
+ '-i', video_path,
25
+ '-c:v', 'copy',
26
+ '-c:a', 'aac',
27
+ '-shortest',
28
+ '-y',
29
+ video_with_audio_path
30
+ ]
31
 
32
+ subprocess.run(cmd, capture_output=True, check=True)
33
+
34
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as blur_tmpfile:
35
+ blurred_video_path = blur_tmpfile.name
36
+
37
+ cmd_blur = [
38
+ 'ffmpeg',
39
+ '-i', video_with_audio_path, # Используем видео с аудио как источник
40
+ '-vf', 'gblur=sigma=25', # Гауссово размытие с sigma=5
41
+ '-c:a', 'copy', # Копируем аудио без изменений
42
+ '-y',
43
+ blurred_video_path
44
+ ]
45
+
46
+ subprocess.run(cmd_blur, capture_output=True, check=True)
47
+
48
+ os.unlink(video_path)
49
+
50
+ return video_with_audio_path, blurred_video_path
51
+ except Exception as e:
52
+ print(f"Error finaliser: {e}")
53
+ return
54
+ else:
55
+ print("FFmpeg not available, returning video without audio")
56
+ return video_path, video_path, current_seed
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  with gr.Blocks() as demo:
59
  gr.Markdown("Finaliser")
 
67
 
68
  ui_inputs = [input_video_component]
69
  generate_button.click(fn=finalise_video, inputs=ui_inputs, outputs=[video_output_1, video_output_2])
70
+
71
+ def check_ffmpeg():
72
+ try:
73
+ subprocess.run(['ffmpeg', '-version'], capture_output=True, check=True)
74
+ return True
75
+ except (subprocess.CalledProcessError, FileNotFoundError):
76
+ return False
77
+
78
+
79
  if __name__ == "__main__":
80
  demo.queue().launch(mcp_server=True)