TDN-M commited on
Commit
d76c658
·
verified ·
1 Parent(s): 4471c86

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +25 -0
utils.py CHANGED
@@ -19,6 +19,20 @@ def get_pexels_video(query):
19
  return random.choice(data['videos'])['video_files'][0]['link']
20
  return None
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def get_bgm_file(bgm_type: str = "random", bgm_file: str = ""):
23
  """
24
  Lấy file nhạc nền.
@@ -36,6 +50,17 @@ def get_bgm_file(bgm_type: str = "random", bgm_file: str = ""):
36
 
37
  return ""
38
 
 
 
 
 
 
 
 
 
 
 
 
39
  def combine_videos(combined_video_path: str,
40
  video_paths: list[str],
41
  audio_file: str,
 
19
  return random.choice(data['videos'])['video_files'][0]['link']
20
  return None
21
 
22
+ def download_video(url, save_path="downloaded_video.mp4"):
23
+ """
24
+ Tải video từ URL và lưu vào tệp cục bộ.
25
+ """
26
+ response = requests.get(url, stream=True)
27
+ if response.status_code == 200:
28
+ with open(save_path, 'wb') as f:
29
+ for chunk in response.iter_content(chunk_size=1024):
30
+ f.write(chunk)
31
+ return save_path
32
+ else:
33
+ logger.error(f"Failed to download video from {url}")
34
+ return None
35
+
36
  def get_bgm_file(bgm_type: str = "random", bgm_file: str = ""):
37
  """
38
  Lấy file nhạc nền.
 
50
 
51
  return ""
52
 
53
+ def add_background_music(video_path, bgm_file, output_path="video_with_bgm.mp4"):
54
+ """
55
+ Thêm nhạc nền vào video.
56
+ """
57
+ video = VideoFileClip(video_path)
58
+ bgm = AudioFileClip(bgm_file).subclip(0, video.duration)
59
+ final_audio = CompositeAudioClip([video.audio, bgm])
60
+ final_video = video.set_audio(final_audio)
61
+ final_video.write_videofile(output_path, audio_codec="aac")
62
+ return output_path
63
+
64
  def combine_videos(combined_video_path: str,
65
  video_paths: list[str],
66
  audio_file: str,