Update img_animation.py
Browse files- img_animation.py +49 -46
img_animation.py
CHANGED
|
@@ -1,46 +1,49 @@
|
|
| 1 |
-
from moviepy.editor import *
|
| 2 |
-
import os
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
.
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
#
|
| 41 |
-
final_video.
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from moviepy.editor import *
|
| 2 |
+
import os
|
| 3 |
+
from moviepy.editor import (
|
| 4 |
+
AudioFileClip,
|
| 5 |
+
ImageClip,
|
| 6 |
+
concatenate_videoclips
|
| 7 |
+
image_folder = "img"
|
| 8 |
+
audio_path = r"C:\Users\danis\Desktop\fast_api\orchestral-joy-30-sec-423312.mp3"
|
| 9 |
+
output_video = "final_video.mp4"
|
| 10 |
+
|
| 11 |
+
# Load audio
|
| 12 |
+
audio = AudioFileClip(audio_path)
|
| 13 |
+
audio_duration = audio.duration
|
| 14 |
+
|
| 15 |
+
# Load images
|
| 16 |
+
images = sorted(os.listdir(image_folder))
|
| 17 |
+
num_images = len(images)
|
| 18 |
+
|
| 19 |
+
# Duration per image
|
| 20 |
+
img_duration = audio_duration / num_images
|
| 21 |
+
|
| 22 |
+
clips = []
|
| 23 |
+
|
| 24 |
+
for img in images:
|
| 25 |
+
img_path = os.path.join(image_folder, img)
|
| 26 |
+
|
| 27 |
+
clip = (
|
| 28 |
+
ImageClip(img_path)
|
| 29 |
+
.set_duration(img_duration)
|
| 30 |
+
.resize(height=720) # resize for video
|
| 31 |
+
.fadein(0.5) # fade in
|
| 32 |
+
.fadeout(0.5) # fade out
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
clips.append(clip)
|
| 36 |
+
|
| 37 |
+
# Combine image clips
|
| 38 |
+
video = concatenate_videoclips(clips, method="compose")
|
| 39 |
+
|
| 40 |
+
# Add audio
|
| 41 |
+
final_video = video.set_audio(audio)
|
| 42 |
+
|
| 43 |
+
# Export
|
| 44 |
+
final_video.write_videofile(
|
| 45 |
+
output_video,
|
| 46 |
+
fps=24,
|
| 47 |
+
codec="libx264",
|
| 48 |
+
audio_codec="aac"
|
| 49 |
+
)
|