Spaces:
Runtime error
Runtime error
| import subprocess | |
| import os | |
| def merge_video(frames_dir, audio_path, output_name="output.mp4"): | |
| """ | |
| ์ด๋ฏธ์ง ํ๋ ์๋ค๊ณผ ์ค๋์ค ํ์ผ์ ํ๋๋ก ํฉ์น๋ ํจ์ | |
| :param frames_dir: ์ด๋ฏธ์ง ํ์ผ๋ค์ด ๋ค์ด์๋ ํด๋ ๊ฒฝ๋ก | |
| :param audio_path: ํฉ์ฑํ ์ค๋์ค ํ์ผ ๊ฒฝ๋ก | |
| :param output_name: ์ต์ข ๊ฒฐ๊ณผ๋ฌผ ํ์ผ ์ด๋ฆ | |
| """ | |
| # 1. ์ด๋ฏธ์ง ํ์ผ ํจํด ์ค์ (์: frame_0001.png, frame_0002.png ...) | |
| image_pattern = os.path.join(frames_dir, "frame_%04d.png") | |
| # 2. FFmpeg ๋ช ๋ น์ด ๊ตฌ์ฑ | |
| command = [ | |
| 'ffmpeg', | |
| '-y', # ๊ธฐ์กด ํ์ผ์ด ์์ผ๋ฉด ๋ฎ์ด์ฐ๊ธฐ | |
| '-framerate', '30', # ์ด๋น ํ๋ ์ ์ (FPS) | |
| '-i', image_pattern, # ์ ๋ ฅ ์ด๋ฏธ์ง ๊ฒฝ๋ก | |
| '-i', audio_path, # ์ ๋ ฅ ์ค๋์ค ๊ฒฝ๋ก | |
| '-c:v', 'libx264', # ๋น๋์ค ์ฝ๋ฑ (H.264) | |
| '-pix_fmt', 'yuv420p', # ๋๋ถ๋ถ์ ์ฌ์๊ธฐ์์ ์ง์ํ๋ ํฝ์ ํฌ๋งท | |
| '-c:a', 'aac', # ์ค๋์ค ์ฝ๋ฑ (AAC) | |
| '-shortest', # ์์/์ค๋์ค ์ค ์งง์ ์ชฝ์ ๋ง์ถค | |
| output_name # ์ต์ข ํ์ผ๋ช | |
| ] | |
| try: | |
| # 3. ๋ช ๋ น์ด ์คํ | |
| print(f"ํฉ์ฑ์ ์์ํฉ๋๋ค: {output_name}") | |
| subprocess.run(command, check=True) | |
| print("ํฉ์ฑ์ด ์๋ฃ๋์์ต๋๋ค!") | |
| except subprocess.CalledProcessError as e: | |
| print(f"ํฉ์ฑ ์ค ์ค๋ฅ ๋ฐ์: {e}") | |
| except FileNotFoundError: | |
| print("FFmpeg๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. ๊ฒฝ๋ก ์ค์ ์ ํ์ธํด ์ฃผ์ธ์.") | |
| # ์ฌ์ฉ ์์ (๋์ค์ ์ค์ ๊ฒฝ๋ก๋ก ์์ ) | |
| # merge_video("./frames", "./background_music.mp3") | |