aiqcamp commited on
Commit
1cfe547
ยท
verified ยท
1 Parent(s): decba1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -20
app.py CHANGED
@@ -79,32 +79,56 @@ def video_to_audio(video_path: str, prompt: str, negative_prompt: str = "music",
79
  rng.seed()
80
  fm = FlowMatching(min_sigma=0, inference_mode='euler', num_steps=num_steps)
81
 
82
- # duration ํŒŒ๋ผ๋ฏธํ„ฐ ์ „๋‹ฌ ๋ฐฉ์‹ ์ˆ˜์ •
83
- video_info = load_video(video_path, static_duration=duration) # static_duration์œผ๋กœ ๋ณ€๊ฒฝ
84
 
 
 
 
 
85
  clip_frames = video_info.clip_frames
86
  sync_frames = video_info.sync_frames
87
  actual_duration = video_info.duration_sec
 
 
 
 
 
88
  clip_frames = clip_frames.unsqueeze(0)
89
  sync_frames = sync_frames.unsqueeze(0)
 
 
90
  seq_cfg.duration = actual_duration
91
  net.update_seq_lengths(seq_cfg.latent_seq_len, seq_cfg.clip_seq_len, seq_cfg.sync_seq_len)
92
 
 
93
  audios = generate(clip_frames,
94
- sync_frames, [prompt],
95
- negative_text=[negative_prompt],
96
- feature_utils=feature_utils,
97
- net=net,
98
- fm=fm,
99
- rng=rng,
100
- cfg_strength=cfg_strength)
 
 
 
 
 
 
101
  audio = audios.float().cpu()[0]
102
 
103
- video_save_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name
104
- make_video(video_info, video_save_path, audio, sampling_rate=seq_cfg.sampling_rate)
105
- logger.info(f'Saved video with audio to {video_save_path}')
106
- return video_save_path
107
 
 
 
 
 
 
 
 
108
  except Exception as e:
109
  logger.error(f"Error in video_to_audio: {str(e)}")
110
  return video_path # ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์›๋ณธ ๋น„๋””์˜ค ๋ฐ˜ํ™˜
@@ -321,6 +345,7 @@ def generate_video(image, prompt):
321
 
322
  # ์˜ค๋””์˜ค ์ฒ˜๋ฆฌ ์ถ”๊ฐ€
323
  try:
 
324
  final_path_with_audio = video_to_audio(
325
  final_path,
326
  prompt=prompt,
@@ -328,16 +353,25 @@ def generate_video(image, prompt):
328
  seed=-1,
329
  num_steps=25,
330
  cfg_strength=4.5,
331
- duration=8
332
  )
333
 
334
- # ์ž„์‹œ ํŒŒ์ผ ์ •๋ฆฌ
335
- if output_path != final_path:
336
- os.remove(output_path)
337
- if final_path != final_path_with_audio:
338
- os.remove(final_path)
 
 
 
 
 
 
 
 
 
 
339
 
340
- return final_path_with_audio
341
  except Exception as e:
342
  logger.error(f"Error in audio processing: {str(e)}")
343
  return final_path # ์˜ค๋””์˜ค ์ฒ˜๋ฆฌ ์‹คํŒจ ์‹œ ์›Œํ„ฐ๋งˆํฌ๋งŒ ๋œ ๋น„๋””์˜ค ๋ฐ˜ํ™˜
 
79
  rng.seed()
80
  fm = FlowMatching(min_sigma=0, inference_mode='euler', num_steps=num_steps)
81
 
82
+ # video_info = load_video(video_path, duration) ๋Œ€์‹ :
83
+ video_info = load_video(video_path, static_duration=duration)
84
 
85
+ if video_info is None:
86
+ logger.error("Failed to load video")
87
+ return video_path
88
+
89
  clip_frames = video_info.clip_frames
90
  sync_frames = video_info.sync_frames
91
  actual_duration = video_info.duration_sec
92
+
93
+ if clip_frames is None or sync_frames is None:
94
+ logger.error("Failed to extract frames from video")
95
+ return video_path
96
+
97
  clip_frames = clip_frames.unsqueeze(0)
98
  sync_frames = sync_frames.unsqueeze(0)
99
+
100
+ # ์‹œํ€€์Šค ๊ธธ์ด ์—…๋ฐ์ดํŠธ
101
  seq_cfg.duration = actual_duration
102
  net.update_seq_lengths(seq_cfg.latent_seq_len, seq_cfg.clip_seq_len, seq_cfg.sync_seq_len)
103
 
104
+ # ์˜ค๋””์˜ค ์ƒ์„ฑ
105
  audios = generate(clip_frames,
106
+ sync_frames,
107
+ [prompt],
108
+ negative_text=[negative_prompt],
109
+ feature_utils=feature_utils,
110
+ net=net,
111
+ fm=fm,
112
+ rng=rng,
113
+ cfg_strength=cfg_strength)
114
+
115
+ if audios is None:
116
+ logger.error("Failed to generate audio")
117
+ return video_path
118
+
119
  audio = audios.float().cpu()[0]
120
 
121
+ # ๊ฒฐ๊ณผ ๋น„๋””์˜ค ์ƒ์„ฑ
122
+ output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name
123
+ success = make_video(video_info, output_path, audio, sampling_rate=seq_cfg.sampling_rate)
 
124
 
125
+ if not success:
126
+ logger.error("Failed to create video with audio")
127
+ return video_path
128
+
129
+ logger.info(f'Successfully saved video with audio to {output_path}')
130
+ return output_path
131
+
132
  except Exception as e:
133
  logger.error(f"Error in video_to_audio: {str(e)}")
134
  return video_path # ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์›๋ณธ ๋น„๋””์˜ค ๋ฐ˜ํ™˜
 
345
 
346
  # ์˜ค๋””์˜ค ์ฒ˜๋ฆฌ ์ถ”๊ฐ€
347
  try:
348
+ logger.info("Starting audio generation process")
349
  final_path_with_audio = video_to_audio(
350
  final_path,
351
  prompt=prompt,
 
353
  seed=-1,
354
  num_steps=25,
355
  cfg_strength=4.5,
356
+ duration=8.0 # float ํƒ€์ž…์œผ๋กœ ๋ช…์‹œ
357
  )
358
 
359
+ if final_path_with_audio != final_path:
360
+ logger.info("Audio generation successful")
361
+ # ์ž„์‹œ ํŒŒ์ผ ์ •๋ฆฌ
362
+ try:
363
+ if output_path != final_path:
364
+ os.remove(output_path)
365
+ if final_path != final_path_with_audio:
366
+ os.remove(final_path)
367
+ except Exception as e:
368
+ logger.warning(f"Error cleaning up temporary files: {str(e)}")
369
+
370
+ return final_path_with_audio
371
+ else:
372
+ logger.warning("Audio generation skipped, using original video")
373
+ return final_path
374
 
 
375
  except Exception as e:
376
  logger.error(f"Error in audio processing: {str(e)}")
377
  return final_path # ์˜ค๋””์˜ค ์ฒ˜๋ฆฌ ์‹คํŒจ ์‹œ ์›Œํ„ฐ๋งˆํฌ๋งŒ ๋œ ๋น„๋””์˜ค ๋ฐ˜ํ™˜