oKen38461 commited on
Commit
2c6d7a1
·
1 Parent(s): dbc9d17

映像処理機能を改善し、オーディオをマージする際の一時ファイルを作成して削除するロジックを追加しました。音声が無い場合のエラーハンドリングも強化しました。

Browse files
Files changed (1) hide show
  1. ben_base.py +16 -9
ben_base.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import math
3
  import torch
4
  import torch.nn as nn
@@ -1102,18 +1101,26 @@ class BEN_Base(nn.Module):
1102
  alpha_webm_path = os.path.join(output_path, "foreground.webm")
1103
  pil_images_to_webm_alpha(foregrounds, alpha_webm_path, fps=original_fps)
1104
  else:
1105
- fg_output = os.path.join(output_path, 'foreground.mp4')
1106
- pil_images_to_mp4(foregrounds, fg_output, fps=original_fps, rgb_value=rgb_value)
1107
- cv2.destroyAllWindows()
1108
-
 
 
1109
  try:
1110
- fg_audio_output = os.path.join(output_path, 'foreground.mp4')
1111
- add_audio_to_video(fg_output, video_path, fg_audio_output)
 
 
1112
  except Exception as e:
1113
- print("No audio found in the original video")
 
1114
  print(e)
 
 
 
 
1115
 
1116
-
1117
 
1118
 
1119
 
 
 
1
  import math
2
  import torch
3
  import torch.nn as nn
 
1101
  alpha_webm_path = os.path.join(output_path, "foreground.webm")
1102
  pil_images_to_webm_alpha(foregrounds, alpha_webm_path, fps=original_fps)
1103
  else:
1104
+ # 1) 一時ファイルに映像のみを書き出す
1105
+ fg_no_audio = os.path.join(output_path, 'foreground_noaudio.mp4')
1106
+ pil_images_to_mp4(foregrounds, fg_no_audio, fps=original_fps, rgb_value=rgb_value)
1107
+
1108
+ # 2) オーディオをマージして最終ファイルを生成
1109
+ final_fg_output = os.path.join(output_path, 'foreground.mp4')
1110
  try:
1111
+ add_audio_to_video(fg_no_audio, video_path, final_fg_output)
1112
+ # 映像のみの一時ファイルを削除
1113
+ if os.path.exists(fg_no_audio):
1114
+ os.remove(fg_no_audio)
1115
  except Exception as e:
1116
+ # 音声が無い場合は映像のみのファイルをそのままリネーム/保持
1117
+ print("No audio track found or failed to merge audio. Keeping video without audio.")
1118
  print(e)
1119
+ if not os.path.exists(final_fg_output):
1120
+ os.rename(fg_no_audio, final_fg_output)
1121
+
1122
+ cv2.destroyAllWindows()
1123
 
 
1124
 
1125
 
1126